DDL数据定义语言
简介
常用的四种关键字为create,drop,alter,truncate。
1.create
create,可以用来创建,定义表,视图,序列,索引等等,这里拿建表举例
1.1 格式
有两种:
第一种:
create table 表名(
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型]
);
第二种:
create table 表名(
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
字段名 数据类型 [列约束类型],
[表级约束],
[表级约束]
);
中括号里面的内容表示可选,列级约束,表级约束都是对列中的值进行约束限制的
1.2 表的命名
对应表和列的名字:
- 必须是字母开头
- 必须是1-30个字符之间的长度
- 表名中只能出现字母、数字、_、#
- 不能和数据库中己有对象的名字重复
- 不能是数据库中的关键字
1.3 数据类型
- char 、varchar 、varchar2 都是存储字符串
char 存储数据的长度是固定的。varchar2 存储数据的长度是可变的。
char 效率比 varchar2效率高。 varchar是数据库标准类型,varchar2类型是oracle数据库中特有的
varchar2不能存空字符串,可以存null。varchar可以存空字符串 - number(p,s)
p 表示最大位数(整数位+小数位), s表示保留的小数位(四舍五入),也可以为负数。
例如,number(5,2)
存进去123.456,取出来为123.46
存进去12345.456,运行报错:值大于为此列指定的允许精度
可以直接使用number,不加参数,描述没有默认没限制 - date日期类型
- blob 存二进制对象,例如视频,音频,图片等
- clob存储大文本,例如很多很多文字
1.4 约束
约束就是对值的要求,如值是否可以为空,是否是惟一,等等
1.4.1 常见约束
- 主键约束 PRIMARY KEY primary key
- 外键约束 FOREIGN KEY foreign key
- 唯一约束 UNIQUE unique
- 非空约束 NOT NULL not null
- check约束 CHECK check
1.4.2 约束分类:
(1)列级约束
不带约束建表
create table student(
id number primary key,
name varchar2(200) not null,
age number,
birthday date
);
带约束建表:
create table student(
id number primary key,
name varchar2(100) not null,
email varchar2(100) unique,
gender char(1) check(gender in('f','m')),
age number,birthday date
);
(2)表级约束
create table student(
id number,
name varchar2(20) not null,
age number default 20,
email varchar2(100),
gender char,
primary key(id),
unique(email),
check(gender in('f','m'))
);
注意,非空约束(not null),不能声明成表级约束
使用表级约束声明外键约束
create table t_customer(
id number primary key,
name varchar2(200) not null
);
create table t_order(
id number primary key,
content varchar2(200) not null,
customer_id number,
foreign key(customer_id) references t_customer(id)
);
使用表级约束 声明 联合唯一约束
create table student(
id number primary key,
class varchar2(50) not null,
name varchar2(50) not null,
unique(class,name)
);
联合唯一约束必须使用表级约束来声明
表级约束和列级约束对比:
- 表级约束和列级约束所写的位置不一样
- not null约束不能用表级约束来声明
- 表级约束和列级约束声明语法稍有所不同
- 如果要声明的约束为联合主键、联合外键、联合唯一的时候,就一定要用表级约束
1.4.3 给约束起名字
在创建约束的时候(行级、表级都可以),还可以给约束起一个名字,这时候就要使用关键字constraint
起名字的规律一般会是:表名_列名_约束类型,如果没有给约束起名字,那么系统也会给这个约束起一个默认的名字,将来我们可以根据之前给约束起好的名字,而找到这个约束,然后进行修改获取其他操作。
例如:
(1)列级约束起名字
create table student(
id number constraint student_id_pk primary key,
name varchar2(100) constraint student_name_nn not null,
email varchar2(100) constraint student_email_un unique,
gender char(1) constraint student_gender_ck check(gender in('f','m')),
age number,
birthday date
);
(2)表级约束起名字
create table t_customer(
id number,
name varchar2(20) not null,
age number,
email varchar2(100),
gender char,
constraint cus_id_pk primary key(id),
constraint cus_email_un unique(email),
constraint cus_gender_ck check(gender in('f','m')));
create table t_order(
id number,
price number not null,
customer_id number,
constraint order_cid_fk foreign key(customer_id) references t_customer(id)
);
建表时的一些特殊操作
(1)建立一张表和s_dept一模一样,s_dept的表结构和表中的数据全部复制过来
create table test1
as
select * from s_dept;
(2)建立一张表和s_dept一模一样,只拿来s_dept的表结构,没有数据
create table test2
as
select * from s_dept
where 1=2;
(3)建立一张表和s_dept一模一样,只复制表中某几个指定列的数据
create table test3
as
select id,last_name,salary from s_dept;
2、drop
作用:进行删除操作。
drop用来删除表,序列,视图,索引等,这里拿表举例。
drop from 表名;
没错,就这么简单
当两个表中存在外键约束时:
//这是一个顾客表
create table t_customer(
id number primary key,
name varchar2(200) not null
);
//这是订单表
create table t_order(
id number primary key,
content varchar2(200) not null,
customer_id number references t_customer(id)
);
drop table t_customer;
drop table t_order;
两张表中的关系是:订单表中的外键列customer_id的值,是引用自顾客表t_customer中的主键列id的值
此时直接删除顾客表示不会成功的,因为t_customer的主键列的值被别的表给引用了
那么该如何删除呢:
(1)方式一:可以先删除订单表t_order,然后再删除t_customer就可以了
(2)方式二:如果非要想直接删除当前这个顾客表t_customer,就需要使用下面的语句:
drop table t_customer cascade constraints;
表示删除t_customer表的同时,也级联删除与表相关的约束,外键约束没有了,这个表自然可以被删除掉,cascade是级联的意思
3、alter
在表创建好的情况下,可以使用alter关键字,来修改表的信息。
alter属于DDL语句,会自动当前事务。
具体使用:
测试用表:
create table t_user(
id number constraint user_id_pk primary key,
name varchar2(100),
salary number
);
drop table t_user;
(1)在表中添加新列
alter table t_user
add birthday date;
(2)删除表中的列
alter table t_user
drop column birthday;
(3)给表中添加约束
alter table t_user
add constraint user_name_ununique(name);
(4)删除表中的约束
alter table t_user
drop constraint user_name_un;
(5)修改表名
rename t_user to mytest;
rename mytest to t_user;
(6)修改列的数据类型
alter table t_user
modify (name varchar2(500));
(7)设置约束失效
alter table t_user
disable constraint user_id_pk cascade;
这里必须知道约束的名字
4、truncate
用来截断表中的数据,表示先删除,再提交。
truncate table t_user;
相当于下面的代码
delete from t_user;
commit;