亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

Mysql數(shù)據(jù)庫基礎(chǔ)知識(shí)點(diǎn)梳理總結(jié)/三

來源:一匹脫韁的野馬 發(fā)布時(shí)間:2019-04-26 16:12:08 閱讀量:1354

一、表格設(shè)計(jì)

多對(duì)多

(1)需要?jiǎng)?chuàng)建第三張表,并且公共字段可以放入到第三張表中


create table teacher11(


id int PRIMARY key auto_increment,


name VARCHAR(20) not null,


sub VARCHAR(20) not null


);


create table student11(


id int PRIMARY KEY auto_increment,


name VARCHAR(20) not null,


sex VARCHAR(5) not null


);


create table tea_std11(


id int PRIMARY KEY auto_increment,


t_id int,


s_id int,


score int,


CONSTRAINT teacher_fk FOREIGN KEY (t_id) REFERENCES teacher11(id),


CONSTRAINT student_fk FOREIGN KEY (s_id) REFERENCES student11(id)


);


查詢id為1的老師教過的所有學(xué)生

步驟一 ,從第三張表中查出老師教過的學(xué)生


select * from tea_std11 where t_id = 1;




步驟二 根據(jù)上面id取查詢學(xué)生的信息,(1,2)代表學(xué)生id


select * from student11 where id in (1,2);




(2)設(shè)置外鍵對(duì)應(yīng)其它表中的主鍵


 

一對(duì)一

一般情況一張表即可

也可以設(shè)計(jì)成兩張表格

第二張表格中的創(chuàng)建字段作為外鍵(設(shè)置成唯一 unique)對(duì)應(yīng)第一張表格中的主鍵

直接使用第二張表格的主鍵作為外鍵,對(duì)應(yīng)第一張表格中的主鍵

create table users(

id int primary key auto_increment,

name varchar(20),

age int

);


create table card(

id int PRIMARY key,

num VARCHAR(20) not null,

address VARCHAR(100) not null,



CONSTRAINT user_card_fk FOREIGN KEY(id) REFERENCES users(id)

)


一對(duì)多

示例如下:


創(chuàng)建客戶表:


CREATE TABLE customers(


       id int,


       name varchar(100),


       address varchar(255),


       PRIMARY KEY(id)


);


創(chuàng)建訂單表:


CREATE TABLE orders(


       order_num int primary key,


       price float(8,2),


       status int,


       customer_id int,


       注:constraint: 約束的意思。foreign key: 外鍵。references: 參照


CONSTRAINT customer_id_fk FOREIGN KEY(customer_id) REFERENCES customers(id)


);


 


二、多表查詢

1.交叉連接 笛卡爾積(查詢結(jié)果是錯(cuò)誤的)


2.內(nèi)連接


(1)語法:


隱式:select * from 表1,表2 where 條件;


例:select * from users,card;


顯示:select * from 表1 inner join 表2 ON 條件;


例:select * from users CROSS JOIN card;



查詢出每個(gè)用戶及身份證信息,顯式出來

select * from users,card where users.id =card.id;


使用別名

select * from users as u,card as c where u.id =c.id;


u.*表示users表格中的所有字段,c.num表示c中的字段num

select u.*,c.num,c.address from users as u,card as c where u.id =c.id;


3.外連接


(1)左外鏈接


語法:select * from 表1 LEFT JOIN 表2 ON 條件


連接規(guī)則:沒有拼接上就顯示為空


select * from card as c LEFT JOIN student as s ON c.id = s.id;


(2)右外連接


語法:select * from 表1 RIGHT JOIN 表2 ON 條件


連接規(guī)則:沒有拼接上就顯示為空


select * from card as c RIGHT JOIN student as s ON c.id = s.id;


4.子查詢


嵌套子查詢


執(zhí)行流程:先執(zhí)行內(nèi)部,后執(zhí)行外部查詢


特點(diǎn):內(nèi)部查詢可以單獨(dú)運(yùn)行


 


