-- 创建表
create table productinfo
(ProductID varchar2(10),ProductName varchar2(20),ProductPrice number(8,2)
,Quantity number(10),Category varchar2(10),Desperation varchar2(1000),Origin varchar2(10));
-- 添加一列
alter table productinfo add remark varchar2(200);
--修改一列的类型
alter table productinfo modify remark number(2,2);
-- 删除一列
alter table productinfo drop column remark;
-- 创建一张新表
create table categoryinfo(
CategoryId varchar2(10),
CategoryName varchar2(30),
primary key(CategoryId));
-- 添加主键约束
alter table categoryinfo
add constraints pk_category primary key(CategoryId);
--移除主键约束
alter table categoryinfo drop constraints CategoryId;
--创建一张表用作外键约束
create table productinfo1
( productid varchar2(20),
productname varchar2(20),
productprice number(8,2),
quantity number(10),
desperation varchar2(1000),
category varchar2(10),
origin varchar2(10),
primary key (productid),
constraints fk_pro foreign key(category)
references categoryinfo(CategoryId)
on delete cascade);
-- 移除外键约束和移除主键约束是一样的做法
alter table productinfo1 drop constraints fk_pro;
-- check约束也就是限制输入,例如年龄只能输入0-100之间的数目
-- 这创建的是一张表,这个表限制的是年龄输入只能在18岁和50岁之间,不能超过这个数
create table custominfo
( CumstomId varchar2(10),
Name varchar2(10),
Age number(2),
Gender varchar2(2),
Tel varchar2(10),
Address varchar2(100),
constraint chk_age check(age>=18 and age<=50));
)
--- 当然,创建了表格check检查,也可以在创建这个表格检查之后移除这个表格检查
alter table custominfo drop constraint chk_age;
-- 另外一方面,我们删除了check检查,也可以再添加上
alter table custominfo add constraint chk_age check(age>=18 and age<50);
-- unique 约束成为唯一约束,可以设置在表中输入的字段都是唯一的,这个约束和之前的主键约束很类似
-- 不同的是主键约束表中只有一个吗,但是唯一约束在表中可以有多个
alter table custominfo add constraint my_unique unique(Name);
alter table custominfo add constraint my_unique2 unique(Age);
-- 删除unique唯一约束
alter table custominfo drop constraint my_unique;
-- 非空约束
alter table custominfo modify Name not null;
-- 当然非空约束是不需要取消的,如果需要取消非空约束直接将非空约束改成null即可
oracle
最新推荐文章于 2021-04-03 07:03:56 发布