Lecture 4
Constraint - View
Objectives
• Constraint
• View
• Reference: Chapter 3 & 5
Faculty of Science and Engineering Database Management System 2
Integrity Constraints
• Data integrity is normally enforced in a database system
by a series of integrity constraints or rules.
• Entity integrity:
• Primary key
• Unique key
• Domain integrity:
• Default
• Check
• Reference integrity:
• Foreign key
Faculty of Science and Engineering Database Management System 3
SQL Server constraints
• Primary key: specify fields that uniquely identify
each record in the table and not null.
• Syntax:
CONSTRAINT <constraint_name>
PRIMARY KEY (<column_name>)
• Example:
Constraint pk_StuID Primary key (StuID)
Faculty of Science and Engineering Database Management System 4
SQL Server constraints (2)
• UNIQUE constraints allow SQL Server
administrators to specify that a column may not
contain duplicate values.
• Syntax:
CONSTRAINT <constraint_name>
UNIQUE (<column_name>)
• Example:
Constraint uk_Name2 Unique (Name2)
Faculty of Science and Engineering Database Management System 5
SQL Server constraints (3)
• Foreign key: fields in a relational database table
that match the primary key column of another table.
• Syntax:
CONSTRAINT <constraint_name>
FOREIGN KEY (<column_name>)
REFERENCES <table_name>(<column_name>)
• Example:
Constraint fk_StuID Foreign key (StuID)
References Students(StuID)
Faculty of Science and Engineering Database Management System 6
SQL Server constraints (4)
• CHECK constraints allow you to limit the types of
data that users may insert in a database.
• Syntax:
CONSTRAINT <constraint_name>
CHECK (<logical_expression>)
• Example:
Constraint ck_Age check Age >= 18 and Age <=60
Faculty of Science and Engineering Database Management System 7
SQL Server constraints (5)
• DEFAULT constraints allow you to specify a value
that the database will use to populate fields that
are left blank in the input source.
• Syntax:
CONSTRAINT <constraint_name>
DEFAULT (<constant_expression>)
• Example:
Constraint df_OrderDate
Default (Getdate())
Faculty of Science and Engineering Database Management System 8
SQL Server constraints (6)
• Allow you to specify that a column may not
contain NULL values or not.
• Syntax:
Column_name <data_type> [Null | Not Null]
• Example:
FirstName vahchar(10) Not Null
Faculty of Science and Engineering Database Management System 9
Uniqueidentifier
• A column or local variable of uniqueidentifier data
type can be initialized to a value in the following
ways:
• By using the NEWID function.
• By converting from a string constant in the form
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which
each x is a hexadecimal digit in the range 0-9 or a-
f.
For example, 6F9619FF-8B86-D011-B42D-
00C04FC964FF is a valid uniqueidentifier value.
Faculty of Science and Engineering Database Management System 10
Using NewID()
• In Create table statement
CREATE TABLE Customer
( CustomerID Uniqueidentifier NOT NULL DEFAULT newid(),
…)
INSERT Customer (CustomerID, …)
VALUES (NEWID(), …)
• In a variable
DECLARE @myid uniqueidentifier
SET @myid = NEWID()
PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)
Result:
Value of @myid is: 6F9619FF-8B86-D011-B42D-00C04FC964FF
Faculty of Science and Engineering Database Management System 11
Datetime data type
• DATETIME: YYYY-MM-DD hh:mm:ss:nnn. It has a range
of 1753-01-01 [Link] through 9999-12-31
[Link](8 bytes)
• SMALLDATETIME: YYYY-MM-DD hh:mm:ss:nnn. It has a
range of 1900-01-01 [Link] through 2079-06-01
[Link](4 bytes)
Faculty of Science and Engineering Database Management System 12
Datetime data type (2)
In SQL Server 2008
• DATE: YYYY-MM-DD. It has a range of 0001-01-01 through 9999-12-
31 (3 bytes)
• TIME: hh:mm:[Link], with a range of [Link].0000000 through
[Link] and is accurate to 100 nanoseconds (3 to 5 bytes)
• DATETIME2: similar to the older DATETIME data type, but has a
greater range and precision. The format is YYYY-MM-DD
hh:mm:ss:nnnnnnnm with a range of 0001-01-01 [Link].0000000
through 9999-12-31 [Link].9999999 ( 6 to 8 bytes)
• DATETIMEOFFSET: similar to DATETIME2, but includes additional
information to track the time zone. The format is YYYY-MM-DD
hh:mm:ss[.nnnnnnn] [+|-]hh:mm with a range of 0001-01-01
[Link].0000000 through 0001-01-01 [Link].0000000 through
9999-12-31 [Link].9999999 (8 to 10 bytes)
Faculty of Science and Engineering Database Management System 13
Views in SQL
• A view is a “virtual” table that is derived from
other tables
• Allows for limited update operations
• Since the table may not physically be stored
• Allows full query operations
• A convenience for expressing certain operations
Faculty of Science and Engineering Database Management System 14
Specification of Views
• SQL command: CREATE VIEW
• a table (view) name
• a possible list of attribute names (for example,
when arithmetic operations are specified or when
we want the names to be different from the
attributes in the base relations)
• a query to specify the table contents
Faculty of Science and Engineering Database Management System 15
SQL Views: An Example
• Specify a different WORKS_ON table
CREATE VIEW WORKS_ON_NEW AS
SELECT FNAME, LNAME, PNAME, sum(HOURS)
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE SSN=ESSN AND PNO=PNUMBER
GROUP BY FNAME, LNAME, PNAME;
Faculty of Science and Engineering Database Management System 16
Using a Virtual Table
• We can specify SQL queries on a newly create
table (view):
SELECT FNAME, LNAME
FROM WORKS_ON_NEW
WHERE PNAME=‘Seena’;
• When no longer needed, a view can be dropped:
DROP WORKS_ON_NEW;
Faculty of Science and Engineering Database Management System 17
Advantages of using views
• Restrict data access and/or simplify data access.
• Simplify data manipulation.
• Import and export data.
• Merge data.
Faculty of Science and Engineering Database Management System 18
Disadvantage of view
• Update restriction
• Structure restriction (new attribute in the base
table)
• Performance (complex views)
Faculty of Science and Engineering Database Management System 19
Update Views
• Update on a single view without aggregate
operations:
• Update may map to an update on the underlying
base table
• Views involving joins:
• An update may map to an update on the underlying
base relations
• Not always possible
Faculty of Science and Engineering Database Management System 20
Un-updatable Views
• Views defined using groups and aggregate
functions are not updateable
• Views defined on multiple tables using joins are
generally not updateable
• WITH CHECK OPTION: must be added to the
definition of a view if the view is to be updated
• To allow check for updatability and to plan for an
execution strategy
Faculty of Science and Engineering Database Management System 21
View can not
• Include ORDER BY clause, unless there is also a TOP
clause (remember that a view is nothing else but a virtual
table, why would we want to order it?)
• Include the INTO keyword.
• Include COMPUTE or COMPUTE BY clauses.
• Reference a temporary table or a table variable.
Faculty of Science and Engineering Database Management System 22
SQL Server – View statements
• Create view <view_name> As
<select_statement>
[with check option]
• Alter view <view_name> As
<select_statement>
• Drop view <view_name>
• Sp_rename <old_viewname>, <new_viewname>
• Sp_helptext <view_name>
Faculty of Science and Engineering Database Management System 23
Q&A
Faculty of Science and Engineering Database Management System 24