Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Good/Bad Practise with Primary/Foreign Keys

$
0
0

Hello,

I have a question about good and bad practise with Primary and Foreign keys. Is it good idea/practise to use second table for extra info about the person using firsts( Users ) table PK as PK and FK on second( Address ) table ? I am planing to create more tables like ADDRESS table.

Or should I avoid doing so ? If yes, why and what would be a better choice ?

CREATE TABLE Users.Users 
(
	UserID		INT	     NOT NULL IDENTITY(30,1),
	FirstName	NVARCHAR(50) NOT NULL,
	MiddleName	NVARCHAR(50) NULL,
	LastName	NVARCHAR(50) NOT NULL,
	DateOfBirth	DATE	     NULL,
	CreationDate	DATETIME2    NOT NULL DEFAULT(GETDATE()),
	SSMA_TimeStamp	TIMESTAMP    NOT NULL,
		CONSTRAINT PK_UserID PRIMARY KEY(UserID),
		CONSTRAINT CHK_DateOfBirth CHECK(DateOfBirth <= CURRENT_TIMESTAMP)
);

CREATE TABLE Users.Address
(
	UserID		INT	NOT NULL,
	Country		NVARCHAR(50) NULL,
	County		NVARCHAR(50) NULL,
	City		NVARCHAR(50) NULL,
	AddressLine1	NVARCHAR(100) NULL,
	AddressLine2	NVARCHAR(100) NULL,
	PostCode	NVARCHAR(15) NULL,
	CONSTRAINT PK_AddressUserID PRIMARY KEY(UserID),CONSTRAINT FK_UserID_UsersUserID FOREIGN KEY (UserID)REFERENCES Users.Users (UserID),

);




Viewing all articles
Browse latest Browse all 8428

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>