A table has a particular number of columns and each column has n number of rows. At times, there might be a probability of repeated rows in a column. For example, a column named identification number has repeated rows. To avoid the duplication of rows, the concept of a key has been introduced.
A key is an attribute or a set of attributes that can be uniquely identifiable. There are different keys like Primary key, Foreign key, Super key, and Candidate key. Primary key is used to identify the unique row from a single column or a set of columns.
Basic Syntax –
create table table name( pk_column datatype primary key .... );
create table student rollnumber number Primary key name varchar2(50) course varchar2(30)
The table is created and the values are inserted –
In the above table, Roll Number 116 is repeated twice. It violates the primary key rule. Hence, gives an error.
The primary key plays a pivotal role in database manipulation. It is impossible to imagine a database without a primary key. A database without a primary key is tedious work. Many problems might arise when a primary key is not included in a table.
A table can have only a single primary key.
NOT NULL constraint must be included along with the columns that have a primary key. In the case of MS SQL SERVER, NOT NULL is automatically created. When the primary key is added to a column, a unique clustered index is created automatically by SQL Server.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between Primary key and Super key
- Primary Indexing in Databases
- Difference between Primary key and Unique key
- Difference between Primary Key and Foreign Key
- Difference between Primary and Candidate Key
- Difference between Primary and Secondary Data
- Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
- Where clause in MS SQL Server
- Order by in MS SQL Server
- Introduction of MS SQL Server
- SQL SERVER | IN Condition
- Joins in MS SQL Server
- Foreign key in MS SQL Server
- Select top in MS SQL Server
- SQL Server Identity
- Having clause in MS SQL Server
- Difference between MS SQL Server and IBM Db2
- Difference between MS SQL Server and IBM DB2
- Mean and Mode in SQL Server
- SQL Server | SERVERPROPERTY()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.