数据库学习与练习笔记(MySql )
SQL标准语法
SQL练习
创建
标记简介:
[]: 可选项
<>:标识符
表
Create Table <表名
>(
<列名
><数据类型
>[列级完整性约束条件
]
[,<列名
><数据类型
>[列级完整性约束条件
]]
...
[,<表级完整性约束条件
>]
);
触发器
Create Trigger <触发器名
>
{Before
|After}
<触发事件
> on<表名
>
Referencing new
|old
row as<变量
>
for each{
row|statement}
[when <触发条件
>]<触发动作
>
索引
Create [Unique][Cluster
] Index <索引名
>
On <表名
>(
<列名
>[<次序
>]
[,<列名
>[<次序
>]]
...
);
显示
模式/数据库
show databases
表
show tables
删除
表
Drop Table <表名
> [Restrict|Cascade]
索引
Drop Index <索引名
>
Drop Index <索引名
> On <表名
>
修改
Alter Table <表名
>
[Add [Column]<新列名
><数据类型
>[完整约束条件
]]
[Add <表完整性约束条件
>]
[Drop [Column]<列名
>[Cascade|Restrict]]
[Drop Constraint<完整性约束名
>[Cascade|Restrict]]
[Alter|Modify Column <类名
><数据类型
>]
Alter Table <表明
> Modify Colum
<属性名
> <数据类型
>
查询
Select [All|Distinct] <目标表达式
>[,<目标表达式
>]···
From <表名或视图
>[别名
][,<表名或视图名
>[别名
]···
]| (<Select 语句
>)[As]<别名
>
[Where <条件表达式
>]
[Group By<列名
1>[Having <条件表达式
>]]
[Order By<列名
2>[Asc|Desc]]
插入数据
Insert Into <表名
>[(<属性列
1>[,<属性列
2>]...)]Values(<常量
1>[,<常量
2>]...)
插入子查询数据
Insert Into <表名
> [<属性列
1>[,<属性列
2>]] 子查询
修改数据
Update <表名
> Set <列名
>=<表达式
>[,<列名
>=<表达式
>]...
[Where <表达式
>]
删除数据
Delete From <表名
>
[Where <条件
>]
创建视图
Create View <视图名
>[(<列名
>[,列名
]...)]
As<子查询
>
[With Check Option]
删除视图
Drop View <视图名
>[Cascade]
创建用户
Create User <用户名
>@
<主机IP
> Identified
by<密码
>
显示用户权限
show grants
for <用户名
>@
<主机IP
>
授权
Grant <权限
>[,<权限
>]...
On <对象类型
><对象名
>[,<对象类型
><对象名
>]...
To<用户
>[,<用户
>]...
[With Grant Option]
Grant <权限
>[,<权限
>]
On <数据库名
>.<表名
>[,<数据库名
>.<表名
>]
To <用户名
>@
<主机名
>,[<用户名
>@
<主机IP
>]
收回
Revoke <权限
>[,<权限
>]...
On <对象类型
><对象名
>[,<对象类型
><对象名
>]...
From <用户
>[,<用户
>]...
[Cascade|Restrict]
Revoke <权限
>[,<权限
>]...
On <数据库名
>.<表名
>[,<数据库名
>.<表名
>]
From <用户名
>@
<主机IP>
[,<用户名
>@
<主机IP
>]
创建数据库模式的权限
Create User <用户名
>[with][DBA
|RESOURCE
|CONNECT]
角色创建
create Role
<角色名
>
角色授权
Grant <权限
>[,<权限
>]...
On <对象类型
>对象名
TO <对象
>[,<对象
>]...
角色授予其他角色和用户
Grant <角色
1>[,<角色
2>]...
TO <角色
3>[,<用户
>]...
[With Admin
Option]
角色权限的收回
Revoke <权限
>[,<权限
>]...
On <对象类型
><对象名
>
From <角色
>[,<角色
>]...
create schema DBTEST
create database DBTEST
show databases
use DBTEST
Create Table Student
(
Sno
Char(9) Primary Key,
Sname
Char(20) Unique,
Ssex
Char(2),
Sage
Smallint,
Sdept
char(20)
)Default Charset=utf8
;
Create Table Course
(
Cno
Char(4) Primary key,
Cname
Char(40) Not Null,
Cpno
Char(4),
Ccredit
Smallint,
Foreign key (Cpno
) References Course
(Cno
)
)Default Charset=utf8
;
Create Table SC
(
Sno
Char(9),
Cno
Char(4),
Grade
Smallint,
Primary key(Sno
,Cno
),
Foreign key(Cno
) References Course
(Cno
),
Foreign key(Sno
) References Student
(Sno
)
)Default Charset=utf8
;
Alter Table Student
Add S_entrance
Date
Alter Table Student
Modify Colum Sage
Int
Alter Table Course
Add Unique(Cname
)
Create Unique Index Stusno
On Student
(Sno
)
Create Unique Index Coucno
On Course
(Cno
)
Create Unique Index SCno
On SC
(Sno
ASC,Cno
DESC)
Drop Index Stusno
on Student
Drop Table Student
Restrict
Drop Table Student
Cascade
drop schema DBTEST
drop database DBTEST
Select Sno
,Sname
From Student
Select Sname
,Sno
,Sdept
From Student
Select Sno
,Sname
,Ssex
,Sage
,Sdept
From Student
Select * From Student
Select Sname
,2020-Sage
From Student
Select Sname
,'Year of BirthDay',2020-Sage
,Lower
(Sdept
) From Student
Select Sname Name
,'Year of BirthDay:' Birth
,2020-Sage Birthday
,Lower
(Sdept
) Department
From Student
Select Sno
From SC
Select All Sno
From SC
Select Distinct Sno
From SC
Select Sname
From Student
where Sdept
= 'CS'
Select Sname
,Sage
From Student
Where Sage
< 20
Select Distinct Sno
From SC
Where Grade
< 60
Select Sname
,Sdept
,Sage
From Student
Where Sage
Between 20 And 23
Select Sname
,Sdept
,Sage
From Student
Where Sage
Not Between 20 And 24
Select Sname
,Ssex
From Student
Where Sdept
NOT In ('CS','MA','IS')
Select * From Student
Where Sno
Like '20121515121'
Select * From Student
Where Sname
Like '张%'
Select * From Student
Where Sname
Like '张_'
Select Sname
,Sno
,Ssex
From Student
Where Sname
Not Like '刘%'
Select Cno
,Ccredit
From Course
Where Cname
Like 'DB/_Design' Escape '/'
Select * From Course
Where Cname
Like 'DB/_%i__' Escape '/'
Select Sno
,Cno
From SC
Where Grade
IS Null
Select Sno
,Cno
From SC
Where Grade
IS Not Null
Select Sname
From Student
Where Sdept
='CS' And Sage
<20
Select Sname
,Ssex
From Student
Where Sdept
='CS' Or Sdept
='MA' Or Sdept
= 'IS'
Select Sno
,Grade
From SC
Where Cno
= '3' Order By Grade
Desc
Select * From Student
Order By Sdept
,Sage
Desc
Select Count(*) From Student
Select AVG(Grade
) From SC
where Cno
= '1'
Select Max(Grade
) From SC
where Cno
= '1'
Select Sum(Ccredit
) From Sc
,Course
Where Sno
=' 201813137031' And SC
.Cno
=Course
.Cno
Select Cno
,Count(Sno
) From Sc
Group By Cno
Select Sno
From SC
Group By Sno
Having Count(*)>3
Select Sno
,Avg(Grade
) From Sc
Group By Sno
Having Avg(Grade
)>90
Select Student
.*,SC
.* From Student
,SC
Where Student
.Sno
= Sc
.Sno
Select Student
.Sno
,Sname
,Ssex
,Sage
,Sdept
,Cno
,Grade
From Student
,SC
where Student
.Sno
= SC
.Sno
Select Student
.Sno
,Sname
From Student
,SC
Where Student
.Sno
= SC
.Sno
And SC
.Cno
= '2' And SC
.Grade
> '90'
Select First.Cno
,Second.Cpno
From Course
First,Course
Second where First.Cpno
= Second.Cno
Select Student
.Sno
,Sname
,Ssex
,Sage
,Sdept
,Cno
,Grade
From Student
Left Outer Join SC
On (Student
.Sno
=SC
.Sno
)
Select Student
.Sno
,Sname
,Cname
,Grade
From Student
,SC
,Course
Where Student
.Sno
= SC
.Sno
And SC
.Cno
= Course
.Cno
Select Sname
From Student
Where Sno
IN (Select Sno
From SC
Where Cno
= '2')
Select Sno
,Sname
,Sdept
From Student
Where Sdept
In (Select Sdept
From Student
Where Sname
='刘晨')
Select Sno
,Sname
,Sdept
From Student
Where Sdept
= (Select Sdept
From Student
Where Sname
='刘晨')
Select Sno
,Sname
,Sdept
From Student
Where Sdept
In ('CS')
Select S1
.Sno
,S1
.Sname
,S1
.Sdept
From Student S1
,Student S2
Where S1
.Sdept
=S2
.Sdept
And S2
.Sname
='刘晨'
Select Sno
,Sname
From Student
Where Sno
In (Select Sno
From SC
Where Cno
In (Select Cno
From Course
Where Cname
='信息系统'))
Select Student
.Sno
,Sname
From Student
,SC
,Course
Where Student
.Sno
=SC
.Sno
And SC
.Cno
= Course
.Cno
And Course
.Cname
='信息系统'
Select Sno
,Cno
From SC O
Where Grade
>= (Select Avg(Grade
) From SC T
Where T
.Sno
=O
.Sno
)
Select Sname
,Sage
From Student
where Sage
<Any (Select Sage
From Student
Where Sdept
= 'CS ') And Sdept
<> 'CS '
Select Sname
,Sage
From Student
Where Sage
< (Select Max(Sage
) From Student
Where Sdept
='CS ') And Sdept
<> 'CS'
Select Sname
,Sage
From Student
Where Sage
<All (Select Sage
From Student
Where Sdept
= 'CS') And Sdept
<> 'CS'
Select Sname
,Sage
From Student
Where Sage
< (Select Min(Sage
) From Student
Where Sdept
= 'CS ') And Sdept
<> 'CS'
Select Sname
From Student
Where Exists (Select * From SC
Where Sno
= Student
.Sno
And Cno
= '1')
Select Sname
From Student
,SC
Where Student
.Sno
=SC
.Sno
And Cno
= '1'
Select Sname
From Student
Where Not Exists (Select * From SC Wheres Sno
=Student
.Sno
And Cno
= '1')
Select Sno
,Sname
,Sdept
From Student S1
Where Exists (Select * From Student S2
Where S1
.Sdept
= S2
.Sdept
And S2
.Sname
= '刘晨')
Select Sname
From Student
Where Not Exists(Select * From Course
where Not Exists (Select * From SC
Where SC
.Sno
=Student
.Sno
And Cno
= Course
.Cno
))
Select Distinct Sno
From SC SCX
Where Not Exists (Select * From SC SCY
Where SCY
.Sno
='201515122' And Not Exists (Select * From SC SCZ
Where SCZ
.Sno
= SCX
.Sno
And SCZ
.Cno
= SCY
.Cno
))
Select * From Student
Where Sdept
= 'CS' Union Select * From Student
Where Sage
<= 19
Select Snon
From SC
where Cno
= '1' Union Select Sno
From SC
Where Cno
= '2'
Select Sno
From SC
Where Cno
='1' And Sno
In (Select Sno
From SC
Where Cno
= '2')
Select * From Student
Where Sdept
= 'CS' And Sage
<=19
Select * From Student
Where Sdept
= 'CS' And Sage
>19
Select SC
.Sno
,Cno
From SC
,(Select Sno
,Avg(Grade
) avg_grade
From SC
Group By Sno
) As Avg_sc
Where SC
.Sno
= Avg_sc
.Sno
And Avg_sc
.avg_grade
Select Sname
From Student
,(Select Sno
From SC
Where Cno
= '1')As SC1
Where Student
.Sno
= SC1
.Sno
Insert Into Student
(Sno
,Sname
, Ssex
,Sdept
,Sage
)Values ('20121528','陈东','男','IS',18)
Insert Into Student
Values('20202','张成名','M',18,'CS')
Insert Into SC
(Sno
,Cno
)Values('20202','1')
Insert Into SC
Values ('20121528', '1',NULL)
Create Table Dept_age
(
Sdept
Char(15).
Avg_avg
Smallint
);
Insert Into Dept_age
(Sdept
,Avg_age
) Select Sdept
,AVG(Sage
) From Student
Group By Sdept
Update Student
Set Sage
= 22 Where Sno
= '20121515'
Update Student
Set Sage
=Sage
+1
Update SC
Set Grade
=0 Where Sno
In (Select Sno
From Student
Where Sdept
='CS')
Delete From Student
Where Sno
= '20202'
Delete From SC
Delete From SC
Where Sno
In (Select Sno
From Student
Where Sdept
= 'CS')
Insert Into Sc
(Sno
,Cno
,Grade
) Values ('2020','1',NULL )
Insert Into SC
(Sno
,Cno
) Values ('2020','1')
Update Student
Set Sdept
= NULL Where Sno
= '2020'
Select * From Student
Where Sname
IS Null OR Ssex
Is Null OR Sage
Is Null OR Sdept
Is Null
Select Sno
From SC
Where Grade
< 60 And Cno
= '1'
Select Sno
From SC
Where Grade
<60 And Cno
= '1' Union Select Sno
From SC
Where Grade
Is Null ANd Cno
= '1'
Select Sno
From SC
Where Cno
= '1' And (Grade
< 60 OR Grade
IS Null)
Create View IS_Student
As Select Sno
,Sname
,Sage
From Student
Where Sdept
= 'IS'
Create View Is_Student
AS Select Sno
,Sname
,Sage
From Student
Where Sdept
= 'IS' With Check Option
Create view IS_SI
(Sno
,Sname
,Grade
) As Select Student
.Sno
,Sname
,Grade
From Student
,SC
Where Student
.Sno
= Sc
.Sno
And SC
.Cno
='1'
Create View IS_S2
AS Select Sno
,Sname
,Grade
From IS_SI
Where Grade
>= 90
Create View BT_S
(SNo
,Sname
,Sbirth
) As Select Sno
,Sname
,2014-Sage
From Student
Create View S_G
(Sno
,Gavg
) As Select Sno
,Avg(Grade
) From SC
Group By Sno
Create View F_Student
(F_Sno
,name
,sex
,age
,dept
) As Select * From Student
Where Ssec
= 'W'
Drop View IS_SI
Cascade
Select Sno
,Sage
From IS_Student
Where Sage
<20
Select IS_Student
.Sno
,Sage
,Sname
From IS_Student
,SC
Where IS_Student
.Sno
= SC
.Sno
And SC
.Sno
='1'
Select * From S_G
Where Gavg
>=90
Select Sno
,Avg(Grade
) From SC
Group By Sno
Having Avg(Grade
)>90
Select * From (Select Sno
,Avg(Grade
) Gavg
From SC
Group By Sno
)As S_G
Where Gavg
>=90
Update Is_student
Set Sname
='HHH' Where Sno
='1'
Insert Is_Student
Values ('2022','sdfsdfasdfasdf',20)
Delete From IS_Student
Where Sno
= '2022'
Create User 'U1'@'localhost
' Identified by '123'
Grant Select On dbtest.student to 'U1
'@'localhost
'
Grant All Privileges On dbtest.student to 'U2
'@'localhost
','U3
'@'localhost
'
Grant All Privileges On dbtest.course to 'U2
''localhost
','U3
'@'localhost
'
Grant Update(Sno),select on dbtest.student to 'U4
'@'localhost
'
Grant Insert on dbtest.sc to U3@'localhost
' with Grant option
Revoke Update(Sno) On dbtest.Student from 'U4
'@'localhost
'
Revoke Insert On dbtest.Sc from 'U5
'@'localhost
'
Revoke Insert On dbtest.Sc from 'U6
'@'localhost
'
--完整性约束条件设置
create Table Student
(
Sno Char(9)Primary Key, /**列级完整性*/
Sname Char(20) Not Null,
Ssex Char(2),
Sage SmallInt,
Sdept Char(20)
);
Create Table Student
(
Sno Char(9),
Sname Char(20)Not null,
Ssex Char(2),
Sage SmallInt,
Sdept Char(20)
Primary key(Sno) /**列级完整性*/
);
Create Table Sc
(
Sno Char(9) not Null,
Cno Char(4) not null,
Grade Smallint,
Primary key(Sno,Cno)/**只能在表级定义主码*/
);
Create Table Sc
(
Sno Char(9) Not null,
Cno Char(4)Not Null ,
Grade Smallint,
primary key(sno,cno), /**表级定义实体完整性*/
Foreign key(Sno) References Student(sno), /**表级定义参照完整性*/
Foreign key(Cno) References Course(Cno) /**表级定义参照完整性*/
);
--显示说明数据库违反参照完整性约束条件时应对策略
Create Table SC
(
Sno Char(9),
Cno Char(4),
Grade SmallInt,
primary Key(Sno,Cno),
Foreign key(sno) references Student(sno)on delete cascade on update cascade,
Foreign Key(Cno) references Course(Cno) on delete no action on update cascade
);
create table sc
(
sno char(9) not null,/**sno属性不允许空值*/
cno char(4) not null,/**Cno属性不允许空值*/
Grade smallint not null,/**Grade属性不允许空值*/
primary key(sno,cno)/** 表级定义实体完整性时,暗含sno,cno 属性不允许为空值,
在列级不允许取空值的定义可不写*/
...
);
create table Dept
(
Deptno numeric(2),
Dname char(9)unique not null,
Location char(10),
primary key(Deptno)
);
/**Mysql 不支持check用法
create table student(
sno char(9) primary key,
sname char(9) not null,
ssex char(2) check (ssex in '男
','女
'),
sage smallint,
sdept char(20)
);
create table sc(
sno char(9),
cno char(4),
Grade smallint check(Grade >=0 and Grade <=100),
primary key(sno,cno),
foreign key (sno) references student(sno),
foreign key(cno) references course(cno)
);
create table student (
sno char(9),
sname char(8)not null,
ssex char(2),
sage smallint,
sdept char(20),
primary key(sno),
check(ssex='女
' OR sname not like 'Ms
.%')
)
create table student(
sno numeric(6),
constraint C1 check (sno between 9000000 and 99999999),
sname char(20),
constraint c2 not null,
sage numeric(3),
constraint c3 check(sage<20),
ssex char(2),
constraint c4 check(ssex in ('男
','女'
)),
constraint sudentkey
primary key(sno
)
);
create table teacher
(
Eno
Numeric(4)primary key,
Ename
char(10),
Job
char(8),
Sal
numeric(7,2),
Deptno
numeric(2),
Constraint Teacherkey
foreign key(Deptno
)
References Dept
(deptno
),
constraint c1
check(sal
+deduct
>=3000)
);
Alter table student
drop constraint c4
;
Alter table student
drop constraint c1
check(sno
between 900000 and 999999);
Alter table student student
drop constraint c3
;
Alter table stident
add constraint c3
check(sage
<40);
*/
转载请注明原文地址:https://blackberry.8miu.com/read-37717.html