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 CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference between Primary key and Unique key
- Difference between Primary Key and Foreign Key
- Difference between Primary key and Super key
- Difference between Primary and Candidate Key
- Foreign key in MS SQL Server
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
- Primary Indexing in Databases
- Difference between Primary and Secondary Data
- Difference between Super Key and Candidate Key
- Delete statement in MS SQL Server
- SQL Server Mathematical functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
- SQL Server Identity
- SQL SERVER | Conditional Statements
- SQL Server | STUFF() Function
- SQL Server | SERVERPROPERTY()
- Mean and Mode in SQL Server
- Allow only alphabets in column in SQL Server
- SQL SERVER | IN Condition
- Difference between MySQL and MS SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.