PRIMARY KEY



PRIMARY KEY in SQL Server Database Table

In SQL server,PRIMARY KEY constraint uniquely identifies each row/record in a table. It can consists of single or multiple columns.


  • A table can have only one primary key.

  • A primary key must contain unique values and cannot have NULL values.

  • Primary key can consist of single or multiple columns (fields),when multiple fields are used as a primary key, they are called as composite key.

  • A primary key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.


    SQL PRIMARY KEY on CREATE TABLE
    Syntax to define the ID attribute as a primary key.
    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 SQL syntax:

    ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID);
    

    NOTE − If you use the ALTER TABLE statement to add a primary key,we have to make sure that the primary key column(s) do not contain NULL values when the table was first created.

    For defining a PRIMARY KEY constraint on multiple columns, use the SQL syntax given below:

    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, NAME)
    );
    

    To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when CUSTOMERS table already exists the following SQL syntax be used:

    ALTER TABLE CUSTOMERS 
       ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID, NAME);
    
    SQL PRIMARY KEY on ALTER TABLE
    Syntax
    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
    
    
    Drop Primary Key

    You can drop a primary key in SQL Server using the ALTER TABLE statement.

    Syntax
    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name;
    
    Disable Primary Key

    You can disable a primary key using the ALTER TABLE statement in SQL Server.

    Syntax
    ALTER INDEX constraint_name ON table_name
    DISABLE;
    
    Enable Primary Key

    You can enable a primary key using the ALTER INDEX statement in SQL Server

    Syntax
    ALTER INDEX constraint_name ON table_name
    REBUILD;
    

    Connection String

    SQL Server Connection String Formats

    posted on 2019-08-27 05:53:53 - SQL Server Tutorials


    UNION

    union in SQL server

    posted on 2019-08-09 23:05:33 - SQL Server Tutorials


    System Functions

    System Functions in SQL Server

    posted on 2019-08-09 06:14:33 - SQL Server Tutorials


    Prompt Examples

    ChatGPT Prompt Examples

    posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


    Use Cases

    Chat GPT Key Use Cases

    posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


    Prompt Frameworks

    Prompt Frameworks

    posted on 2023-06-21 19:33:06 - ChatGPT Tutorials