Showing posts with label Foreign Key. Show all posts
Showing posts with label Foreign Key. Show all posts

Tuesday, 24 March 2015

Difference Between Primary Key and Foreign Key in SQL


Difference Between Primary Key and Foreign Key


Set Primary Key,Unique Key,Foreign Key Sql Server Database Table in Visual Studio

Set Primary Key,Unique Key,Foreign Key


Set to Primary Key ,Foreign Key , Unique Key Sql Server DataBase Table Fields  Using Visual Studio


DEMO



Primary Key


     CREATE TABLE [dbo].[country] (
    [id]      INT          IDENTITY (1, 1) NOT NULL,
    [name]    VARCHAR (50) NOT NULL,
    [country] VARCHAR (50) NOT NULL,
    [amount]  VARCHAR (50) NOT NULL,
    
   CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED ([id] ASC),
   
  );


Unique Key


CREATE TABLE [dbo].[country] (

    [id]      INT          IDENTITY (1, 1) NOT NULL,
    [name]    VARCHAR (50) NOT NULL,
    [country] VARCHAR (50) NOT NULL,
    [amount]  VARCHAR (50) NOT NULL,

    CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED ([id] ASC),

    UNIQUE NONCLUSTERED ([name] ASC)

);


Foreign Key



CREATE TABLE [dbo].[register] (

    [username] VARCHAR (50) NOT NULL,
    [password] VARCHAR (50) NOT NULL,
    [country]  VARCHAR (50) NOT NULL,
    [name]     VARCHAR (50) NOT NULL,

    CONSTRAINT [FK_reg_country] FOREIGN KEY ([name]) REFERENCES [dbo].[country] ([name])

);