第一章节
(1)用SQL语言创建数据库、建表。建表时为各表建立关键字、设置外码,数据暂不录入.
建库:
```
create database school;
建表:
create table students
(sid char(10) primary key,
sname char(30) not null,
email char(30),
grade int
);
create table teachers
(tid char(10) primary key,
tname char(30) not null,
email char(30),
salary int
);
create table courses
(cid char(10) primary key,
cname char(30) not null,
hour int
);
设置外码:
create table choices
(no int primary key,
sid char(10) not null,
tid char(10),
cid char(10) not null,
score int,
constraint fksid foreign key (sid) references students(sid),
constraint fktid foreign key (tid) references teachers(tid),
constraint fkcid foreign key (cid) references courses(cid),
);
2) 为students表、courses建立按主键増序排列的索引
create index stusid on students(sid asc);
create index coucid on courses(cid asc);
3) 删除course上的索引
drop index courses.coucid;
students表中增加一个“出生日期”字段,类型为日期时间型
alter table students
add “出生日期” datetime;
5) 删除students中的“出生日期”字段
alter table students
drop column “出生日期”;
6) 删除SCHOOL数据库中的students表
alter table choices
drop constraint fksid;
drop table students;
摘要
- create database<数据库名>
功能:创建数据库 - create table <表名>
( <列名><数据类型> [列级完整性约束条件]
[,<列名><数据类型> [列级完整性约束条件]]
[,<列级完整性约束条件>] );
功能:创建一张新的表 - create [unique] index <索引名> on<表名>(<列名>);
功能:创建表的索引 - drop index 表名.索引名
功能:取消表的索引 - alter table <表名>add <列名><数据类型>;
alter table<表名> drop column<列名>;
功能:表中新增列及列数据类型和删除列 - drop table<表名>cascade;
功能:删除数据库中的表 - drop database <数据库名>
功能:删除数据库