mysql 創建學生表

– 查看MySQL服務器所有數據庫

show databases;

– 刪除SRS數據庫

drop database if exists SRS;

– 創建學生選課系統(SRS)數據庫並指定默認字符集

create database SRS default charset utf8;

– 切換至SRS數據庫

use SRS;

– 查看當前數據庫中所有表

show tables;

– 創建學生表

create table TbStudent
(
stuid integer not null,
stuname varchar(20) not null,
stusex bit default 1,
stubirth datetime not null,
stutel char(11),
stuaddr varchar(255),
stuphoto longblob,
primary key (stuid)
);

– 修改學生表刪除stutel列

alter table TbStudent drop column stutel;

– 查看學生表結構

desc TbStudent;

– 如果表TbCourse已經存在就刪除它

drop table if exists TbCourse;

– 創建課程表

create table TbCourse
(
cosid integer not null,
cosname varchar(50) not null,
coscredit tinyint not null,
cosintro varchar(255)
);

– 給課程表指定主鍵

alter table TbCourse add constraint pk_course primary key (cosid);

– 創建學生選課記錄表

create table TbSC
(
scid integer primary key auto_increment,
sid integer not null,
cid integer,
scdate datetime not null,
score float
);

添加外鍵

alter table TbSC add foreign key (sid) references TbStudent (stuid) on delete cascade on update cascade;
alter table TbSC add foreign key (cid) references TBCourse (cosid) on delete set null on update cascade;

– 添加學生記錄

insert into TbStudent values (1001, '張三丰', default, '1978-1-1', '成都市一環路西二段17號', null);
insert into TbStudent (stuid, stuname, stubirth) values (1002, '郭靖', '1980-2-2');
insert into TbStudent (stuid, stuname, stusex, stubirth, stuaddr) values (1003, '黃蓉', 0, '1982-3-3', '成都市二環路南四段123號');
insert into TbStudent values (1004, '張無忌', 1, '1990-4-4', null, null);
insert into TbStudent values 
(1005, '丘處機', 1, '1983-5-5', '北京市海淀區寶盛北里西區28號', null),
(1006, '王處一', 1, '1985-6-6', '深圳市寶安區寶安大道5010號', null),
(1007, '劉處玄', 1, '1987-7-7', '鄭州市金水區緯五路21號', null),
(1008, '孫不二', 0, '1989-8-8', '武漢市光谷大道61號', null),
(1009, '平一指', 1, '1992-9-9', '西安市雁塔區高新六路52號', null),
(1010, '老不死', 1, '1993-10-10', '廣州市天河區元崗路310號', null),
(1011, '王大錘', 0, '1994-11-11', null, null),
(1012, '隔壁老王', 1, '1995-12-12', null, null),
(1013, '郭嘯天', 1, '1977-10-25', null, null);

– 刪除學生記錄

delete from TbStudent where stuid=1004;

– 更新學生記錄

update TbStudent set stubirth='1980-12-12', stuaddr='上海市寶山區同濟支路199號' where stuid=1002;

– 添加課程記錄

insert into TbCourse values 
(1111, 'C語言程序設計', 3, '大神級講師授課需要搶座'),
(2222, 'Java程序設計', 3, null),
(3333, '數據庫概論', 2, null),
(4444, '操作系統原理', 4, null);

– 添加學生選課記錄

insert into TbSC values 
(default, 1001, 1111, '2016-9-1', 95),
(default, 1002, 1111, '2016-9-1', 94),
(default, 1001, 2222, now(), null),
(default, 1001, 3333, '2017-3-1', 85),
(default, 1001, 4444, now(), null),
(default, 1002, 4444, now(), null),
(default, 1003, 2222, now(), null),
(default, 1003, 3333, now(), null),
(default, 1005, 2222, now(), null),
(default, 1006, 1111, now(), null),
(default, 1006, 2222, '2017-3-1', 80),
(default, 1006, 3333, now(), null),
(default, 1006, 4444, now(), null),
(default, 1007, 1111, '2016-9-1', null),
(default, 1007, 3333, now(), null),
(default, 1007, 4444, now(), null),
(default, 1008, 2222, now(), null),
(default, 1010, 1111, now(), null);

– 查詢所有學生信息

select * from TbStudent;

