首页 > 代码库 > 数据库(补充)

数据库(补充)

1、创建并命名外键

alter table OrderDetails add constraint fk_oderId foreign key (OrderId) references orders(OrderId);
alter table OrderDetails add constraint fk_productId foreign key (ProductId) references Products(ProductId);

2、创建索引

create index idx_orderName on orders(OrderName);

3、创建视图

create view ww_sttistics
as
select Products.ProductName,SUM(Quantity) as number ,sum(Amount) as total
from Products,Orders,OrderDetails
where Products.ProdcuctID = OrderDetails.ProductID and OrderDetails.ProductName = Orders.OrderID
group by Products.ProductName

4、建表-性别约束

SEX char(2) not null check(DSEX in (‘,‘)) default ‘,

5、建表-年龄约束

AGE smallint check(DAGE >= 20 and DAGE <= 60),

6、级联删除

SQL数据库的表,进行级联操作,首先需要设置表的主外键关系,同时也要定义级联,

create table TEACHERS(

  TNO varchar(5),

  TNAME varchar(22),

  TSEX char(2),

  primary key(TNO)

);

 

create table WORKS(

  TNO varchar(5),

  CNO varchar(5),

  WYEAR char(4),

  WSTAGE char(2),

  primary key(TNO,CNO),

  foreign key (TNO) references TEACHERS(TNO) on delete cascade

);

数据库(补充)