oracle sql 之表及约束
2015-04-06 16:49
78 查看
基本了解:数据库对象包括:表,试图,索引,序列,同义词
常用操作sql命令
1:查看数据库中创建所有的表名
select type_name from user_tables;
2:查看创建所有的数据库对象
select distinct object_name from user_objects;
创建表:
1:第一种方式
create table 表名(
字段 类型
...............
)
例如:create table employees (
id number(10),
name varchar2(20),
salary number(10,2),
hire_date date
);
2:第二种方式,依据现有表来创建表
create table 表名
as
select 字段1,字段2,....... from 表名 where 1=2;
例如:select id,ename,department_id,department_name,salary from emp where 1=2;
( 其中where 1=2 的目的是为了(只建立表结构)在创建表时不把原有表的数据也插入到新建的表中)
修改表:ALTER
1:为表增加一列 alter table 表名 add(字段,类型)
例如:alter table emp add(email varchar2(20))
2:修改表数据类型 alter table 表名 modify(字段,类型)
例如:alter table emp modify(email varchar2(15))
3: 删除一列 alter table 表名 drop column 列明
例如:alter table emp drop column email
4:对列重命名 alter table表名 rename column 列名 to 改后的列名
例如:alter table emp rename column email to emails
删除表:drop
drop table 表名
列级约束:作用范围在列上,列约束必须跟在列定义的后面
表级约束:作用在多个列上,也可以作用在一个列上,表级约束单独定义,不跟在列定义的后面
约束关键词:唯一:unique 非空:not null 主键:primary key 外键:foreign key
check约束:含有check约束的字段输入的内容只能为check约束的内容。
级联删除(on delete cascade): 当父表中的列删除时,子表中相应的列也删除
级联置空(on delete set null): 当附表中列删除时,子表中相应的列置空
在数据库中sql创建表示例:
create table emp2(
--列级约束
id varchar2(10) ,
name varchar2(10) not null,
sex varchar2(2) check(sex='男' or sex='女'),
telephone varchar2(11),
salary number(10,2),
email varchar2(15) unique,
department_id varchar2(10),
--表级约束
constraint emp2_email_pk
primary key(id),
constraint emp2_dept_fk
foreign key(deptment_id) references department(deptment_id)on delelte cascade
);
修改约束:alter table emp2
modify(salary number(10,2) not null) !!!注意:添加not null约束时,用modify
添加约束; alter table emp2 add constraint emp2_telephone_un unique(telephone)
删除约束;alter table 表名 drop constraint约束名
使得约束失效:alter table 表名
disable constraint 约束名
使得约束有效:alter table 表名enable constraint 约束名
常用操作sql命令
1:查看数据库中创建所有的表名
select type_name from user_tables;
2:查看创建所有的数据库对象
select distinct object_name from user_objects;
创建表:
1:第一种方式
create table 表名(
字段 类型
...............
)
例如:create table employees (
id number(10),
name varchar2(20),
salary number(10,2),
hire_date date
);
2:第二种方式,依据现有表来创建表
create table 表名
as
select 字段1,字段2,....... from 表名 where 1=2;
例如:select id,ename,department_id,department_name,salary from emp where 1=2;
( 其中where 1=2 的目的是为了(只建立表结构)在创建表时不把原有表的数据也插入到新建的表中)
修改表:ALTER
1:为表增加一列 alter table 表名 add(字段,类型)
例如:alter table emp add(email varchar2(20))
2:修改表数据类型 alter table 表名 modify(字段,类型)
例如:alter table emp modify(email varchar2(15))
3: 删除一列 alter table 表名 drop column 列明
例如:alter table emp drop column email
4:对列重命名 alter table表名 rename column 列名 to 改后的列名
例如:alter table emp rename column email to emails
删除表:drop
drop table 表名
列级约束:作用范围在列上,列约束必须跟在列定义的后面
表级约束:作用在多个列上,也可以作用在一个列上,表级约束单独定义,不跟在列定义的后面
约束关键词:唯一:unique 非空:not null 主键:primary key 外键:foreign key
check约束:含有check约束的字段输入的内容只能为check约束的内容。
级联删除(on delete cascade): 当父表中的列删除时,子表中相应的列也删除
级联置空(on delete set null): 当附表中列删除时,子表中相应的列置空
在数据库中sql创建表示例:
create table emp2(
--列级约束
id varchar2(10) ,
name varchar2(10) not null,
sex varchar2(2) check(sex='男' or sex='女'),
telephone varchar2(11),
salary number(10,2),
email varchar2(15) unique,
department_id varchar2(10),
--表级约束
constraint emp2_email_pk
primary key(id),
constraint emp2_dept_fk
foreign key(deptment_id) references department(deptment_id)on delelte cascade
);
修改约束:alter table emp2
modify(salary number(10,2) not null) !!!注意:添加not null约束时,用modify
添加约束; alter table emp2 add constraint emp2_telephone_un unique(telephone)
删除约束;alter table 表名 drop constraint约束名
使得约束失效:alter table 表名
disable constraint 约束名
使得约束有效:alter table 表名enable constraint 约束名
相关文章推荐
- 利用信赖域算法求解无约束的非线性最小二乘问题~
- java中覆盖必须满足的约束
- sql server unique约束的增加与删除
- Oracle常见约束
- SQL - 创建一个学生表,要求有主键约束和非空约束
- 总结mysql的三种外键约束方式
- 2014.11.22 差分约束学习笔记
- SQL语句面试题目:多表查询、SQL约束、DDL、DML
- PHP的MSSQL操作类部署之后,是完全独立的操作类,不受SQL"客户端网络实用工具"约束!
- MySQL 外键约束的要求解析
- Oracle笔记之 创建和管理表_数据处理_约束
- 无约束最优化方法
- Storyboard(故事板)中的UIScrollView添加约束,使其能够滚动
- javaBean验证框架(6)—附加约束和组合约束
- sql完美删除外键约束,避免产生问题
- POJ 1716 Integer Intervals 差分约束
- 关于xml学习(二)——xml的约束之DTD
- bzoj 1202(差分约束)/(带权并查集)
- Cadance约束设置分析
- SQL--constraint 约束