文章目录
1、模式的定义与删除
1.1、定义模式
SQL的数据定义语句
操作对象 | 创建 | 删除 | 修改 |
---|---|---|---|
模式 | create schema | drop schema | |
表 | create table | drop table | alter table |
视图 | create view | drop view | |
索引 | create index | drop index | alter index |
模式定义语句:
create schema <模式名> authorization<用户名> //**模式名默认为用户名 **
在创建模式的同时可以在这个模式下进一步创建基本表、视图、等:
create schema <模式名> authorization<用户名>[<表定义子句>|<视图定义子句>|<授权定义子句>];
//eg
create schema test authorization zhang
create table tab1
(col1 smallint,
col2 int,
);
1.2、删除模式
drop schema <模式名><cascade|restrict>
//cascade和restrict必须两者选一个,cascade表示删除模式时并删除所有数据库对象
//restrict表示如果模式下有对象,则不能删除
2、基本表的定义、删除与修改
2.1、定义基本表
create table <表名> (<列名><数据类型>[列级完整性约束条件]
[,<列名><数据类型>[列级完整性约束条件]]
...
[,<表级完整性约束>]
);
2.2、修改基本表
alter table <表名> //要修改的基本表
[add [column] <新列名><数据类型>[完整性约束]]
[add <表级完整性约束>] //删除表中的列
[drop [column] <列名> [cascade|restrict]]
[rrop constraint <完整性约束名> [restrict|cascade]] //删除指定的完整性约束条件
[alter column <列名> <数据类型>]; //修改原有的列定义(列名、数据类型)
eg:
- 向student表增加“入学时间”列,数据类型为日期型。
alter table student add S_entrance date;
- 将年龄的数据类型改为整数
alter table student alter column sage int;
3.增加课程取唯一值的约束
alter table course add unique(cname);
2.3、删除基本表
删除格式:
drop table <表名> [restrict|cascade]; //默认情况下为restrict
1、restrict:有限制的删除,删除时,基本表不能被其他的表所约束
2、cascade: 无限制删除
3、索引
3.1、建立索引
create [unique] [cluster] index <索引名> on <表名>(<列名>[次序][,...])
//unique:每一个索引值只对应唯一的数据记录
//cluster:聚簇索引
eg:
creater unique index stuson on student(son asc); //student表按学号升序建立唯一索引
//asc:升序,默认为升序
//desc:降序
3.2、修改索引
索引重命名:
alter index <旧索引名> rename to <新索引名>;
3.3、删除索引
drop index <索引名>;