数据库复习

1.表的创建
格式

create table 表名
(
	属性名 类型   Primary Key,
	
);
如何加主码
Primary Key(属性名,属性名),

案例

create table Student
(
    Sno	char(10) Primary Key,
    Sname	varchar(20),	
    Ssex	char(2)	,
    Sage	smallint,
    Sdept	varchar(20),
);

2.添加与修改表的列

Alter table 表名 Add 属性名 属性类型;
Alter table 表名 drop Column 属性名;

案例

-- ********** 此处写“1、添加phone列”的SQL语句 ********** --
Alter table Student Add phone char(12);
-- ********** 此处写“2、删除Cpno列”的SQL语句 ********** --
Alter table Course drop Column Cpno;

3.删除表

Drop table 表名;

案例

Drop table SC;

4.表中插入数据
按顺序输入,没有就null

insert into 表名 values('  ',' ',);

案例

insert into Student values('002','Ketty','f',19,'MA');

4.表中修改数据

Update 表名
set 属性名怎么变
Where 条件

案列

-- ********** 此处写“1、将不及格的学生成绩加5分”的SQL语句 ********** --
Update sc
set Grade = Grade+5
Where Grade<60;
-- ********** 此处写“2、将CS系男同学的年龄加1”的SQL语句 ********** --
Update Student
set Sage = Sage+1
Where Ssex = 'm' and Sdept = 'CS';
-- ********** 此处写“3、将学生的学号前加上‘S’(其中S要大写)”的SQL语句 ********** --

Update Student
set Sno = 'S' + Sno;
-- ********** 此处写“1、将学生的学号前的‘S’删掉”的SQL语句 ********** --
Update Student
set sno = substring(sno,2,4);
-- ********** 此处写“2、在学生学号的后面加上‘S’”的SQL语句 ********** --
Update Student
set sno =substring(sno,1,4)+'S';

5.删除表中数据

Delete 
From 表名
Where 条件;

案列

-- ********** 此处写“1、在SC表中删除成绩为空的选课信息”的SQL语句 ********** --
Delete 
From sc
Where grade is null;
-- ********** 此处写“2、删除年龄等于18岁的女同学”的SQL语句 ********** --

Delete 
From Student
Where ssex = 'f' and sage = 18;
-- ********** 此处写“2、删除年龄等于18岁的女同学”的SQL语句 ********** --

-- ********** 此处写“3、删除学分为3分的课程”的SQL语句 ********** --
Delete 
From course
Where ccredit=3;

5.查询表中数据简单查询

select 属性名
from 表名
where 条件;
-- ********** 此处写“1、查询CS系男同学的学号,姓名,年龄”的SQL语句 ********** --
select sno,sname,sage
from Student
where ssex = 'M' and sdept = 'CS';
-- ********** 此处写“3、查询先行课程不为空的课程(使用*表示查询结果)”的SQL语句 ********** --
select *	
from Course
where Cpno is not null;
-- ********** 此处写“4、查询姓名中带有'n'字母的学生的学号,姓名(使用like语句)”的SQL语句 ********** --
select sno,sname
from Student
where sname  like '%n%';
-- ********** 此处写“5、使用distinct关键字查询学生表中不同的系,列出系(去除重复元祖)”的SQL语句 ********** --
select distinct sdept
from Student
-- ********** 此处写“2、查询没有选C06(课程号)课程的同学的学号,姓名,性别”的SQL语句 ********** --
select sno,sname,ssex from student
    where sno not in (select sno from sc where cno='C06')
-- ********** 此处写“3、查询成绩最高的选课信息,列出学号,课程号和成绩”的SQL语句 ********** --
select Student.sno,SC.cno,SC.grade
from Student,SC,Course
where  Student.sno=SC.sno and Course.cno = SC.cno and SC.grade in 
(
    select max(SC.grade)
    from SC
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值