供自己面試複習使用,資料庫為mysql
一、資料庫連接配接
1.1、Mysql資料庫連接配接
mysql -h localhost -u root -p
1.2、檢視和切換資料庫
show databases;
use test;
1.3、檢視資料庫中有哪些表
二、表操作
2.1、建立表
2.2、插入資料
2.3、檢視資料
2.4、修改資料
2.5、删除資料
2.6、檢視表結構
2.6、删除表
三、select查詢
為友善的進行練習,建立students學生、course課程、teachers教師三個表。
create table students(stu_id char(10) primary key,stu_name char(100),age int,city char(100));
create table course(c_id char(10),stu_id char(10),t_id char(10),score int,primary key (c_id,stu_id));
create table teachers(t_id char(10) primary key,t_name char(100));
insert into students values("101","lily",18,"beijing"),("102","lucy",18,"beijing"),("103","tom",20,"shanghai"),("104","lilei",19,"guangzhou"),("105","su",18,"shanghai");
insert into course values("math","101","t01",100),("math","102","t01",98),("math","103","t01",69),("math","104","t01",100);
insert into course values("chinese","101","t02",90),("chinese","102","t02",100),("chinese","103","t02",55),("chinese","105","t02",88);
insert into teachers values("t01","zhang"),("t02","wang"),("t03","li");
3.1、排序
檢視國文課的成績表,按學分從低到高進行排序
檢視數學課的成績表,按學分從高到低進行排序(分數相同的,按學号升序排序)
3.2、去除重複
檢視學生所在的城市,城市重複的隻顯示一次
3.3、限制結果
從學生表中,檢視stu_id前3名同學的資訊
從學生表中,檢視stu_id第4到第5名學生的資訊
3.4、模糊查詢
檢視名稱中包含"li"的學生資訊
3.5、組合過濾
檢視來自beijing或shanghai的學生資訊
select * from students where city in ("beijing","shanghai");
select * from students where city ="beijing" or city ="shanghai";
檢視數學分數超過90的學生id
3.6、拼接字段
将所有學生的資訊按以下格式顯示出來:學号:101 姓名:lily
3.7、聚集函數(包含5種:count、最大值、最小值、總值、平均值)
檢視數學課程的學生總數、最高分、最低分、總分、平均分
3.8、分組查詢
檢視各個學科的平均分
檢視各個學科中成績為100分的學生個數
3.9、表聯結
檢視學生的成績表,其中包括學生學号、姓名、課程編号、分數。
檢視選修了"wang"老師課程的所有學生學号和學生名稱
也可以使用别名
select s.stu_id,s.stu_name from students as s,course as c,teachers as t where s.stu_id=c.stu_id and c.t_id=t.t_id and t.t_name="wang";