SQL Primary Key, SQL Tutorial, Global Guide Line Technology.
Bookmark Bookmark: SQL Primary Key, SQL Tutorial Forum | Blog |

      Home                    

Basic SQL Guide.           
SQL Introduction.               
 What is Table?                  
 SQL Create Table.            
 SQL Insert Statement.      
 SQL Select Keyword.        
 SQL Order by clause.        
 SQL Count Statement.      
 SQL Group By Clause.      
 SQL Having Clause.          
 SQL Alias Tables.             
 SQL Join Tables.              
 SQL Outer Join.               
 SQL Update Statement.   
 SQL Delete Statement.    
 SQL Distinct Keyword.      
 SQL Where Keyword.       
 SQL And Or Keyword.      
 SQL In Keyword.              
 SQL Between Keyword.   
 SQL Like Keyword.          
 SQL Functions.                
 SQL Concatenate.           
 SQL Substrings.               
 SQL Trim Function.         
 SQL Constraints.              
 SQL Primary Key.
 SQL Foreign Key.            
 SQL Create Views.           
 SQL Create Index.           
 SQL Alter Table.             
 SQL Drop Table.             
 SQL Truncate Table.      
 SQL Summary.                

SQL Tutorial >> SQL Primary Key.


     Back             Next     



SQL Tutorial will guide us now, that an SQL Primary Key is used to uniquely identify each row in our table. It can either be part of the actual record itself , or it can be an artificial field one that has nothing to do with the actual record. An SQL Primary Key can consist of one or more fields on a table. When multiple fields are used as an SQL primary key, they are called a composite SQL primary key.

Primary keys can be specified either when the table is created (using SQL CREATE TABLE) or by changing the existing table structure (using SQL ALTER TABLE).

Below are examples for specifying a SQL Primary Key when creating a table:

Example of SQL PRIMARY Key Constraint with MySQL Create Table.

CREATE TABLE Employees
(empID integer,
FirstName varchar(30),
LastName varchar(30),
PRIMARY KEY (empID));

Example of SQL PRIMARY Key Constraints with Oracle Create Table.

CREATE TABLE Employees
(empID integer PRIMARY KEY,
FirstName varchar(30),
LastName varchar(30));

Example of SQL PRIMARY Key Constraints wtih SQL Server Create Table.

CREATE TABLE Employees
(empID integer PRIMARY KEY,
FirstName varchar(30),
LastName varchar(30));

Below are examples for specifying an SQL Primary Key Constraint by using SQL ALTER Table Command:

SQL PRIMARY Key Constraint Example with MySQL Alter Table.

ALTER TABLE Employees ADD PRIMARY KEY (empID);

SQL PRIMARY Key Constraint Example with Oracle Alter Table.

ALTER TABLE Employees ADD PRIMARY KEY (empID);

SQL PRIMARY Key Constraint Example with SQL Server Alter Table.

ALTER TABLE Employees ADD PRIMARY KEY (empID);

Note: Before using the SQL ALTER TABLE command to add an SQL Primary Key Constraint, we'll need to make sure that the field is defined as 'NOT NULL'  in database, other wise NULL cannot be an accepted value for that field we must make that field or column as 'NOT NULL'

     Back             Next     
 

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2018 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 212 visitors are online now