代码
drop database if exists emp;
Create database emp
default character set=gb2312
default collate=gb2312_chinese_ci;
use emp;
Create table dept(deptID char(9) primary key ,
Dname char(20) unique);
Create table employee(employee_ID int primary key auto_increment,
Ename char(20) not null,
Esex char(2) default '男' check (esex in ('男','女')),
Age smallint,
deptID char(9),
FOREIGN KEY (deptID)REFERENCES dept(deptID)
);
Create table student2(name char(20) unique,
sno char(9) primary key,
sex char(2)CHECK (sex in('男','女')),
birthday char(20),
birthplace char(20));
Insert into student2 values('刘晨','10003','女','1983-6-5','成都');
Insert into student2 values('李默','100001','女','','北京');
Insert into student2 values('程萌','100005','女','','重庆');
Insert into student2 values('王杰','100003','女','','天津');
Insert into student2 values('孙耿','100008','女','','');
update student2 set birthplace='重庆' where sno like '%001';
delete from student2 where sno like '%001';
select *from student2;
select name,birthday,birthplace from student2;
select name as '姓名',birthday as '生日',birthplace as '地址' from student2;
select name as '姓名',birthday as '生日',birthplace as '地址',2019-birthday as'年龄'
from student2;
select birthplace from student2;
select 2019-birthday as'年龄' from student2 where 2019-birthday>22;
select * from student2 where 22<=2019-birthday<=24;
select * from student2 where birthplace='成都';
select * from student2 where name like '李%';
select * from student2 where sno like '%[1,3,8]';
select * from student2 where name like'张%' and 2019-birthday>22;
select * from student2 where birthday is null;
select grade from sc where dept='软件工程';
create user '张三'@'localhost','李四'@'localhost','王五'@'localhost'identified by '123456';
SQL创建数据库表
DROP DATABASE IF EXISTS `s-t`;
CREATE DATABASE `s-t`
default character set=gb2312
default collate=gb2312_chinese_ci;
USE `s-t`;
CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE Course
(Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4),
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno)REFERENCES Course(Cno)
);
insert into student
values('201215121','李勇','男','20','CS');
insert into student
values('201215122','刘晨','女','19','CS');
insert into student
values('201215123','王敏','女','18','MA');
insert into student
values('201215125','张立','男','19','IS');
set foreign_key_checks=0;
insert into Course
values('1','数据库','5','4');
insert into Course
values('2','数学','','2');
insert into Course
values('3','信息系统','1','4');
insert into Course
values('4','操作系统','6','3');
insert into Course
values('5','数据结构','7','4');
insert into Course
values('6','数据处理','','2');
insert into Course
values('7','PASCAL语言','6','4');
insert into SC
values('201215121','1','92');
insert into SC
values('201215121','2','85');
insert into SC
values('201215121','3','88');
insert into SC
values('201215122','2','90');
insert into SC
values('201215122','3','80');
set foreign_key_checks=1;
SQL管理数据库表
USE st;
ALTER TABLE Student ADD S_entrance DATE;
ALTER TABLE Student modify COLUMN Sage INT;
ALTER TABLE Course ADD UNIQUE(Cname);
ALTER TABLE Course drop column ccredit;
ALTER TABLE Course modify COLUMN CNAME Char(50);
ALTER TABLE Course ADD column Nature char(4);
ALTER TABLE Course drop column Nature;
CREATE VIEW IS_Student
AS
SELECT Sno,Sname,Sage
FROM Student
WHERE Sdept='IS';
set foreign_key_checks=0;
DROP TABLE Student;
set foreign_key_checks=1;
单表查询
select *,grade+5
from SC;
select distinct Sdept
from STUDENT;
select SNO,CNO,grade,
case
when grade>=90 then '优秀'
when grade>=80 then '良'
when grade>=70 then '中'
when grade>=60 then '及格'
else '不及格'
end 等级
from SC;
select *
from Student
where SNAME like '李_';
select SNAME,ssex,sage
from Student
where Sno in('201215121','201215122','201215123');
select *
from Student
where SNAME not like '_立%';
select Sno
from SC
where Cno=2 and grade>=90;
select *
from Course
where cpno='';
select SSEx 性别,count(*) 每种性别人数
from Student
group by SSEX;
select Sno,avg(grade) 平均分
from SC group by Sno
having avg(grade)>=75;
select *, avg(grade)
from SC group by Sno
order by avg(grade) desc LIMIT 1;
select * from Student
where Sno in(
select Sno
from SC group by Sno
order by avg(grade) desc ) LIMIT 1;
连接查询
select SNAME,grade
from STUDENT,SC
where STUDENT.SNO=SC.SNO
and CNO in('1','2');
select SNAME,cname,grade
from STUDENT,SC,COURSE
where STUDENT.SNO=SC.SNO and COURSE.CNO=SC.CNO
and CNAME='信息系统' and grade>=85;
select *
from STUDENT,SC,COURSE
where STUDENT.SNO=SC.SNO and COURSE.CNO=SC.CNO
and SNAME='李勇';
select *
from STUDENT,SC
where STUDENT.SNO=SC.SNO and STUDENT.sdept='cs';
select *
from STUDENT,SC,COURSE
where STUDENT.SNO=SC.SNO and COURSE.CNO=SC.CNO
and STUDENT.ssex='女';
select *
from STUDENT left join SC on STUDENT.SNO=SC.SNO;
select *
from SC right join Course on SC.CNO=Course.CNO;
select *
from Course where CNO not in (select cno from sc group by CNO);
子查询
select SNO,cno
from SC
where CNO in('2','3');
select * from STUDENT
where SNO in(
select SNO
from SC
where CNO in('2','3'));
SELECT Sno,Sname FROM Student
WHERE Sno IN
(SELECT Sno FROM SC
WHERE Cno IN
(SELECT Cno FROM Course WHERE Cpno= '5' ) );
SELECT Cno,cname FROM Course
WHERE ccredit>(SELECT AVG(ccredit) FROM Course);
select SNAME,SSEX,sdept from STUDENT
where SNO in(
select SNO
from SC
where CNO ='3' and grade between 80 and 90);
select *
from STUDENT
where SNO in(
select SNO from SC
where CNO in(
select CNO from Course
where CNAME='数据库'));
select * from SC
where sno in(
select sno from STUDENT
where sdept in(
select sdept from STUDENT
where SNAME='李勇')
and sname<>'李勇');
#and sname<>'李勇'要也可以 不要也可以
select sno,grade+5,grade from SC
where SNO in(
select SNO
from STUDENT
where SNAME='李勇');
select sno,grade from SC
where grade>90
and SNO in(
select SNO from STUDENT
where sdept='CS');
SELECT Sname,Sdept FROM Student
WHERE sno in (select sno from sc where grade > ALL(SELECT grade FROM Sc WHERE Sno= '201215122')
and Sno<> '201215122');
select Sname from student
where Sno IN(select Sno from SC
group by Sno having count(*)>(select count(*) from course where ccredit='3'));
更新数据
INSERT INTO Student VALUES ('201215126','王丽丽',null, null, null);
INSERT INTO course VALUES ('8','计算机网络', null, null);
INSERT INTO SC VALUES ('201215126','8',89);
INSERT INTO Student VALUES ('201215127','王序','男','19','is');
CREATE TABLE S_AVG
(Sno CHAR(9) PRIMARY KEY,
AVG_GRADE int);
INSERT INTO S_AVG ( Sno, AVG_GRADE)
SELECT Student.Sno, AVG(GRADE)
FROM Student, SC
WHERE Student.Sno=SC.Sno
AND SSEX ='女'
GROUP BY SC.Sno
HAVING AVG(GRADE)>=80;
CREATE TABLE S_AVG (Sno CHAR(9) PRIMARY KEY, AVG_GRADE int);
INSERT INTO S_AVG ( Sno, AVG_GRADE)
SELECT Student.Sno, AVG(GRADE)
FROM Student,sc
WHERE SSEX ='女' and Student.sno in(select Student.sno from sc
GROUP BY SC.Sno
HAVING AVG(GRADE)>=80);
UPDATE Student
SET Sname='王小丽'
WHERE Sname='王丽丽';
UPDATE SC
SET grade=85
WHERE grade<=80
AND Cno IN ( SELECT Cno
FROM Course
WHERE cname='信息系统');
UPDATE SC,course
SET grade=85
WHERE grade<=80 and course.cname='信息系统' and Course.cno=sc.cno;
select avg(grade) from sc where Cno IN
( SELECT Cno FROM course WHERE cname='数学') INTO @avgs ;
update SC set GRADE = @avgs where GRADE < @avgs and Cno IN
( SELECT Cno FROM course WHERE cname='数学');
DELETE FROM Student
WHERE Sno ='201215127';
DELETE FROM SC WHERE sno in (SELECT sno FROM Student WHERE SNAME='李勇');
set foreign_key_checks=0;
DELETE FROM Student
WHERE sdept ='cs';
UPDATE Sc
SET grade = NULL
WHERE Sno='201215122';
SELECT Sno
FROM SC
WHERE Grade < 60 AND Cno='2'
UNION
SELECT Sno
FROM SC
WHERE Grade IS NULL AND Cno='2';
视图操作
CREATE VIEW A_GRADE(Sno,SNAME,AVG_GRADE)
AS SELECT Student.Sno,SNAME,AVG(GRADE)
FROM Student,SC
WHERE Student.Sno = SC.Sno
GROUP BY Sc.Sno;
CREATE VIEW GRADE_T(Sno,Sname,cno,cname,ccredit,GRADE)
AS
SELECT Student.Sno,Sname,sc.cno,cname,ccredit,GRADE
FROM Student,sc,course
WHERE Student.Sno=sc.Sno and sc.cno= course.cno ;
CREATE VIEW S_SC(SNO,sname,cno,grade)
AS
SELECT STUDENT.SNO,sname,cno,grade
FROM STUDENT,SC
WHERE STUDENT.SNO = SC.sno and ssex = '男' AND grade>=85
ORDER BY SC.grade DESC;
SELECT sno,sname,AVG_GRADE
FROM A_GRADE
WHERE AVG_GRADE>=85;
CREATE VIEW CS_Student
AS
SELECT Sno,Sname,Ssex,Sage
FROM Student
WHERE Sdept= 'cs';
UPDATE CS_Student
SET Sname= '艾学习'
WHERE Sno= '201215122';
INSERT
INTO CS_Student
VALUES('201215128','尤所成',null,19);
set foreign_key_checks=0;
DELETE
FROM CS_Student
WHERE Sno= '201215122';
set foreign_key_checks=1;
完整性
DROP DATABASE IF EXISTS `s-t`;
CREATE DATABASE `s-t`
default character set=gb2312
default collate=gb2312_chinese_ci;
USE `s-t`;
CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex enum('男','女'),
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE Course
(Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40) UNIQUE not null,
Cpno CHAR(4),
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT DEFAULT 0,
PRIMARY KEY (Sno,Cno),
CONSTRAINT fk_sno FOREIGN KEY (sno) REFERENCES student(sno),
CONSTRAINT fk_cno FOREIGN KEY (cno) REFERENCES course(cno)
);
ALTER TABLE sc DROP FOREIGN KEY fk_sno;
ALTER TABLE sc DROP FOREIGN KEY fk_cno;
ALTER TABLE SC ADD CONSTRAINT fk_sno FOREIGN KEY(sno) REFERENCES student(sno);
ALTER TABLE SC ADD CONSTRAINT fk_cno FOREIGN KEY(cno) REFERENCES course(cno);
insert into student
values('201215121','李勇','男','20','CS');
insert into student
values('201215122','刘晨','女','19','CS');
insert into student
values('201215123','王敏','女','18','MA');
insert into student
values('201215125','张立','男','19','IS');
set foreign_key_checks=0;
insert into Course
values('1','数据库','5','4');
insert into Course
values('2','数学','','2');
insert into Course
values('3','信息系统','1','4');
insert into Course
values('4','操作系统','6','3');
insert into Course
values('5','数据结构','7','4');
insert into Course
values('6','数据处理','','2');
insert into Course
values('7','PASCAL语言','6','4');
insert into SC
values('201215121','1','92');
insert into SC
values('201215121','2','85');
insert into SC
values('201215121','3','88');
insert into SC
values('201215122','2','90');
insert into SC
values('201215122','3','80');
set foreign_key_checks=1;
安全性
CREATE USER 'user1'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'user2'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'user3'@'localhost' IDENTIFIED BY '123456';
INSERT INTO mysql.user(HOST,USER,authentication_string,ssl_cipher,x509_issuer,x509_subject)
VALUES('localhost','user4',md5('123456'),'','','');
GRANT ALL PRIVILEGES ON *.* TO 'user5'@'localhost' IDENTIFIED BY'123456';
GRANT ALL PRIVILEGES ON `s-t`.* TO 'user1'@'localhost';
GRANT select,create,drop on `s-t`.student TO 'user2'@'localhost';
GRANT select,update(sname) on `s-t`.student TO 'user3'@'localhost';
revoke ALL PRIVILEGES ON `s-t`.* from 'user1'@'localhost';
revoke update(sname) on `s-t`.student from 'user3'@'localhost';
触发器
create trigger trigger_limitSex
before insert on student for each row
begin
if NEW.sdept='cs' or NEW.sdept='is' or NEW.sdept='ma'
then
set @mesg='更改成功';
else
set @mesg='更改不成功';
insert INTO xxxx value(1);
end if;
End;
create trigger trigger_limitcno
before insert on course for each row
begin
if NEW.cno regexp '[0-9]{4}'
then set @mesg='插入成功';
else
set @mesg='插入不成功';
insert INTO xxxx value(1);
end if;
End;
create trigger trigger_limitage
BEFORE UPDATE ON student FOR EACH ROW
BEGIN
if NEW.sage<15 or NEW.sage>45
then set @mesg='修改不成功';
insert INTO xxxx value(1);
else
set @mesg='修改成功';
end if;
END;
create trigger trigger_delete
before delete on course for each row
begin
SET @oldcno=old.cno;
DELETE FROM sc WHERE sno=@oldcno;
END;