目录
7、删除所有元组(表中所有行的数据)(delete from)
注:本文仅写了一些常用的SQL代码,一些不常用的并未列出。其次为了使代码框中的文字更加清晰,代码框中的文字并没有使用注释符进行注释。
1、基本数据类型
char(n):固定长度为n的字符串;
varchar(n):可变长度的字符串,最大为n;
int:整数类型;
smallint:小整数类型;
numeric(m,n):定点数,共m位,有n位处于小数点右边;
例如 numeric(3,1),符合条件的数据:31.4
float(n):精度为n的浮点数
real,double precision:浮点数与双精度浮点数
2、创建数据库关系(create)
这里以创建一个学生关系为例使用
create table 关系名();
进行创建,()里面包含:属性+数据类型
create table Student
(
Sno char(5),
Sname varchar(20),
Sage smallint,
Ssex char(2),
Sdept char(2));
3、主键声明(primary key)
使用
primary key(属性)
进行主键声明
例如,对以下创建声明Sno为Student的主键
create table Student
(
Sno char(5),
Sname varchar(20),
Sage smallint,
Ssex char(2),
Sdept char(2)),
primary key(Sno));
4、外键声明(foreign key)
声明外键的代码为:
foreign key 属性 references 关系(属性);
声明外键关系表示,两个属性的取值必须对应。
具体声明代码如下示例
create table Student
(
Sno char(5) ,
Sname char(20),
Sage smallint,
Ssex char(2),
Sdept char(2),
primary key(Sno)); --主键可以在最后设置
create table Course
(
Cno char(2) primary key, --主键也可以采用这种方法
Cname char(20),
Cpno char(2),
Ccredit smallint,
create table SC
(
Sno char(5),
Cno char(2),
Grade smallint,
foreign key (Sno) references Student(Sno),
这里声明了SC关系中的Sno作为外键,
被参考对象为Student关系中的Sno属性,
其中Student(Sno)也可写成Student
foreign key (Cno) references Course);
这里声明了SC关系中的Cno作为外键,
被参考对象为Course关系中的Cno属性,
两个关系中都有Cno,可省略括号
5、约束语句之不允许为空值约束(not null)
不为空约束通常就在创建关系,定义属性的同时进行约束
采用 not null,示例如下
create table student
(
st_id varchar(9) not null,
st_nm varchar(8) not null,
st_sex varchar(2),
st_birth varchar(4),
primary key(st_id));
6、关系中添加(插入)数据(insert into)
在关系中添加一个元组采用
insert into 关系名 values(属性对应的值)
具体示例如下
create table Student
(
Sno char(5) ,
Sname char(20),
Sage smallint check(Sage>=15 and Sage<=45),
Ssex char(2),
Sdept char(2),
primary key(Sno));
insert into Student values('17001','钱横',18,'男','CS');
insert into Student values('17002','王林',19,'女','CS');
insert into Student values('17003','李民',20,'男','IS');
insert into Student values('17004','赵三',16,'女','MA');
这里添加了四条student关系表的元组信息
7、删除所有元组(表中所有行的数据)(delete from)
如果要删除一个关系表中的所有信息,则可以采用delete语句
delete from student;
表示删除student关系表中的所有信息,
但会保留关系student,
即还可以往里面添加信息。
8、删除一个关系(数据表)(drop table)
如果要删除整个关系表里的所有信息以及对应关系表本身
则采用drop语句,例如
drop table student;
9、为已有关系增加属性(alter)
为一个关系表添加属性:
alter table 关系表名 add 属性 对应的域
具体示例如下
create table Student
(
Sno char(5) ,
Sname char(20),
Sage smallint,
Sdept char(2),
primary key(Sno)); --主键可以在最后设置
我们要为student关系添加一个Ssex属性为char(2)类型
alter table student add Ssex char(2);
10、从关系中删除属性(alter)
为一个关系表删除属性:
alter table 关系表名 drop 属性
具体示例如下
create table Student
(
Sno char(5) ,
Sname char(20),
Sage smallint,
Sdept char(2),
primary key(Sno)); --主键可以在最后设置
我们要删除student关系中的Sage属性
alter table student drop Sage;
要注意,对于删除属性这个操作,
在很多数据库系统里面是不支持的
11、查询语句之去重(默认不去重)(distinct)
最简单的查询语句
select 需要查询的属性名
from 关系表名
有时候我们想要强行删除重复的项,
则在select后面加上distinct去重,例如
select distinct name
from student
表示查询student关系下,name属性集,且去除重复项
12、查询语句之自然连接(natural join)
有时候,我们需要根据某些属性,
将两个表联系起来进行查询,例如以下
select name,course_id
from instructer,teacher
where instructer.id = teacher.id;
则该查询可以用更简洁的自然连接表示
select name,course_id
from instructer natural join teacher;
以此类推,也可以进行多个表的自然连接
select name,title
from stud