close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com




MySQL - PRIMARY KEY

A PRIMARY KEY is a column of table which uniquely identifies each tuple (row) in that table..

Primary keys must contain UNIQUE values, and cannot contain NULL values

Primary key always has unique data.

A table can contain only one primary key constraint.

The main advantage of Primary key is that we get fast access.


Syntax : PRIMARY KEY


CREATE TABLE table_name
 (
 column1 data_type(size) NOT NULL ,
 column2 data_type(size) NOT NULL,
 column3 data_type(size) NOT NULL,
 ....
 PRIMARY KEY(column)
 );      

The following MySQL creates a PRIMARY KEY on the "ROLL_NO" column when the "Students" table is created


Example : PRIMARY KEY


 CREATE TABLE Students
 (
 ROLL_NO int(3) NOT NULL ,
 NAME varchar(20) NOT NULL,
 SUBJECT varchar(20) ,
 PRIMARY KEY(ROLL_NO)
 );    

mysql primary key

To create a PRIMARY KEY constraint on the "ROLL_NO" column when the table is already created, use the following MySQL:


Example : PRIMARY KEY


ALTER TABLE Students 
ADD PRIMARY KEY (ROLL_NO);