oracle 数据定义语言(DDL)语法

  DDL语言包括数据库对象的创建(create)、删除(drop)和修改(alter)的操作

1.创建表语法

 create table table_name(

column_name datatype  [null | not null],

column_name datatype  [null | not null],

..........

[constraint]

)

constraint 是为表中的列设置约束,常见的有主键约束、外键约束、检查约束等等。

示例:创建productionfo 表

CREATE TABLE productionfo(
productId VARCHAR2(10),
priductName Varchar2(20),
prioducePrice NUMBER(8,2),
productName NUMBER(10),
productType VARCHAR2(10),
origin VARCHAR2(10)

)

对表的操作

1.删除表

drop table productionfo

2.清空表的数据

truncate table productionfo

3.修改表名

Alter table tableName  rename to  newTableName  语法结构

Alter table productionfo rename to production

4.修改列明

alter table productionfo rename productType to type

5.修改列的类型

alter table productionfo modify productType varchar(30)

6添加列

Alter table productionfo add miaoshu varchar2(20)

7.删除数据表一列

Alter table productionfo drop column miaoshu 

8.添加注释

comment on  column 表名.字段名 is '注释内容'  语法

comment on column productionfo productType is '产品类型'

二.约束

   oracle 数据库约束 ,主键约束、外键约束、唯一约束、检查约束、非空约束。

1.主键约束

     主键约束每一个表中只有一个添加方式有两种:

     创建表时添加 crreate table tableName(

           column datatype  primary key

      .........

      )

    使用constraints 关键字添加

 语法:alter table tableName  add constraints pk_productId  primary key (productid)

示例:ALTER TABLE productionfoo ADD CONSTRAINTS pk_productid PRIMARY KEY(productid)

特点:该表productid 这一列数据不能重复也不能为空

1.1 移除主键约束

  alter table production drop constraints pk_productid             -------pk_productid  是添加主键时的名字。

 2.外键约束

  外键约束可以保证使用外键约束的数据表列与所运用的主键约束的数据列一致,外键约束可以再同一表中添加多个  

语法:alter table table1add constraint  fk_name(外键名称) foreign key (要设为外键的列名) references table2(columnName)(与哪个表有关联表2中该列列名);

3.检查约束--check约束

  check约束是检查约束,能规定每一个列能输入的值,以保证数据的正确性

  添加方式:创建表的时候

 CONSTRAINT constraint_name CHECK (column_name condition)
如:constraint constraint_productId  check(productId <100)
修改表的时候添加 add constraint constraint_name check(column condition)
4.UNIQUE 约束 唯一性约束
可以设置表中输入的字段都是唯一的。
CONSTRAINT constraint_name UNIQUE(column_name)
5.NOT NULL 约束
创建表的时候直接在字段后边添加 not null 关键字即可
修改表时:alter table table_name modify column not null;
 

  

 

猜你喜欢

转载自www.cnblogs.com/gubai/p/9244553.html