使用嵌套子查詢


select * from student11 where id in (select s_id from tea_std11 where t_id=1);




相關(guān)子查詢


執(zhí)行流程:先執(zhí)行外部查詢,將記錄傳遞給內(nèi)部


特點(diǎn):內(nèi)部查詢需要外部查詢的字段


 


 


create table teacher11(

id int PRIMARY key auto_increment,

name VARCHAR(20) not null,

sub VARCHAR(20) not null

);

create table student11(

id int PRIMARY KEY auto_increment,

name VARCHAR(20) not null,

sex VARCHAR(5) not null

);

create table tea_std11(

id int PRIMARY KEY auto_increment,

t_id int,

s_id int,

score int,

CONSTRAINT teacher_fk FOREIGN KEY (t_id) REFERENCES teacher11(id),

CONSTRAINT student_fk FOREIGN KEY (s_id) REFERENCES student11(id)

);


查詢id為1的老師教過的所有學(xué)生

步驟一 ,從第三張表中查出老師教過的學(xué)生

select * from tea_std11 where t_id = 1;

步驟二 根據(jù)上面id取查詢學(xué)生的信息,(1,2)代表學(xué)生id

select * from student11 where id in (1,2);


使用嵌套子查詢

select * from student11 where id in (select s_id from tea_std11 where t_id=1);


相關(guān)子查詢


求t_id=1的平均分

select avg(score) from tea_std11 where t_id=1;


求:每一科考試成績大于平均分的學(xué)生分?jǐn)?shù)

select * from tea_std11 as t1 where t1.score>(select avg(score) from tea_std11 t2 WHERE t1.t_id=t2.t_id);


select * from tea_std11;




select avg(score) from tea_std11 where t_id=1;




select * from tea_std11 as t1 where t1.score>(select avg(score) from tea_std11 t2 WHERE t1.t_id=t2.t_id);




常用函數(shù)

聚合函數(shù)


Count()計(jì)數(shù)


(1)統(tǒng)計(jì)一個(gè)班多少學(xué)生

select count(id) from student;

select count(*) from student;

(2)統(tǒng)計(jì)ui分?jǐn)?shù)大于60的學(xué)生個(gè)數(shù)

select count(*) from score where ui>60;

統(tǒng)計(jì)總分大于200的人數(shù)

select count(*) from score where (python+ui+mysql)>200;


Sum()  求和



(1)統(tǒng)計(jì)一個(gè)班mysql總成績

select sum(mysql) from score;

(2)統(tǒng)計(jì)各科的總成績

select sum(python),sum(mysql),sum(ui) from score;

(3)統(tǒng)計(jì)python,mysql,ui總成績之和

select sum(python+mysql+ui) from score;


Avg()   求平均數(shù)


統(tǒng)計(jì)一個(gè)班python成績的平均分

select avg(python) from score;

select sum(python)/count(python) from score;


 


select avg(python+mysql+ui) from score;


Max(),min() 最大最小


select min(ui),max(ui) from score;


Group by分組


create table t_order(id int primary key,product varchar(20),price float(8,2));

insert into t_order values(1,'xiaomi', 1000);

insert into t_order values(2,'xiaomi',1100);

insert into t_order values(3,'huawei',2200);

insert into t_order values(4,'apple',8200);

1.對(duì)訂單表中商品歸類后,顯示每一類商品的總價(jià)

select product,sum(price) from t_order GROUP BY product;

2.查詢每一類商品總價(jià)格大于3000的商品

select product,sum(price) as total from t_order GROUP BY product HAVING total>3000; 


having注意事項(xiàng)


WHERE是在分組(group by)前進(jìn)行條件過濾,


HAVING子句是在分組(group by)后進(jìn)行條件過濾,


WHERE子句中不能使用聚合函數(shù),HAVING子句可以使用聚合函數(shù)。



分享:
評(píng)論:
你還沒有登錄,請(qǐng)先