SQL Tutorial - PRIMARY KEY CONSTRAINTS

Поделиться
HTML-код
  • Опубликовано: 25 июл 2024
  • Another fantastic SQL Tutorial brought to you by BeardedDev.
    In this video we look at what Primary Keys are and how to add Primary Keys within Create Table statements or Alter Table statements: • SQL Tutorial - PRIMARY...
    If you are new to primary keys this tutorial will help you know how to add primary keys by using object explorer and tsql.
    If you want to know what a composite primary key is watch this video: • SQL Tutorial - PRIMARY...
    If you are interested in learning how to add constraints to tables in SQL Server check out this link: • SQL Tutorial - Create ...
    For more great instructional videos on data development or business intelligence click here to see all the videos available on my channel: / @beardeddevdata
    SQL:
    IF OBJECT_ID(N'dbo.Customers', N'U') IS NOT NULL
    DROP TABLE dbo.Customers;
    GO
    CREATE TABLE dbo.Customers
    (
    CustomerId INT IDENTITY(1, 1) NOT NULL
    , FirstName NVARCHAR(50) NOT NULL
    , MiddleName NVARCHAR(50) NOT NULL
    , LastName NVARCHAR(50) NOT NULL
    , DOB DATE NULL
    , Email NVARCHAR(100) NULL
    , HomeTel VARCHAR(20) NULL
    , MobileTel VARCHAR(20) NULL
    , [1stLineAddress] VARCHAR(100) NULL
    , City INT NULL
    , County INT NULL
    , Country INT NULL
    )
    IF OBJECT_ID(N'dbo.Customers', N'U') IS NOT NULL
    DROP TABLE dbo.Customers;
    GO
    CREATE TABLE dbo.Customers
    (
    CustomerId INT IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Customers_CustomerID PRIMARY KEY (CustomerId)
    , FirstName NVARCHAR(50) NOT NULL
    , MiddleName NVARCHAR(50) NOT NULL
    , LastName NVARCHAR(50) NOT NULL
    , DOB DATE NULL
    , Email NVARCHAR(100) NULL
    , HomeTel VARCHAR(20) NULL
    , MobileTel VARCHAR(20) NULL
    , [1stLineAddress] VARCHAR(100) NULL
    , City INT NULL
    , County INT NULL
    , Country INT NULL
    )
    ALTER TABLE dbo.Customers
    ADD CONSTRAINT PK_Customers_CustomerId PRIMARY KEY (CustomerId)
    SELECT * FROM sys.key_constraints
    Don't forget to subscribe to the channel and let me know your thoughts in the comments section below.
  • НаукаНаука

Комментарии • 9

  • @carlosnvlr
    @carlosnvlr 3 года назад +3

    This is THE BEST explanation vid I have been able to find, and you even demonstrate it with examples! I really understand so much better!
    Thank you so much!!!

  • @user-rn7ng8fc2v
    @user-rn7ng8fc2v 2 года назад +1

    Ty dude

  • @AyyeeeProductions
    @AyyeeeProductions Год назад +1

    thank you!!!!!!!!!!!!!!

  • @sureshbehera3077
    @sureshbehera3077 5 лет назад +1

    Why a table content a single primary key

    • @BeardedDevData
      @BeardedDevData  5 лет назад

      Hi, can you please give more detail, I'm not sure I understand your question

    • @sureshbehera3077
      @sureshbehera3077 5 лет назад +1

      @@BeardedDevData i faced this question in a interview,a table content only one primary key right ? So they asked why a table can allow only create a only one primary key what is the reson behind it.?

    • @BeardedDevData
      @BeardedDevData  5 лет назад +1

      Hi, when you create a primary key on a table a clustered index is created and that determines how data is stored. As you can only store the table one way you can only have one primary key.