SQL KEYS
Different types of key in SQL:
PRIMARY KEY
A column or columns is called primary key (PK) that uniquely identifies each row in the table. When multiple columns are used as a primary key, it is known as composite primary key.
syntax
CREATE TABLE table_name( var_name vartype() , var_name1 vartype() , var_name 2 vartype() PRIMARY_KEY(varname) )
FOREIGN KEY
foreign key is a field or a column that is used to establish a link between two tables.
CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) );
COMPOSITE KEY
A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table when the columns are combined uniqueness is guaranteed, but when it taken individually it does not guarantee uniqueness.
CREATE TABLE TABLE_NAME (COLUMN_1, DATA_TYPE_1, COLUMN_2, DATA_TYPE_2, ??? PRIMARY KEY (COLUMN_1, COLUMN_2, ...));
UNIQUE KEY
A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a database table.
mysql unique key
CREATE TABLE students CREATE TABLE students ( S_Id int NOT NULL, LastName varchar (255) NOT NULL, FirstName varchar (255), City varchar (255), UNIQUE (S_Id) )
ALTERNATE KEY
Alternate key is a secondary key