PRIMARY KEY and FOREIGN KEY tutorial in php

List of Constraints in MySQl :

NOT NULL

UNIQUE

DEFAULT

CHECK

PRIMARY KEY

FOREIGN KEY


What is PRIMARY KEY Constraint ?

Primary key always has unique data.

A primary key cannot have null value.

A table can contain only one primary key constraint.


Create Table with PRIMARY KEY Syntax :

CREATE TABLE tableOne_name(

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(50) NOT NULL,

age INT NOT NULL,

city VARCHAR(10) NOT NULL,

PRIMARY KEY(id)

);


Create Table with FOREIGN KEY Syntax :

CREATE TABLE tableTwo_name(

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(50) NOT NULL,

age INT NOT NULL,

city VARCHAR(10) NOT NULL,

PRIMARY KEY(id) ,

FOREIGN KEY(city) REFERENCES tableOne_name(cid)

);


Alter Table with FOREIGN KEY Syntax :

ALTER TABLE table_name ADD FOREIGN KEY (city) REFERENCES tableOne_name(cid),

Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions