technical skills grow

Responsive Ads Here

Sunday, January 24, 2021

How to add primary or unique and foreign key in mysql

1. Primary Key :

#insert uniqe value  
CREATE TABLE STATION
(ID INT ,STATION_NAME varchar(11),DATA_DATE DATETIME,PRIMARY KEY (ID));

#uniqe value insert to ID & DATA_DATE
CREATE TABLE STATION
(ID INT ,STATION_NAME varchar(11),DATA_DATE DATETIME,PRIMARY KEY (ID,DATA_DATE));

Add constraint

alter table STATION add CONSTRAINT PK_ID PRIMARY KEY (ID,DATA_DATE)

2.Unique Key

#uniqe value insert to ID & DATA_DATE

CREATE TABLE STATION
(ID INT AUTO_INCREMENT,STATION_NAME varchar(11),DATA_DATE DATETIME,UNIQUE KEY (ID));

#alter table STATION add constraint UK_SN
unique key (STATION_NAME)
 

Foreign key : 

CREATE TABLE STATION_NAME1
(ID INT  ,Name VARCHAR(20) primary key)  

Note :This table column should be unique 

#CREATE TABLE STATION
(ID INT PRIMARY KEY ,STATION_NAME varchar(11),DATA_DATE DATETIME);
 

#Add foreign Key :

alter table STATION add foreign key (STATION_NAME)  REFERENCES STATION_NAME1(Name)




No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts