SQL - Tables - Constraints
SQL - Tables - Constraints
1
Structured Query Language (SQL)
COMMIT Transaction Control Manages the changes made by DML statements. Changes to
ROLLBACK Language (TCL) the data can be grouped together into logical transactions.
SAVEPOINT
GRANT Data Control Gives or removes access rights to both the Oracle database
REVOKE Language (DCL) and the structures within it
3
SQL’s Data Definition Language (DDL)
• DDL statements define, modify and remove objects from data
dictionary tables that are maintained by the DBMS
4
Oracle’s Data Dictionary
• Data dictionary tables maintain data about the structure of databases
• Users cannot directly insert, update or delete information in the data dictionary
7
Database Objects
• Table: Basic unit of storage; stores data
8
Naming Conventions
• Must begin with a letter
• Must not duplicate the name of another object owned by the same user
10
Tables in the Oracle Database
• User Tables
• Data Dictionary
11
Data Dictionary
• Below are two categories of data dictionary view. Each category has a
distinct prefix which reflects their intended use.
Prefix Description
USER_ These views contain information about objects owned by the user
ALL_ These views contain information about all of the tables accessible to the user
12
Querying the Data Dictionary
• USER_TABLES
• USER_OBJECTS
• USER_CATALOG or CAT
13
Querying the Data Dictionary (cont’d)
SELECT *
FROM user_tables;
DATE Date and time values between January 1, 4712 B.C. and
December 31, 9999 A.D.
LONG Variable-length character data up to 2 gigabytes
15
Creating a Table by using a Subquery
CREATE TABLE table
[(column, column…)]
AS subquery;
• Match the number of specified columns to the number of subquery columns
CREATE TABLE Payments_bak (customernumber, dateofpayment, amountpaid) AS
SELECT customernumber, paymentdate, amount
FROM payments;
• Create structure of a table based on another table. The new table does not have all constraints
CREATE TABLE Payments_bak (customernumber, dateofpayment, amountpaid) AS
SELECT customernumber, paymentdate, amount
FROM payments
WHERE 0 =1;
17
Adding a Column
• You cannot specify where the column is to appear. The new column
becomes the last column
18
Modifying a Column
• You can change a column’s datatype, size, and default value
• A change to the default value affects only subsequent insertions to the
table
19
Dropping a Column
• The column may or may not contain data
• The table have at least one column remaining in it after it is altered
• Once a column is dropped, it cannot be recovered
20
Dropping a Table
• Any pending transactions are committed
• All indexes are dropped
• Any views and synonyms will remain but are invalid
21
Constraints Guidlines
• Constraint names - SYS_Cn format
• Create a constraint:
• At the same time as the table is created
• After the table has been created
22
What are constraints?
• Constraints enforce rules at the table level
column, …
[CONSTRAINT constraint_name] constraint_type
(column, …),
24
The PRIMARY KEY Constraint
• Primary Key constraints can be defined at the column level or table
level
25
The PRIMARY KEY Constraint (cont’d)
• The following two statements are equal.
27
Alter Table - Primary Key
• Use Alter Table Statement if table already exists.
• When a constraint is added, all existing data are verified for violations.
• ON DELETE CASCADE indicates that when the row in the parent table is
deleted, the dependent rows in the child table will also be deleted
• ON DELETE SET NULL converts foreign key values to null when the
parent value is removed
29
Foreign Key Constraint
• The following two statements are equal.
CREATE TABLE customer (
CustNum NUMBER,
Cust_Name VARCHAR(50),
City VARCHAR(50),
RepNum NUMBER,
CONSTRAINT pk_customer PRIMARY KEY(CustNum),
CONSTRAINT fk_customer_repnum FOREIGN KEY(RepNum) REFERENCES Rep(RepNum));
• Above statement will validate existing column values. The statement will fail if there are any values that
do not exist in the Primary Key of related table.
• UNIQUE key constraints allow the input of nulls unless you also
define NOT NULL constraints for the same column(s)
32
Unique Key Constraint Example
• The following two statements are equal.
CREATE TABLE customer (
CustNum NUMBER,
Cust_Name VARCHAR(50),
City VARCHAR(50),
RepNum NUMBER,
CONSTRAINT pk_customer PRIMARY KEY(CustNum),
CONSTRAINT fk_customer_repnum FOREIGN KEY(RepNum) REFERENCES Rep(RepNum) ,
CONSTRAINT uk_custName UNIQUE(cust_name));
34
Check Constraint Example
• The following two statements are equal.
CREATE TABLE customer (
CustNum INTEGER,
Cust_Name VARCHAR(50),
City VARCHAR(50),
RepNum INTEGER,
CONSTRAINT pk_customer PRIMARY KEY(CustNum),
CONSTRAINT fk_customer_repnum FOREIGN KEY(RepNum) REFERENCES Rep(RepNum),
CONSTRAINT uk_custName UNIQUE(cust_name),
CONSTRAINT ck_customer_city CHECK(City IN ('Toronto', 'Ottawa')));
36
The DEFAULT Constraint
• Specify a default value for a column during an insert
• The default data type must match the column datatype
CREATE TABLE customer (
CustNum NUMBER, Cust_Name VARCHAR(50),
City VARCHAR(50) DEFAULT 'Toronto',
RepNum NUMBER,
CONSTRAINT pk_customer PRIMARY KEY(CustNum),
CONSTRAINT fk_customer_repnum FOREIGN KEY(RepNum) REFERENCES Rep(RepNum),
CONSTRAINT uk_custName UNIQUE(cust_name),
CONSTRAINT ck_customer_city CHECK(City IN ('Toronto', 'Ottawa')));