MySQL基础案例

创建数据库微信字符集gbk;
create database weixin character set gbk;
显示数据库;
show databases;
使用数据库
use weixin;
创建Student表格
create table Student(
-> Sno char(9) primary key,
-> Sname char(20),
-> Ssex char(2),
-> Sage smallint,
-> Sdept char(20)
-> );
插入信息
insert into Student values(‘201215121’,‘陈阳’,‘男’,‘20’,‘计算机’);
查询Student
select * from Student;
创建
create table Course(
-> Cno char(4) primary key,
-> Cname char(40) not null,
-> Cpno char(4),
-> Ccredit smallint
-> );
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)
-> );
根据查询条件查询表中对应的数据;
select Sname
-> from Student
-> where Sage>18;
授权查询信息给使用者
grant select
-> on table Student
-> to 陈阳;
建立计算机系的学生视图:把对应视图的select权限授予王平,把该视图上的所有操作权限授予张明
(1)Create view CS_Student
AS
Select * from Student
Where Sdept=’CS’;
(2)grant select
on CS_Student
to 王平;
(3)grant all privileges
On CS_Student
To 张明;
插入表格
insert
-> into Student(Sno,Sname,Ssex,Sdept,Sage)
-> values(‘20200327’,‘Alice’,‘fe’,‘IS’,‘18’);
省略顺序插入
mysql> insert
-> into Student
-> values(‘201813221’,‘木头’,‘女’,‘21’,‘计算机’);
在Student中对每一个系,求学生的平均年龄,并把结果存入数据库。
inset
-> into Dept_age
-> select Sdept,AVG(Sage)
-> from Student
-> group by Sdept;
插入多个元组
Inset
Into 表名
Values(‘’,’’), (‘’,’’), (‘’,’’), (‘’,’’);
更改学生的年龄主键为Sno
update Student
-> set Sage=33
-> where Sno=‘20200327’;
学生的年龄都加1;
update Student
-> set Sage=sage+1;
给计算机全体学生成绩清零
表:SC
grade=0;where sno=’ ’in
条件:IS
查询计算机学号 select Sno from Student where Sdept=”IS”;
Update SC
Set grade=0
Where Sno in
(select Sno from Student Where Sdept=”IS”);
数据操作:数据查询+数据更新
数据更新:insert update delete
Delete语法:
delete
from 表名
where
删除学号为20200316的信息
mysql> delete
-> from Student
-> where Sno=‘20200316’;
删除一个元组
删除多个元组
delete
from SC;
带子查询的删除
删除计算机系所有学生的选课记录;
mysql> delete
-> from SC
-> where Sno in
-> (select Sno from Student where Sdept=‘CS’);

你可能感兴趣的:(MySQL基础案例)