1. 程式人生 > >mysql表間的關係和查詢

mysql表間的關係和查詢

mysql間的關係及查詢
多對一
# 先建立被關聯的表dep                
create table dep(id primary key auto_increment,
                 dep_name char(10),
                 dep_comment char(60))
                 
# 後建立關聯表emp              
create table emp(id int primary key auto_increment,
                 name char(15),
                 gender enum('male','femael')not null default 'male',
                 de_id int,
                 foreign key(dep_id),references dep(id));
                 
# 先向被關聯的表dep中插入值
insert into dep(dep_name,dep_comment) values('教學部','給學生上課',
                                             '外交部','代表學生的良好形象',
                                             '技術部','維護教學裝置'));
                                             
# 再向關聯表emp中插入值
insert into emp(name,gender,de_id) values(('aaa','male',1),
                                          ('bbb','male',2),
                                          ('ccc','female',3),
                                          ('ddd','male',2)
                                            );
多對多關係

表一:book

id book_name price
1 九陰真經 100
2 葵花寶典 200
3 吸星大法 300

表二:press

id press_name phone
1 人民出版社 123
2 青春文學 234
3 北京郵電出版社 345

一本書可以是多個出版社出版的,所以需要單獨建一個關係表出來

id book_id p_id
1 1 1
2 1 3
3 2 1
建立表
# 建立表1
create table book(id int primary key auto_increment,
                  book_name char(15),
                  price int(5));
# 建立表2
create table press(id int primary key auto_increment,
                   press_name char(20),
                   phone int(11));                 
# 建立關係表
create table b_p(id int primary key auto_increment,
                 book_id int(3),
                 p_id int(3),
                foreign key(book_id) references book(id) on update cascade on delete cascade,
                foreign key(p_id) references press(id) on update cascade on delete cascade);

# 插入值
insert into book values(1,'九陰真經',100),(2,'九陽神功',200),(3,'吸星大法',300);

insert into press(press_name) values('人民出版社'),('青春文學'),('北京郵電出版社');

insert into b_p(book_id,p_id) values(1,1),(1,3),(2,1);
修改表和複製表
# 修改表名
    alter table 表名 rename 新表名;
    
    
# 增加新欄位
    alter table 表名 add 欄位名 資料型別[約束條件];
    

# 刪除欄位
    alter table 表名 drop 欄位名
    
    
# 修改欄位
    alter table 表名 modify 欄位名 資料型別[約束條件];
    
    alter table 表名 change 舊欄位名 新欄位名 舊資料型別[約束條件];
    
    alter table 表名 change 舊欄位名 新欄位名 新資料型別[約束條件];
    
    
# 複製表
    create table book_copy select *from book;
    
單表查詢
select id,name from emp
    where id >1 and name like'%on%'
    group by dep_id
    having 分組後的過濾條件
    order by 排序依據
    limit n;
group by & 聚合函式
# 設定sql_mode 為 only_full_group_by

select 欄位1 from 表名 group by 欄位1     # 注意前後兩個欄位名要相同

# 每個部門員工的最高工資
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(gender) from emp group by post;

select post,avg(salary) from emp where age>=30 group by post;
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;
concat & as
select concate('姓名',name) from emp ;

select name,salary*12 as annual_salary from emp;

select concate('姓名',name) as 姓名  from emp;
having
# 1. having 的語法給是與where一模一樣,但是having是在group by之後進行一步過濾
# 2. where不能用聚合函式,但是having可以用聚合函式

# 統計個部門年齡在30歲以上的員工的平均工資,並且保留平均工資大於10000的部門
select post,avg(salary) from emp
                        where age>=30
                        group by post
                        having avg(salary)>10000;
            
# 強調: having必須在group by後用
distinct
# 去重,執行順序在having之後
select distinct post,avg(salary) from emp
        where age>=30
        group by post
        having avg(salary)>10000;
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; # 多個排序標準,

# 統計個部門年齡大於10歲,平均工資大於1000,且升序排列
select distinct post,avg(salary)from emp where age >10 
            group by post
            having avg(salary) > 1000
            order by post;
limit
# 限制顯示條數

select * from emp limit 3;

# 選擇工資前三的員工資訊
select * from emp order by salary desc limit 3;

# 分頁顯示
select * from emp limit 5,5;
正則匹配
select * from emp where name regexp '^jin.*(n|g)$';

# $表示結束
# regexp關鍵字
多表連線
建表
create table department(id int,name varchar(20));

create table employee(id int primary key auto_increment,
                      name varchar(20),sex enum('male','female')not null default 'male',age intm dep_id int);
笛卡爾積查詢
select * from emp,dep where emp.dep_id = dep.id
內連線
select * from emp inner join dep on emp.dep_id = dep.id;
左連線
select * from emp left join dep on emp.dep_id = dep.id;
右連線
select * from right join dep on emp.dep_id = dep.id;
全連線
select * from emp left join dep on emp.dep_id = dep.id
union
select * from right join dep on emp.dep_id = dep.id;
子查詢
select * from emp inner join dep on demp.dep_id = dep.id
select name from emp where dep_id = (select id from dep where name = '技術');