Tuesday, 11 October 2011

SQL PRIMARY KEY Constraint

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a database table.

Primary keys must contain unique values.

A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can have only ONE primary key.


SQL PRIMARY KEY Constraint on CREATE TABLE

The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is created:

MySQL:

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)

Note: In the example above there is only ONE PRIMARY KEY (pk_PersonID). However, the value of the pk_PersonID is made up of two columns (P_Id and LastName).


SQL PRIMARY KEY Constraint on ALTER TABLE

To create a PRIMARY KEY constraint on the "P_Id" column when the table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)

Note: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).


To DROP a PRIMARY KEY Constraint

To drop a PRIMARY KEY constraint, use the following SQL:

MySQL:

ALTER TABLE Persons
DROP PRIMARY KEY

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID


9 comments:

  1. Thanks for your marvelous posting! I definitely enjoyed
    reading it, you happen to be a great author.I will ensure that I bookmark your blog and will often come back someday.
    I want to encourage you to definitely continue your
    great job, have a nice afternoon!

    Also visit my weblog: minecraft

    ReplyDelete
  2. Very good website you have here but I was wondering if you knew of
    any message boards that cover the same topics discussed in this article?
    I'd really like to be a part of group where I can get suggestions from other experienced people that share the same interest.
    If you have any suggestions, please let me know. Cheers!

    Here is my blog ... Quest Bars

    ReplyDelete
  3. Hello, I enjoy reading through your article post.
    I wanted to write a little comment to support you.
    Quest Bars blogesaurus

    Also visit my webpage; quest bars click this

    ReplyDelete
  4. I have been surfing online more than 4 hours today, yet I never found any interesting article like
    yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the
    web will be much more useful than ever before.

    my site :: quest bars

    ReplyDelete
  5. It is appropriate time to make some plans for the
    future and it is time to be happy. I have read this post and if I could I desire to suggest you few interesting
    things or advice. Perhaps you could write next articles referring
    to this article. I wish to read even more things about it!


    Feel free to surf to my web site: quest protein bars

    ReplyDelete
  6. Hello! This is kind of off topic but I need some guidance from
    an established blog. Is it difficult to set up your own blog?

    I'm not very techincal but I can figure things out pretty
    fast. I'm thinking about setting up my own but I'm not sure where to begin. Do you have any ideas or
    suggestions? Appreciate it

    Also visit my blog: facebook quest protein bar

    ReplyDelete
  7. Saved as a favorite, I really like your blog!

    Here is my blog post; Diet Plans to Lose Weight

    ReplyDelete
  8. My brother suggested I may like this web site. He used
    to be totally right. This post actually made my day.
    You cann't consider just how a lot time I had spent for this information! Thank you!


    my web site ... Diet Plans for Women to Lose Weight

    ReplyDelete
  9. Nice post. I learn something totally new and challenging
    on sites I stumbleupon every day. It's always useful to read content from other authors and use
    something from their sites.

    Visit my page :: coconut oil hair

    ReplyDelete