10.8 修改表、複製表、刪除表 10.81 修改表 alter table 10.82 複製表 10.83 刪除表 10.9 單表查詢 10.91 where過濾 10.92 group by分組 group_concat (不能做中間結果)、concat 、concat_ws 、as 10.93 ...
10.8 修改表、複製表、刪除表
10.81 修改表 alter table
1. 修改表名 alter table 表名 rename 新表名; 2. 增加欄位 alter table 表名 add 欄位名 數據類型 [完整性約束條件…]; alter table t1 add stu char(10) not null after name; #添加到name欄位之後 alter table t1 add sex enum('male','female') default 'male' first;#添加到最前面 3. 刪除欄位 alter table t1 drop sex; 4. 修改欄位(增加主鍵) alter table t1 modify age int(3); alter table t1 modify id int(11) primary key auto_increment; #修改為主鍵 alter table t1 change 舊欄位名 新欄位名 新數據類型 [完整性約束條件…]; 5. 對已經存在的表增加複合主鍵 alter table t1 add primary key(ip,port); 6. 刪除主鍵 a. 刪除自增約束 alter table t1 modify id int(11) not null; b. 刪除主鍵 alter table t1 drop primary key; 7. 修改存儲引擎 alter table it engine=innodb; 8. 增加主鍵(設置索引) alter table t1 add primary key(id);
10.82 複製表
create table new_t1 select * from t1; # 複製表結構+記錄,但是key和自增不會複製 alter table new_t1 modify id int(11) primary key auto_increment; #添加主鍵和自增 #條件為假,查不到任何記錄 create table new1_t1 select * from t1 where 1=2; #只複製表結構,但是key和自增不會複製 alter table new_t1 modify id int(11) primary key auto_increment; #添加主鍵和自增 create table t2 like t1; #只完全複製表結構,不複製記錄
10.83 刪除表
drop table t1;
10.9 單表查詢
語法
select distinct 查詢欄位1,查詢欄位2......... from 表名 where 分組之前的過濾條件 group by 分組條件 having 分組之後的過濾條件 order by 排序欄位1 asc,排序欄位2 desc limit 5,5;
10.91 where過濾
select id,name from db39.emp where id >= 3 and id <= 6 select * from db39.emp where id between 3 and 6; select * from emp where salary = 20000 or salary = 18000 or salary = 17000; select * from emp where salary in (20000,18000,17000); select * from emp where salary not in (20000,18000,17000); select * from emp where id not between 3 and 6; 要求:查詢員工姓名中包含i字母的員工姓名與其薪資 select name,salary from db39.emp where name like '%i%' 要求:查詢員工姓名是由四個字元組成的的員工姓名與其薪資 select name,salary from db39.emp where name like '____'; select name,salary from db39.emp where char_length(name) = 4; 要求:查詢崗位描述為空的員工名與崗位名 select name,post from db39.emp where post_comment is NULL; select name,post from db39.emp where post_comment is not NULL;
10.92 group by分組
#設置sql_mode為only_full_group_by,意味著以後但凡分組,只能取到分組的依據 mysql> set global sql_mode="strict_trans_tables,only_full_group_by"; #聚合函數:每個部門的最高、最低、平均、總工資,計數 select post,max(salary) from emp group by post; select post,min(salary) from emp group by post; select post,avg(salary) from emp group by post; select post,sum(salary) from emp group by post; select post,count(id) from emp group by post;
group_concat (不能做中間結果)、concat 、concat_ws 、as
#group_concat(分組之後使用):取出分組後,組內定製的詳細信息 select post,group_concat(name) from emp group by post; select post,group_concat(name,"_SB") from emp group by post; select post,group_concat(name,": ",salary) from emp group by post; select post,group_concat(name,":",age,":",sex) from emp group by post; +-----------------------------------------+-------------------------------------------------------- | post | group_concat(name,"_SB") | +-----------------------------------------+-------------------------------------------------------- | operation | 程咬鐵_SB,程咬銅_SB,程咬銀_SB,程咬金_SB,張野_SB | | sale | 格格_SB,星星_SB,丁丁_SB,丫丫_SB,歪歪_SB | |外交大使 | egon_SB | +-----------------------------------------+-------------------------------------------------------- # concat(不分組時用):自定製取出的結果 select name as 姓名,salary as 薪資 from emp; select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪資 from emp; +------------------+-----------------+ | 姓名 | 薪資 | +------------------+-----------------+ | NAME: egon | SAL: 7300.33 | | NAME: alex | SAL: 1000000.31 | | NAME: wupeiqi | SAL: 8300.00 | +------------------+-----------------+ # concat_ws (不分組時用):每個分組結果都用相同的分隔符時使用 select concat_ws(":",name,age,sex,post) as info from emp; +------------------------------------------------------+ | info | +------------------------------------------------------+ | egon:18:male:外交大使 | | 程咬金:18:male:operation | | 程咬銀:18:female:operation | | 程咬銅:18:male:operation | | 程咬鐵:18:female:operation | +------------------------------------------------------+ # 補充as語法 mysql> select emp.id,emp.name from emp as t1; # 報錯 mysql> select t1.id,t1.name from emp as t1; # 查詢四則運算 select name,salary*12 as annual_salary from emp;
10.93 having過濾
having的語法格式與where一模一樣,只不過having是在分組之後進行的進一步過濾,即where不能用聚合函數,而having是可以用聚合函數的
#統計各部門年齡在30歲以上的員工平均工資,並且保留平均工資大於10000的部門 select post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000; +---------+---------------+ | post | avg(salary) | +---------+---------------+ | teacher | 255450.077500 | +---------+---------------+ #強調:having必須在group by後面使用 select * from emp having avg(salary) > 10000;#報錯
10.94 distinct去重
select distinct post from emp; +-----------------------------------------+ | post | +-----------------------------------------+ | 外交大使 | | teacher | | sale | | operation | +-----------------------------------------+
10.95 order by 排序
select * from emp order by salary (asc); #預設升序排 select * from emp order by salary desc; #降序排 select * from emp order by age desc,salary asc; #先按照age降序排,如果相同再按照薪資升序排 # 統計各部門年齡在10歲以上的員工平均工資,並且保留平均工資大於1000的部門,然後對平均工資進行排序 select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary); +-----------------------------------------+---------------+ | post | avg(salary) | +-----------------------------------------+---------------+ | sale | 2600.294000 | |外交大使 | 7300.330000 | | operation | 16800.026000 | | teacher | 151842.901429 | +-----------------------------------------+---------------+
10.96 limit 限制顯示條數
select * from emp limit 3; #從頭開始只顯示3條信息 select * from emp order by salary desc limit 1; #找到工資最大的一條信息 +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+ | id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+ | 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+ # 分頁顯示 select * from emp limit 0,5;#從0開始顯示5條信息(1-5) select * from emp limit 5,5;#從5開始顯示5條信息(6-10)
10.97 正則表達式
select * from emp where name regexp '^jin.*(n|g)$'; +----+------------+--------+-----+------------+---------+--------------+----------+--------+ | id | name | sex | age | hire_date | post |post_comment| salary | office | depart_id | +----+------------+--------+-----+------------+---------+--------------+----------+--------+ | 6 | jingliyang|female|18| 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 7 | jinxin |male |18| 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | +----+------------+--------+-----+------------+---------+--------------+----------+--------+