oracle

-- 创建表
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即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值