– 查詢所有課程名稱及學分(投影和別名)

select cosname as `課程名稱`, coscredit as `學分` from TbCourse;

– 查詢所有女學生的姓名和出生日期(篩選)

select stuname, stubirth from TbStudent where stusex=0;

– 查詢所有80後學生的姓名、性別和出生日期(篩選)

select stuname as `姓名`, if(stusex, '男', '女') as `性別`, stubirth as `出生日期`

from TbStudent where stubirth between ‘1980-1-1’ and ‘1989-12-31’;
– 查詢姓王的學生姓名和性別(模糊)

select stuname, stusex from TbStudent where stuname like '王%';

– 查詢姓郭名字總共兩個字的學生的姓名(模糊)

select stuname from TbStudent where stuname like '郭_';

– 查詢姓郭名字總共三個字的學生的姓名(模糊)

select stuname from TbStudent where stuname like '郭__';

– 查詢名字中有王字的學生的姓名(模糊)

select stuname from TbStudent where stuname like '%王%';

– 查詢沒有錄入家庭住址和照片的學生姓名(多條件篩選和空值處理)

select stuname from TbStudent where stuaddr is null and stuphoto is null;

– 查詢學生選課的所有日期(去重)

select distinct scdate from TbSC;

– 查詢學生的姓名和生日按年齡從大到小排列(排序)

select stuname, stubirth from TbStudent order by stubirth;

– 查詢所有錄入了家庭住址的男學生的姓名、出生日期和家庭住址按年齡從小到大排列(多條件篩選和排序)

select stuname, stubirth, stuaddr from TbStudent where stusex=1 and stuaddr is not null order by stubirth desc;

– 查詢年齡最大的學生的出生日期(聚合函數)

select min(stubirth) from TbStudent;

– 查詢年齡最小的學生的出生日期(聚合函數)

select max(stubirth) from TbStudent;

– 查詢男女學生的人數(分組和聚合函數)

select if(stusex, '男', '女') as `性別`, count(stusex) as `人數` from TbStudent group by stusex;

– 查詢課程編號爲1111的課程的平均成績(篩選和聚合函數)

select avg(score) as `平均成績` from TbSC where cid=1111;

– 查詢學號爲1001的學生所有課程的總成績(篩選和聚合函數)

select sum(score) as `總成績` from TbSC where sid=1001;

– 查詢每個學生的學號和平均成績, null值處理成0(分組和聚合函數)

select sid as `學號`, ifnull(avg(score), 0) as `平均成績` from TbSC group by sid;

– 查詢平均成績大於等於90分的學生的學號和平均成績

select sid as `學號`, avg(score) as `平均成績` from TbSC group by sid having avg(score)>=90;

– 查詢年齡最大的學生的姓名(子查詢)

select stuname from TbStudent where stubirth=(select min(stubirth) from TbStudent);

– 查詢選了兩門以上的課程的學生姓名(子查詢/分組條件/集合運算)

select stuname from TbStudent where stuid in 

(select sid from TbSC group by sid having count(sid)>2);
– 查詢選課學生的姓名和平均成績(子查詢和連接查詢)

-- 寫法1:
select stuname, avgscore from TbStudent t1 inner join
(select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2
on t1.stuid=t2.sid;
-- 寫法2: 
select stuname, avgscore from TbStudent t1,
(select sid, avg(score) as avgscore from TbSC where score is not null group by sid) t2
where t1.stuid=t2.sid;

– 查詢學生姓名、所選課程名稱和成績(連接查詢)

-- 寫法1:
select stuname, cosname, score from 
TbStudent t1, TbCourse t2, TbSC t3
where t1.stuid=t3.sid and t2.cosid=t3.cid and t3.score is not null;
-- 寫法2:
select stuname, cosname, score from TbStudent t1 inner join TbCourse t2
inner join (select sid, cid, score from TbSC where score is not null) t3 
on t1.stuid=t3.sid and t2.cosid=t3.cid;

– 查詢每個學生的姓名和選課數量(左外連接和子查詢)

select stuname as `姓名`, ifnull(coscount, 0) as `選課數` from TbStudent t1
left outer join (select sid, count(sid) as coscount from TbSC group by sid) t2 
on t1.stuid=t2.sid;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章