DDL

建表:
create table 表名 (
列名 数据类型(值) 约束类型,
列名 数据类型(值) 约束类型,
);
关联主键语法:foreign key(manager_id) references p_emp(id)
约束类型:
primary key 主键 pk
not null 非空 nn
unique 唯一
check 自定义 ck ,比如限制(列名> 0)
数据类型:
number(3,2)2表示小数点几位
char 固定长度
varchar2 不固定
添加约束名:
constraint 约束名 约束类型
约束命名规范:表名_列名_约束类型缩写
第二种添加约束的方式:
先定义列,再定义约束 、、not null 只能跟列名写一起
constraints 表名_列名_约束缩写 约束类型 (列表名);
、、、、、、、foreign key(manager_id) references p_emp(id) 外键关联主键


复制一个表格里的数据:
create table 新表 as select id,first_name,salary from s_emp;
复制表格,支付至表结构,但是不复制数据
create table new_emp as select id,first_name,salary from s_emp where 1=2; 此处1=2是false,则数据不会被拷贝

删除表格:
drop table 表名
drop table 表名 cascade constraints; 当表的主键被别的表引用时后面加cascade constraints;基本不会用到
修改表格:
1添加
alter table 表名 add 列名 数据类型 约束;——alter table tbl_user add age number(3) default 18 not null;
2删除一列
alter table 表名 drop column 列名;——alter table tbl_user drop column age;
3修改列(修改列的数据类型和约束)
alter table 表名 modify 原列名 新数据类型 新约束;——alter table tbl_user modify password default'000000' not null;
4修改列名
alter table 表名 rename column 原列名 to 新列名;——alter table tbl_user rename column password to pwd;
修改约束:
1.添加约束
alter table 表名 add constraint 约束名 约束类型(列名)
注意:如果是添加非空约束,则:
alter table 表名 add constraint 表名_列名_nn check (is not null)
注意:所有的DDL语句都是自动提交事务的,所以语句是不能回滚的
2.删除约束
alter table 表名 drop constraint 约束名;
3.使约束生效
alter table 表名 enable constraint 约束名
4.使约束失效
alter table 表名 disable constraint 约束名

posted @ 2017-11-06 20:27  沃泽法克  阅读(232)  评论(0)    收藏  举报