This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
CS 338: Computer Applications in Business: Databases (Fall 2014)
DBMS must prevent the entry of incorrect data to preserve the quality of the information stored
7
A database is only good as the data stored in it …A database is only good as the data stored in it …
Â
Using integrity constraints (ICs): • a condition that is specified on a database schema and• restricts data that can be stored in an instance of the database
• DBMS enforces ICs, that is, it only permits legal instances to be stored in a database
How?How?
Review Referential Constraints…
• Sometimes data stored in one relation is linked to data stored in another relation• If one relation is modified, the other must be checked, and
perhaps modified, to keep data consistent
8
What is What is
Â
Using Foreign Keys
How to enforce data consistency in this case?How to enforce data consistency in this case?
5
CS 338: Computer Applications in Business: Databases (Fall 2014)
• it is non-procedural• you specify what information you require, rather than how to get it• In other words, SQL does not require you to specify the access methods to
the data• it is essentially free-format
• Parts of the statements do not have to be typed at particular locations on the screen
SQL is easy to learn …
13
Â
• can be used by DBAs, management, application developers, and other types of end-users
SQL can be used by a range of users …
• An ISO standard exists for SQL, making it both the formal and de facto standard language for relational databases
Basic SQLÆ Basic Guidelines for Writing SQL Statements
� More readable with indentation and lineation: � Each clause should begin on a new line� Start of a each clause should line up with start of other clauses� If clause has several parts, they should each appear on a
separate line and be indented under start of clause to show therelationship
� Use extended form of Backus Normal Form (BNF)notation� Upper case letters represent reserved words� Lower case letters represent user-defined words� | indicates a choice among alternativesÆ e.g. a | b | c� {} Curly braces indicate a required element� [] Square brackets indicate an optional element� An ellipsis (…) indicates optional repetition (0 or more)
17
Â
Basic SQLÆ Basic Guidelines for Writing SQL Statements
� Literals are constants used in SQL statements
� All non-numeric literals must be enclosed in singlequotes (e.g. ‘London’)
� All numeric literals must not be enclosed in quotes(e.g. 650.00).
18
Â
10
CS 338: Computer Applications in Business: Databases (Fall 2014)
• The CREATE TABLE command is used to specify a new relation • You need to 1. provide a table name,
2. specify attributes and initial constraints
CREATE TABLE
• Attributes are specified first and each attribute is given:1. a name, 2. a data type (to specify its domain of values), and3. Attribute constraints (i.e. NOT NULL)
Attributes …
• Key, entity integrity, and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared
• They can also be added later using the ALTER TABLE command
Initial constraints …
12
CS 338: Computer Applications in Business: Databases (Fall 2014)
1. SQL schema is implicitly specified in the environment in which the CREATE TABLE statements are executed
There are two methods for creating SQL Schema:
2. Explicitly attach the schema name to the relation name separated by a period
CREATE TABLE COMPANY.EMPLOYEE ...
Example
Â
The CREATE TABLE Command in SQL
24
• Different from Virtual relations
• Created through the CREATE VIEW statement
• May or may not correspond to an actual physical file
• Relations declared through CREATE TABLE statement arecalled base tables (or base relations)• relation and its tuples are actually created and stored as
a file by the DBMS
Â
13
CS 338: Computer Applications in Business: Databases (Fall 2014)
Specifying Key and Referential Integrity Constraints
37
• Specifies one or more attributes that make up the primary key of a relation
• Example: Dnumber INT PRIMARY KEY;
PRIMARY KEY clause
Keys and referential integrity constraints are very important• We use special clauses within the CREATE TABLE statement to specify
them
• Specifies alternate (secondary) keys
• Example: Dname VARCHAR(15) UNIQUE;
UNIQUE clause
Specifying Key and Referential Integrity Constraints
38
• Referential integrity constraints can be violated when tuples are inserted or deleted, or when a foreign key or primary key attribute value is modified
• Default operation: reject update on violation• Can always specify an alternative action by attaching a
referential triggered action clause• Options include SET NULL, CASCADE, and SET
DEFAULT
FOREIGN KEY clause
Keys and referential integrity constraints are very important• We use special clauses within the CREATE TABLE statement to specify
them
Â
Nathan Wilson
20
CS 338: Computer Applications in Business: Databases (Fall 2014)
Specifies that if an attempt is made to delete a row with a keyreferenced by foreign keys in existing rows in other tables, an error israised and the DELETE statement is rolled back
ON DELETE NO ACTION
Updating or deleting foreign keys• CREATE TABLE statement support ON DELETE and ON UPDATE
clauses[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ][ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
Specifies that if an attempt is made to update a key value in a rowwhose key is referenced by foreign keys in existing rows in othertables, an error is raised and the UPDATE statement is rolled back
ON UPDATE NO ACTION
Updating or Deleting Foreign Keys
40
Specifies that if an attempt is made to delete a row with a keyreferenced by foreign keys in existing rows in other tables, all rowsthat contain those foreign keys are also deleted
ON DELETE CASCADE
Specifies that if an attempt is made to update a key value in a row,where the key value is referenced by foreign keys in existing rows inother tables, all the values that make up the foreign key are alsoupdated to the new value specified for the key
ON UPDATE CASCADE
Â
.
.
.
21
CS 338: Computer Applications in Business: Databases (Fall 2014)
In addition to key and referential constraints, other tableconstraints can be specified using CHECK clauses at the end ofa CREATE TABLE statement• These are called tuple-based constraints
• Apply to each tuple individually
Add a new column called Dept_create_date which stores the date when the department was created. • Add a CHECK clause to make sure that a manager’s start
date is later than the department’s create date• CHECK (Dept_create_date <= Mgr_start_date);
Example
Practice Question(s)Do exercise 4.8 on page 112 (6th Edition)