Defining primary key on database table
I have a large table and there is no any restriction to insert the value and I want to make a field as a unique. The data which would be entered into that particular column,That should be uniquely identified for that tuple. So the primary key concept comes into my mind but how would I implement after creation of table. So please go through the procedure to make a field Unique for whole table data.
Thanks...
Defining primary key on database table
Defining primary key :
The is the combination of one or more field which uniquely identify particular tuple of a table.A table can have only one primary key.The primary key can be defined for a table before and after creation of table.
The creation of primary key constraint for a table during creation of table would be as follows :
Code:
CREATE TABLE table_name
(col1 datatype null/not null, col2 datatype null/not null,...
CONSTRAINT constraint_name PRIMARY KEY (col1, col2, . col_n));
In this way,you can define the primary key for a table during creation of table.
Defining primary key with ALTER command
Defining primary key after table creation :
You can create the primary key constraint after creation of table.The primary key can be defined using ALTER ... command of the database.
The definition of primary key would be as follows:
Code:
ALTER TABLE table_name
add CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
The add CONSTRAINT option will create the primary key using ALTER TABLE statement.
Dropping primary key of database table
Dropping primary key :
You can define the primary key and drop it also using drop statement.You can create the primary key manually and automatic. The default primary key is automatically created when you define any column as UNIQUE.
In the case of UNIQUE,you can alter the primary constraint but can't delete and you can edit it. If you have created manually then you define that with a name.Use that name to DROP the primary key which is given below :
Code:
ALTER TABLE table_name
drop CONSTRAINT constraint_name;
Disabling and Enabling primary key
Disabling and Enabling primary key :
You can disable and enable the functioning of primary key constraint of your database table.Here I am going to show you the syntax to disable and enable it.
To Disable,use this statement :
Quote:
ALTER TABLE table_name
disable CONSTRAINT constraint_name;
To Enable,use this statement :
Quote:
ALTER TABLE table_name
enable CONSTRAINT constraint_name;