日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

数据库SQL基础知识点

發布時間:2024/1/23 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库SQL基础知识点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

**

1、 多表查詢

**

– 創建teacher 表

create table teacher( id int, name VARCHAR(20) not null, gender CHAR(5) not null, primary key(id) );

– 創建學生表格

create table student( id int, name VARCHAR(20) not NULL, age int not null ); alter table student add primary key(id);

– 第三張關系表

create table tea_std( id int PRIMARY key auto_increment, teacher_id int, student_id int, CONSTRAINT fk_teacher FOREIGN KEY(teacher_id) REFERENCES teacher(id), CONSTRAINT fk_student FOREIGN key(student_id) REFERENCES student(id) );

一對一

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) )drop table card; drop table users;

多表查詢
– 交叉連接,笛卡爾積, 注意 結果不正確。
– 隱式

select * from users,card ;

– 顯式、使用關鍵字

select * from users CROSS JOIN card;

內連接
– 查詢出每個用戶及身份證信息顯示出來。

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

– 表格使用別名

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

/**
u.* 表示 users 表格中所有的字段,
c.num、c.address 表示 card 表中的字段。
**/

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

– 查詢用戶對應的所有訂單信息以及個人信息。

select * from customers as c,orders as o where c.id=o.customer_id;

– 使用關鍵字 INNER JOIN

select * from customers as c INNER JOIN orders as o ON c.id=o.customer_id;

左外連接

select * from customers as c LEFT JOIN orders as o ON c.id= o.customer_id;

右 外連接

select * from customers as c RIGHT JOIN orders as o ON c.id=o.customer_id;select * from orders as o RIGHT JOIN customers as c ON c.id=o.customer_id;

多對多

create table teacher( id int primary key auto_increment, name varchar(20) not null, sub VARCHAR(20) not null);create table student( id int primary key auto_increment, name varchar(20) not null, gender varchar(5) not null );create table tea_std( id int primary key auto_increment, t_id int, s_id int,score int,CONSTRAINT teacher_fk FOREIGN KEY(t_id) REFERENCES teacher(id), CONSTRAINT student_fk FOREIGN KEY(s_id) REFERENCES student(id) );

– 查詢出id為1的老師教過的所有學生。

– 步驟1,先從第三張表中查詢出教過的學生的id

select s_id from tea_std where t_id = 1;

– 步驟2:根據id 去學生表中查找學生的信息。

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

– 使用嵌套子查詢

select * from student where id in(select s_id from tea_std where t_id = 1);

– 相關子查詢

– 求:每一科考試成績大于平均分的學生的分數。

-- select * from tea_std; -- select avg(score) from tea_std where t_id=1;select * from tea_std as t1 where t1.score>(select avg(t2.score) from tea_std t2 where t1.t_id=t2.t_id);

– 分析步驟:
– 先執行 外部查詢 拿到第一條記錄
– 1 1 1 60

-- select * from tea_std as t1 where t1.score>(select avg(t2.score) from tea_std t2 where t2.t_id=1);

– 執行內部查詢(子查詢)

-- select avg(t2.score) from tea_std t2 where t2.t_id=1

– 1 1 1 60
– 2 1 2 90
– 75
– 比較結果 ,每一天記錄的分數 和 平均分進行比較。
– select * from tea_std as t1 where t1.score>75; 60>75

- 聚合函數

count() 計數 ,統計所有的記錄。
– (1)、統計一個班級共有多少學生?

select COUNT(*) from student;

– (2)、統計數學ui大于60的學生有多少個?

select COUNT(*) as total from score where ui>60;

– (2)、統計總分大于200的人數有多少?

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

– count(*) 不會忽略掉 null。 使用其他字段 會自動忽略null。

select count(*) from score; select count(python) from score;

– sum() 求和函數
– (1)、統計一個班級mysql總成績?

select sum(mysql) from score;

– (2)、統計一個班級python、mysql、ui各科的總成績

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

– (3)、統計一個班級每一個人的 python、mysql、ui的成績總和。
– `

select s1.name, s1.python,s1.mysql ,s1.ui, s1.python+s1.mysql+s1.ui '總分' from score s1 , score s2 where s1.id=s2.id;` select name,python,mysql,ui ,python+mysql+ui as total from score;

– (4)、統計一個班級python成績平均分

select AVG(python) from score; select sum(python)/COUNT(python) from score; select sum(python)/COUNT(*) from score;

– avg(): 求平均數。
– (2)、求一個班級總分平均分
– SELECT AVG(chinese+english+math) FROM student
– min() 求最小 、max() 求最大

select min(ui),max(ui) from score; 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);select * from t_order;

– 1.對訂單表中商品歸類后,顯示每一類商品的總價

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

– 2.對訂單表中商品歸類后,查詢每一類商品總價格大于3000的商品

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

總結

以上是生活随笔為你收集整理的数据库SQL基础知识点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。