1.常用數據類型
1)整數:int, bit
2)小數:decimal ??? #decimal(5,2)表示共有五位數,保留兩位小數
3)字符串:varchar, char ??????????????????????
4)日期時間:date, time, datetime
5)枚舉類型(enum)
?
2.約束
1)主鍵primary key:物理上存儲的順序
2)非空not null:此字段不能為空
3)唯一unique:此字段不允許重復
4)默認default:當不填寫此值時會使用默認值,如果填寫則已填寫為準
5)外鍵foreign key:對關系字段進行約束,當為關系字段填寫值時,會到關聯的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗并拋出異常
數值類型 | |||
類型 | 字節大小 | 有符號范圍 | 無符號范圍 |
TINYINT | 1 | -127~127 | 0~255 |
SMALLINT | 2 | -32768~32767 | 0~65535 |
MEDIUMINT | 3 | -8388608~8388607 | 0~16777215 |
INT/INTEGER | 4 | -2147483648~2147483647 | 0~4294967265 |
BIGINT | 8 | -9223372036854775808~-9223372036854775807 | 0~18446744073709551615 |
字符串 | |||
類型 | 字節大小 | 示例 | |
CHAR | 0-255 | char(3)不管輸入幾個字節都會占3個字節 | |
VARCHAR | 0-255 | varchar(3)輸入比三小的字節會占用實際字節大小 | |
TEXT | 0-65535 | 大文本 | |
日期時間類型 | |||
DATE | 4 | ‘2020-01-01’ | |
TIME | 3 | ’12:05:34’ | |
DATETIME | 8 | ‘2020-01-01? 12:05:34’ | |
YEAR | 1 | ‘2019’ | |
TIMESTAMP | 4 | ‘1970-01-01? 00:00:01’UTC~‘2038-01-01? 00:00:01’UTC |
3.sql語句alter
顯示當前時間
select now();
?
創建classes表(id, name)
create table zzzz(
??? id int primary key not null auto_increment,
??? name varchar(20),
??? age int
);
?
查看表結構
desc zzzz
?
1)創建students表(id, name, age, high, gender, cls_id)
create table students (
??? id int unsigned not null auto_increment primary key,
??? name varchar(20),
??? age tinyint unsigned default 0,
??? high decimal(5,2),
??? gender enum('男', '女', '中性', '保密') default '保密',
??? cls_id int unsigned
);
?
創建classes表(id, name)
create table classes(
??? id int unsigned not null auto_increment primary key,
??? name varchar(20)
);
?
2)修改表屬性
修改表-添加字段
alter table 表名 add 列名 類型;
alter table students add birthday datetime;
?
修改表-修改字段:不重命名版
?alter table 表名 modify 列名 類型及約束;
alter table students modify birthday date;
?
修改表-修改字段:重命名版
alter table 表名 change 原名 新名 類型及約束;
alter table students change birthday birth date;
?
3)修改表-刪除字段
alter table 表名 drop 列名;
alter table students drop birthday;
?
4) 刪除表
drop table 表名;
drop table students;
?
4.sql語句增加insert
1)全列插入
insert into 表名 values(..)??? 主鍵字段 可以用0 null default 來站位
向students表里插入 一個學生信息
insert into students values (0,'小明',19,188.999,'男', 1);
?????????????
2)部分插入
??????? insert into students(id, name, age) values (0,'綠帽子',19);
??????? 部分插入(多條記錄)
??????? insert into students(id, name, age) values (0,'綠帽子',19),(0,'小跳蚤',21);
???????
5.sql語句修改update
update 表名 set 列1=值1, 列2=值2... where 條件;
update students set age=100 where id=1;
update students set age=100,cls_id=77 where id=1;
?
6.sql語句刪除delete與truncate
1)物理刪除
delete from 表名 where 條件
delete from students where cls_id=88;???
?
2)邏輯刪除
用一條字段來表示 這條信息是否已經不能在使用了
給students表添加一個is_delete字段 bit 類型
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=6;
?
3)truncate
清空表,連同id字段自增重置為1,重新插入的數據id默認會從1開始,用truncate刪除的數據無法恢復
truncate students
?
7.sql語句查看select
查詢基本使用(條件,排序,聚合函數,分組,分頁)
1)查詢所有列
select * from 表名
select * from students;
?
一定條件查詢(where)
select * from where id=5;
???
查詢制定列
select id,name from students;
???
使用as給字段起別名
select id,name as '姓名', age, high, gender from students;
???
通過表名字段查詢
select students.name from students;
???
給表起別名查詢
select s.id,s.name,s.age from students as s;
???
消除重復行
distinct
select distinct age from students;
?
2)條件查詢
查詢年紀大于18歲的信息
select * from students where age > 18;
???????
18歲到28歲之間(and)
select * from students where age >= 18 and age =< 28;??? 允許使用&&
select * from students where age between 18 and 28
?
在18歲以上或者身高180以上的人(or)
select * from students where age > 18 or high > 180;??? 允許使用||
?
3)模糊查詢like
% 替代1個或者多個甚至是沒有
查詢姓名中有‘小’的所有名字
select * from students where name like '%小%';
?
查詢兩個字人的名字
select * from students where name like '__';??? 兩個下劃線
?
查詢至少有2個字的名字
select * from students where name like '%__%';
?
4)范圍查詢
in (1,3,8)表示在一個非連續的范圍內
查詢年紀為18和34的人
select * from students where age in (18, 34);
?
查詢 年齡在17歲到34歲之間的信息
select * from students where age between 17 and 34;
?
查詢 年紀不在18到34歲的信息
select * from students where age not between 17 and 34;
?
5)空判斷
判斷is null
查詢身高為空的人的信息
select * from students where high is null;
?
6)排序order by
asc從小到大排列,即升序
desc從大到小排序,即降序
order by支持多字段
?
查詢年紀在18到34歲之間的男性,按照年紀從小到大
select * from students where gender=1 and age between 18 and 34 order by age;
?
查詢年紀在18到34歲之間的女性,身高從高到矮
select * from students where gender=2 and age between 18 and 34 order by high desc;
?
查詢年紀在18到34歲的男性,身高從高到矮排序,如果身高相同的情況下按照年紀從小到大排序,如果年齡也相等那么按照id從小到大排序;
select * from students where age between 18 and 34 and gender=1 order by high desc,age,id;
???????
8.聚合函數
1)總數count
查詢男性有多少人
select count(*) from students where gender=1;
?
2)最大值max
查詢最大的年紀
select max(age) from students;
?
查詢女性的最高身高
select max(high) from students where gender=2;
?
3)最小值 min
select min(high) from students;
?
4)求和sum
計算所有人的年齡總和
select sum(age) from students;
?
5)平均值avg
計算平均年紀 sum(age)/count(*)
select sum(age)/count(*) from students;
select avg(age),2 from students;
?
保留2位小數
select round(avg(age),2) from students;
???
6)分組group by
按照性別分組,查詢所有的性別
select gender from students group by gender;
?
計算每組性別的人數
select gender, count(*) from students group by gender;
?
查詢男性組中的姓名 group_concat
select gender,group_concat(name) from students where gender=1 group by gender;??? group_concat()按照拼接后的字符串顯示
?
7)having
查詢每個性別平均年紀超過30歲的性別,以及姓名 having avg(age) > 30
select gender, group_concat(name) from students group by gender having avg(age) > 30;
?
查詢每種性別中的人數多于4個的組的信息
select gender,group_concat(name) from students group by gender having count(*)>4;
?
8)分頁limit
顯示5頁
select * from students limit 5;
?
分頁顯示,每頁顯示2條數據
select * from students limit 0, 2;
?
按照身高從高到矮排序,查找出所有女性,并且分頁顯示,每頁顯示2條數據
select * from students where gender=2 order by high desc limit 0,2;
?