SQL笔记01

建表

create database stuinfo
use stuinfo
--major
--mno mname
create table major(
	mno int ,
	mname varchar(20) ,
	primary key(mno)
)
select * from major

--stu
--sno sname age sex mno
create table stu(
	sno int,
	sname varchar(30),
	age smallint,
	sex bit,
	mno int,
	primary key(sno),
	foreign key(mno) references major(mno)
)
select * from stu;

--cou
--cno cname ctime ccredit
create table cou(
	cno int,
	cname varchar(30),
	ctime smallint,
	ccredit decimal(4,2),
	primary key(cno)
)

--sc
--sno cno grade
create table sc(
	sno int,
	cno int,
	grade decimal(5,2),
	primary key(sno,cno),
	foreign key(sno) references stu(sno)
)
--对sc表加一个外键
alter table sc add constraint fk_sc foreign key(cno) references cou(cno)

增、删


use stuinfo
--添加属性add
alter table stu add qq varchar(20)
--删除属性drop column
alter table stu drop column qq
select * from stu;

create table t(
	tt int
)
select * from t
--删除表
drop table t

--插入信息insert
--1计算机科学与技术
--2软件工程
select * from major
insert into major(mno,mname) values(1,'计算机科学与技术')
insert into major(mno,mname) values(2,'软件工程')
select * from stu
insert into stu values(1,'kk',20,0,1)
insert into stu values(2,'kk',20,0,null)

--删除delete
delete from stu where sno=1
select * from stu
--删除一个专业
delete from major where mno=1
--别的表有外键的先置null update
update stu set mno=null where mno=1
update stu set sname='康康' where sno=1

重新建表,插入数据

drop table sc;
drop table stu;
drop table cou;
create table stu(
sno varchar(13) primary key,
sname varchar(30) not null,
age int ,
sex bit,
mno int,
foreign key(mno) references major(mno)
)
create table cou(
cno varchar(13) primary key,
cname varchar(30) not null,
ctime int,
ccredit decimal(5,2)
)
create table sc(
cno varchar(13),
sno varchar(13),
grade decimal(5,2),
primary key(cno,sno),
foreign key(cno) references cou(cno),
foreign key(sno) references stu(sno),
)
--插入数据
select * from major
insert into major(mno,mname) values(1,'计算机科学与技术');
insert into major(mno,mname) values(2,'软件工程');
insert into major(mno,mname) values(3,'网络工程');
insert into major(mno,mname) values(4,'智能科学与技术');
select * from stu
insert into stu values('2020001','小一',18,0,1);
insert into stu values('2020002','小二',18,1,1);
insert into stu values('2020003','小四',18,1,1);
insert into stu values('2020004','小五',18,1,1);
insert into stu values('2020005','小六',18,0,2);
insert into stu values('2020006','小七',18,1,2);
insert into stu values('2020007','小八',18,0,2);
insert into stu values('2020008','小九',18,1,2);
insert into stu values('2020009','小十',19,0,3);
insert into stu values('20200010','小十',20,0,3);
insert into stu values('20200011','小快',19,0,3);
insert into stu values('20200012','小冬',21,0,3);
insert into stu values('20200013','小宇',19,0,null);
insert into stu values('20200014','小点',19,0,4);
insert into stu values('20200015','彭杰',21,0,4);
insert into stu values('20200016','彭小杰',21,0,4);
select * from  cou
insert into cou values('20201','C语言',32,5);
insert into cou values('20202','C#',32,3);
insert into cou values('20203','数据结构',16,5);
insert into cou values('20204','大学英语1',32,3.5);
insert into cou values('20205','大学英语2',32,3.5);
insert into cou values('20206','大学英语3',32,3.5);
insert into cou values('20207','大学英语4',32,3.5);
select * from sc
insert into sc(sno,cno,grade) values('2020006','20201',null);
insert into sc(sno,cno,grade) values('2020005','20201',null);
insert into sc(sno,cno,grade) values('2020001','20201',90);
insert into sc(sno,cno,grade) values('2020002','20201',90);
insert into sc(sno,cno,grade) values('2020003','20201',90);
insert into sc(sno,cno,grade) values('2020004','20201',58);
insert into sc(sno,cno,grade) values('2020004','20202',98);
insert into sc(sno,cno,grade) values('2020004','20203',90);
insert into sc(sno,cno,grade) values('2020005','20203',90);

你可能感兴趣的:(sql,数据库,mysql)