SQL XP 05
SQL XP 05
Objectives
In this lesson, you will learn to:
Create tables
Insert rows into tables
Delete tables
Create user-defined datatype
List various types of data integrity
Add the following constraints to tables:
PRIMARY KEY
UNIQUE
FOREIGN KEY
CHECK
DEFAULT
Creating Tables and Enforcing Data Integrity
Task List
Draft the statement to create a table
Create the table in the database
Check whether the table has been created
Insert a row into the table
Creating Tables and Enforcing Data Integrity
Result:
The name of the table to be created is Newspaper
Creating Tables and Enforcing Data Integrity
Task List
Draft the statement to remove a table from the database
Remove the Newspaper table from the database
Verify that the table has been removed from the database
Creating Tables and Enforcing Data Integrity
Just a Minute…
Draft the statement to remove the College table from the
database.
Creating Tables and Enforcing Data Integrity
User-Defined Datatypes
Creating a User-Defined Datatype
A user-defined datatype is created using the sp_addtype
system stored procedure
Syntax
sp_addtype name, [system_data_type]
[, 'null_type']
Creating Tables and Enforcing Data Integrity
Task List
Identify the inconsistency in the table structures
Identify the system-defined datatype that can be converted
to a user-defined datatype
Identify the name for the user-defined datatype
Create a user-defined datatype
Verify that the datatype has been created
Create the NewsAd table with the new datatype
Creating Tables and Enforcing Data Integrity
Data Integrity
Data integrity ensures the consistency and correctness of
data stored in a database. It is broadly classified into the
following four categories:
Entity integrity
Domain integrity
Referential integrity
User-defined integrity
Entity Integrity
Ensures that each row can be uniquely identified by an
attribute called the primary key
Creating Tables and Enforcing Data Integrity
Just a Minute...
Which integrity ensures that the values in the foreign key
match with the value of the corresponding primary key?
Creating Tables and Enforcing Data Integrity
Creating Constraints
Constraints are created to ensure data integrity
Constraints define rules that must be followed to maintain
consistency and correctness of data
A constraint can either be created at the time of creating a
table or can be added later
Constraints can be enforced at two levels:
Column level
Table level
Creating Tables and Enforcing Data Integrity
Just a Minute…
Which constraint enforces domain integrity by restricting the
value to be inserted in a column?
Creating Tables and Enforcing Data Integrity
Task List
Identify how to enforce data integrity
Draft the statement to create a table
Create the table with constraints
Verify constraints by inserting data
Creating Tables and Enforcing Data Integrity
Summary
In this lesson, you learned that:
A table is a database object used to store data
A table can be created using the CREATE TABLE statement
The INSERT statement is used to insert data into the table
The DROP TABLE statement is used to delete the table
A user-defined datatype is created by a user and is based on
a system datatype
A user-defined datatype is created using the sp_addtype
system stored procedure
Creating Tables and Enforcing Data Integrity
Summary (Contd.)
A user-defined datatype can be dropped using the sp_droptype
system stored procedure
sp_help provides information about a database object or a user-
defined datatype
Data integrity ensures the completeness, accuracy, and
reliability of data contained in the database
Data integrity can be classified as entity integrity, domain
integrity, referential integrity, and user-defined integrity
Data integrity can be enforced through constraints
Constraints are rules that can be specified at either the table-
level or the column-level
Creating Tables and Enforcing Data Integrity
Summary (Contd.)
A constraint can be created using either the CREATE TABLE
or the ALTER TABLE statements
A constraint can be dropped with the ALTER TABLE
statement or by dropping the table
Constraints are classified as PRIMARY, FOREIGN, UNIQUE,
CHECK, and DEFAULT