Primary key and foreign key examples
PRIMARY KEY constraint is used to prevent duplicate value in column.
FOREIGN KEY constraint is used to create link to another table's column.
PRIMARY KEY and FOREIGN KEY Example
Create Table Items ( ItemCode varchar(10)not null constraint pkItemCode Primary key, ItemName varchar(20)not null, ItemQuantity float not null, ItemPrice float not null ) Create table OrderDet ( OrderId varchar(20)not null constraint pkorderid primary key, ItemCode varchar(10) not null Constraint fkItemCoe Foreign Key (ItemCode) references Items(ItemCode), ItemName varchar(20)not null, OrderedQuantity float not null, ItemPrice float not null ) Insert into Items Values('P001','Zara tshirt','50','50') Insert into Items Values('P002','Adidas tshirt','50','80') Insert into OrderDet values('001','P001','Zara tshirt','2','50') Insert into OrderDet values('002','P002','Adidas','1','80')
PRIMARY KEY and FOREIGN KEY constraints example in video
PRIMARY KEY and FOREIGN KEY constraints example in image
Alter foreign key constraint sql server
Alter table OrderDet Drop fkItemCode Alter Table OrderDet add Constraint fkItemCoe Foreign Key (ItemCode) references Items(ItemCode)
No comments:
Post a Comment