OracleDDL基本操作
和大家分享一下我在Oracle数据库定义语言的学习笔记
1、创建表空间语法
create tablespace test --表空间名称
datafile ‘D:\oradata\orcl\test.dbf’ --表空间对应的数据文件
size 32m --初始大小
autoextend on; --自动增长打开
2、删除表空间语法
drop tablespace test
including contents and datafiles;
including contents and datafiles
表示删除表空间的内容和对应的数据文件
3、为用户指定默认表空间语法:
create user 用户名 identified by 密码
default tablespace 表空间名;
4、创建表语法:
create table student(
sid number(4), --学号
sname varchar2(20), --姓名
sex char(2), --性别
birthday date, --生日
sal number(7,2) --奖学金
);
5、删除表语法:
drop table student;
6、修改表 alter table
增加一列
alter table 表名 add (列名 类型);
修改字段的长度
alter table 表名 modify (列名 类型);
删除一列
alter table 表名 drop column 列名;
修改表的名字
rename 旧表名 to 新表名;
查看表结构
desc 表名;(注意:此命令局限于命令窗口)
7、约束 constraint :保证数据的完整性,避免非法数据。
主键约束 primary key
外键约束 foreign key
检查约束 check
是否为空 null | not null
默认值 default
添加约束语法:
alter table 表名
add constraint 约束名 约束类型 约束条件;
8、删除约束语法:
alter table 表名
drop constraint 约束名;
9、主键约束
alter table 表名
add constraint 约束名 primary key(列名);
10、外键约束
alter table 表名
add constraint 约束名 foreign key(外键列)
references 主表名(主键列);
11、检查约束
alter table 表名
add constraint 约束名 check(检查条件);
12、删除约束
alter table 表名
drop constraint 约束名;
13、非空约束
alter table 表名
modify 列名 not null;
14、默认约束
alter table 表名
modify 列名 default 默认值;
15、复制表:利用现有表创建新表
CREATE TABLE <table_name>
as <SELECT 语句>; (注意:复制表不能复制表的约束)
版权声明:本文为博主原创文章,未经博主允许不得转载。