SQL Primary Key – Tutorialpath

SQL Primary Key – Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world.

As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating a SQL Primary Key. With the help of this article, you will get to know the complete process of how to create a SQL Primary Key in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

SQL Primary Key

Summary: in this tutorial, you will learn about the primary key and how to use SQL primary key constraint to add a primary key to the table. 

What is a Primary Key in SQL?

A primary key is a field or combination of fields that uniquely identifies a record. A primary key is a column or group of unique columns. In other words, every value for the primary key is unique.

The primary key that consists of two or more columns is also known as the composite primary key.

Rules for Primary Key

  • Each table can have only one primary key.
  • All values ​​are unique and primary key values ​​can uniquely identify each row.
  • The system will not allow a row to be inserted with a primary key that already exists in the table.
  • A primary key cannot be full.

If the primary key consists of only one column, you can define the use of PRIMARY KEY constraint as a column constraint:

CREATE TABLE table_name (
    pk_column data_type PRIMARY KEY,
    ...
);

In case the primary key has two or more columns, you must use the PRIMARY KEY constraint as a table constraint:

CREATE TABLE table_name (
    pk_column_1 data_type,
    pk_column_2 data type,
    ...
    PRIMARY KEY (pk_column_1, pk_column_2)
);

Each table can have only one primary key. Not all columns that participate in the primary key must be defined as NULL. SQL Server automatically sets the NOT NULL constraint for all primary key columns if the NOT column constraint is not specified for these columns.

Example:

Course_id Course_name
1 PHP
2 JAVA
3 HTML

Since the values ​​in the course_id column are uniquely identified to the rows in the course table, the course_id column is the primary key of the course table.

Each table has one and only one primary key. The primary key does not accept NULL or duplicate values.

If the primary key contains two or more columns, the values ​​may be duplicated in one column, but the combination of values ​​from all columns in the primary key must be unique.

Employee_id Course_id Taken_date
100 3 17-06-1998
101 3 20-11-2000
102 3 10-01-2004
103 3 02-09-2011
104 3 16-05-2009
105 3 21-03-2013
106 3 13-12-2017
107 3 24-04-2020

The primary key of the training table consists of the Employee_ID and Course_ID columns.

The values ​​in the course_id column are duplicate, although the combination of values ​​in employee_id and course_id is not.

To create a primary key in a new table

Here is the syntax to define the ID attribute as a primary key in a CUSTOMERS table.

CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);

To create a primary key constraint on the “ID” column when the CUSTOMERS table already exists, use the following syntax

ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);

NOTE: If you use the ALTER TABLE statement to append a primary key, the primary key column (s) must already be declared to not contain the NULL value (when the table was first created).

Delete Primary Key

You can clear the primary key constraints from the table with the syntax given below.

ALTER TABLE table_name
DROP CONSTRAINT primary_key_constraint;

In this tutorial, we have introduced you to the primary key concept and shown you how to manage the primary key of a table including adding and removing the primary key.

Conclusion

So, hopefully, we have shared all the basic steps that help you with SQL Primary Key.  PHP extract the last character from a string. In this tutorial, you have several ways for PHP to remove the last character from any given strings.

Leave a Reply

Your email address will not be published. Required fields are marked *