Professional Documents
Culture Documents
Designing and
Implementing Tables
Module Overview
Designing Tables
Working with Schemas
Creating and Altering Tables
What is a Table?
Relational databases store data in tables (relation)
Normalizing Data
Normalization is a process
Second Form
Third Form
Primary Keys
Candidate key can be used to uniquely identify a row
surrogate keys
Foreign Keys
Foreign keys are references between tables
Foreign key in one table holds a candidate key for another table
dbo.backupset
dbo.restorehistory
dbo.sysjobhistory
What is a Schema?
Schemas are containers for objects such as
Tables
Stored Procedures
Functions
Types
Views
Creating Schemas
Schemas are created using the CREATE SCHEMA command
Schemas have owners
CREATE
CREATE SCHEMA
SCHEMA KnowledgeBase
KnowledgeBase
AUTHORIZATION
AUTHORIZATION Paul
Paul
CREATE
CREATE TABLE
TABLE Article
Article (ArticleID
(ArticleID int IDENTITY(1,1)
IDENTITY(1,1)
PRIMARY
PRIMARY KEY,
KEY,
ArticleContents
ArticleContents xml)
xml)
GRANT
GRANT SELECT
SELECT TO
TO Salespeople;
Salespeople;
Create a schema
Drop a schema
Creating Tables
Tables are created with the CREATE TABLE statement
Columns need to be specified
Nullability should be specified
Primary Key should be identified
CREATE
CREATE TABLE
TABLE PetStore.Owner
PetStore.Owner
( OwnerID
OwnerID int
int IDENTITY(1,1)
IDENTITY(1,1) NOT
NOT NULL
NULL PRIMARY
PRIMARY KEY,
KEY,
OwnerName
OwnerName nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
HairColor
HairColor nvarchar(10)
nvarchar(10) NULL
NULL
);
);
Dropping Tables
Tables are removed by the DROP TABLE statement
Referenced tables (via foreign keys) cannot be dropped
All permissions, constraints, indexes, and triggers are also
dropped
Stored procedures
Functions
DROP
DROP TABLE
TABLE PetStore.Owner;
PetStore.Owner;
GO
GO
Altering Tables
Tables are modified using the ALTER TABLE statement
ALTER TABLE retains permissions on the table
ALTER TABLE retains the data in the table
Add/Drop columns and constraints
Enable/Disable constraints and triggers
ALTER
ALTER
ADD
ADD
GO
GO
TABLE
TABLE PetStore.Owner
PetStore.Owner
PreferredName
PreferredName nvarchar(30)
nvarchar(30) NULL;
NULL;
ALTER
ALTER TABLE
TABLE PetStore.Owner
PetStore.Owner
DROP
DROP COLUMN
COLUMN PreferredName;
PreferredName;
GO
GO
Create tables
Alter tables
Drop tables
Temporary Tables
Session temporary tables are only visible to their creators in the
Deleted
CREATE
TABLE
when#Squares
all users referencing the table disconnect
CREATE
TABLE
#Squares
(
( Number
Number int
int PRIMARY
PRIMARY KEY,
KEY,
NumberSquared int
int
);
);
GO
GO
Computed Columns
Computed columns are derived from other columns or from
functions
CREATE
CREATE TABLE
TABLE PetStore.Pet
PetStore.Pet
(PetID
(PetID int
int IDENTITY(1,1)
IDENTITY(1,1) PRIMARY
PRIMARY KEY,
KEY,
PetName
PetName nvarchar(30)
nvarchar(30) NOT
NOT NULL,
NULL,
DateOfBirth
DateOfBirth date
date NOT
NOT NULL,
NULL,
YearOfBirth
YearOfBirth AS
AS DATEPART(year,DateOfBirth)
DATEPART(year,DateOfBirth) PERSISTED
PERSISTED
);
);
GO
GO
permits)
Logon information
Virtual machine
10776A-MIA-SQL1
User name
AdventureWorks\Administrator
Password
Pa$$w0rd
Lab Scenario
A business analyst from your organization has provided you
with a first-pass at a schema design for some new tables
being added to the MarketDev database. You need to
provide an improved schema design based on good design
practices and an appropriate level of normalization. The
business analyst was also confused about when data should
be nullable. You need to decide about nullability for each
column in your improved design.
The new tables need to be isolated in their own schema.
You need to create the required schema DirectMarketing.
The owner of the schema should be dbo.
When the schema has been created, if you have available
time, you need to create the tables that have been
designed.
Lab Review
When should a column be declared as nullable?
Could columns such as AddressLine1, AddressLine2,
PhoneNumber2, PhoneNumber3?