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? column 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;