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: [ Ссылка ]
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: [ Ссылка ]
If you are interested in learning how to add constraints to tables in SQL Server check out this link: [ Ссылка ]
For more great instructional videos on data development or business intelligence click here to see all the videos available on my channel: [ Ссылка ]
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.
SQL Tutorial - PRIMARY KEY CONSTRAINTS
Теги
primary keysql primary keyprimary key in databasemysql primary keycomposite keyprimary in dbmsprimary key and foreign keysql create table primary keycomposite primary keycreate table primary keyprimary key constraintcreate primary key sql serversql keyprimary key foreign keysql server add primary keyprimary key exampledatabase designsql add primary keysql server create table primary keyprimary key and unique key