SQL DDL - La Salle University

advertisement
SQL Data Definition
 Spread throughout chapter 7
7
A Data Dictionary for the CH06 Database
7
Table 6.3
Data Types
 Each attribute will be defined to have a particular
data type
7
 Data type selection is usually dictated by the nature
of the data and by the intended use
 Pay close attention to the expected use of attributes
for sorting and data retrieval purposes
Some Common SQL Data Types
7
Data Type
Format
Numeric
NUMBER(L,D)
INTEGER
SMALLINT
DECIMAL(L,D)
Character
CHAR(L)
VARCHAR(L)
Date
DATE
Data Definition Commands
 Creating Table Structures
7
CREATE TABLE <table name>(
<attribute1 name and attribute1 characteristics,
attribute2 name and attribute2 characteristics,
attribute3 name and attribute3 characteristics,
primary key designation,
foreign key designation and foreign key
requirements>);
DDL - Create Table
7
CREATE TABLE STUDENT
( SSN
FNAME
MIDINIT
LNAME
YEAR
MAJOR
CREDIT
GPA
HOMETOWN
BALANCE
CONSTRAINT STDPK
PRIMARY KEY (SSN)
CHAR(9) NOT NULL,
VARCHAR(15) NOT NULL,
CHAR,
VARCHAR(15) NOT NULL,
CHAR(2) NOT NULL DEFAULT ‘Fr’,
CHAR(4) DEFAULT ‘Und’,
INT DEFAULT 0,
DECIMAL(4,2),
VARCHAR(15),
DECIMAL(7,2) DEFAULT 0,
)
 Foreign Keys
7
DDL
CREATE TABLE enrollments
(classindex INT NOT NULL,
stdssn
CHAR(9) NOT NULL,
CONSTRAINT ENRLPK
PRIMARY KEY(CLASSINDEX,STDSSN),
CONSTRAINT ENRLSECT
FOREIGN KEY (CLASSINDEX) REFERENCES SECTION(INDEX)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT ENRLSTUD
FOREIGN KEY (STDSSN) REFERENCES STUDENT(SSN)
ON DELETE CASCADE
ON UPDATE CASCADE )
 Domains
CREATE DOMAIN ZIP_CODE_TYP AS CHAR(9);
Other SQL Constraints
 NOT NULL constraint

7
Ensures that a column does not accept nulls
 UNIQUE constraint

Ensures that all values in a column are unique
 DEFAULT constraint

Assigns a value to an attribute when a new row is added to a table
 CHECK constraint

Validates data when an attribute value is entered
Data Definition Commands
 SQL Integrity Constraints
7

Entity Integrity



PRIMARY KEY
NOT NULL and UNIQUE
Referential Integrity



FOREIGN KEY
ON DELETE
ON UPDATE
Domains
 Domain is a set of permissible values for an attribute.
 Defining a domain requires:
7

Name
 Data type
 Default value
 Domain constraint or condition
CREATE DOMAIN depttyp AS CHAR(4);
 Then can use in defining any attributes of that type –
one benefit, we can ensure that we define the PK and
FK the same way
 Syntax:
CREATE DOMAIN <domain name> AS <data type>
DEFAULT <value> CHECK <condition>
SQL Indexes
 When a primary key is declared, DBMS automatically
creates a unique index
7
 Often need additional indexes
 Using the CREATE INDEX command, SQL indexes can be
created on the basis of any selected attribute
SQL Indexes
7
CREATE INDEX P_CODEX
ON PRODUCT(P_CODE);
CREATE UNIQUE INDEX P_CODEX
ON PRODUCT(P_CODE);
 Composite index

Index based on two or more attributes
CREATE INDEX meetingX
ON SECTION ( Sem, Year, Time, Room);

Often used to prevent data duplication
DDL
7
 DROP SCHEMA
 DROP TABLE
 ALTER TABLE
Advanced Data Definition Commands
 All changes in the table structure are made by using the
ALTER command
7

Followed by a keyword that produces specific change

Three options are available

ADD

MODIFY

DROP
Advanced Data Management
Commands
7
 Changing Table Structures
ALTER TABLE <table name>
MODIFY (<column name> <new column
characteristics>);
ALTER TABLE <table name>
ADD (<column name> <new column characteristics>);
Changing a Column’s Data Type
 ALTER can be used to change data type
7
 Some RDBMSs (such as Oracle) do not permit
changes to data types unless the column to be
changed is empty
ALTER TABLE PRODUCT
MODIFY (V_CODE CHAR(5));
Changing a Column’s Data
Characteristics
 Use ALTER to change data characteristics
7
 If the column to be changed already contains data,
changes in the column’s characteristics are
permitted if those changes do not alter the data type

ALTER TABLE PRODUCT
MODIFY (P_PRICE DECIMAL(9,2));
Adding or Dropping a Column
 Use ALTER to add a column

7
Do not include the NOT NULL clause for new column
ALTER TABLE PRODUCT
ADD (P_SALECODE CHAR(1));
 Use ALTER to drop a column

Some RDBMSs impose restrictions on the deletion of an
attribute
ALTER TABLE Students
DROP COLUMN Year;
Advanced Data Management
Commands
 Primary and Foreign Key Designation
7
ALTER TABLE PRODUCT
ADD PRIMARY KEY (P_CODE);
ALTER TABLE PRODUCT
ADD FOREIGN KEY (V_CODE) REFERENCES
VENDOR;
ALTER TABLE PRODUCT
ADD PRIMARY KEY (P_CODE)
ADD FOREIGN KEY (V_CODE) REFERENCES
VENDOR;
Advanced Data Management
Commands
 Deleting a Table from the Database
7

DROP TABLE <table name>;
DROP TABLE PART;
Views
7
 A view is a virtual table - looks like it exists but doesn’t
 View has a name (like a table) and you can do many of
the same things to views as tables
 Content of a view is derived from the contents of some
real table(s). (“base tables”)

View is defined via a SELECT statement … see upcoming
 A user may see the view and think it is the actual DB
Example Views
7
 CREATE VIEW CS_STUD
AS SELECT
*
FROM STUDENT
WHERE MAJOR = ‘CS’;
 CREATE VIEW PRIV_STUD
AS SELECT
SSN,FNAME,LNAME,YEAR,MAJOR
FROM STUDENT
 CREATE VIEW ENROLL_STATS (DEPT,LARGE,SMALL,AVE,TOTAL)
AS SELECT DEPT, MAX(ENROLL),MIN(ENROLL),AVG(ENROLL),
SUM(ENROLL)
FROM SECTIONS
GROUP BY DEPT
Example Views
7
 CREATE VIEW STUD_ENROLL_INFO
AS SELECT
ENROLLMENTS.INDEX,STUDENT.SSN, FNAME,LNAME,
YEAR, MAJOR, GPA
FROM ENROLLMENTS,STUDENT
WHERE ENROLLMENTS.STUDENT = STUDENT.SSN;
Updates involving Views
7
 NOT ALL views are updatable (e.g. ENROLL_STATS,
STUD_ENROLL_INFO)
 If a given update is ambiguous as to what the user would
want, it is hard for the DBMS to decide for them
 Updatable:

a view based on a single table is updatable if the view includes
the PK
 Not Updateable:

views involving joins
 view involving grouping and aggregate functions
Advantages of Views
7
 (Some) Logical Independence
 Show user only the info they need to see
 Views can simplify the user’s interaction with DB
End SQL DDL
7
Download