DATA DEFINITION LANGUAGE (DDL)
CREATE
CREATE SCHEMA AUTHORISATION
Authentication: process the DBMS uses to verify that only registered users access the database - If using an enterprise RDBMS, you must be authenticated by the RDBMS - To be authenticated, you must log on to the RDBMS using an ID and password created by the database administrator - Every user ID is associated with a database schema
Schema: a logical group of database objects that are related to each other - A schema belongs to a single user or application - A single database can hold multiple schemas that belong to different users or applications - Enforce a level of security by allowing each user to only see the tables that belong to them
Syntax:
CREATE SCHEMA AUTHORIZATION {creator};
- Command must be issued by the user who owns the schema o Eg. If you log on as JONES, you can only use CREATE SCHEMA AUTHORIZATION JONES;
CREATE TABLE
Syntax:
CREATE TABLE table_name ( column1 data type [constraint], column2 data type [constraint], PRIMARY KEY(column1, column2), FOREIGN KEY(column2) REFERENCES table_name2; );
CREATE TABLE AS
You can create a new table based on selected columns and rows of an existing table. The new table will copy the attribute names, data characteristics and rows of the original table.
Example of creating a new table from components of another table:
CREATE TABLE project AS SELECT emp_proj_code AS proj_code emp_proj_name AS proj_name emp_proj_desc AS proj_description emp_proj_date AS proj_start_date emp_proj_man AS proj_manager FROM employee;
3
CONSTRAINTS
There are 2 types of constraints: - Column constraint – created with the column definition o Applies to a single column o Syntactically clearer and more meaningful o Can be expressed as a table constraint - Table constraint – created when you use the CONTRAINT keyword o Can apply to multiple columns in a table o Can be given a meaningful name and therefore modified by referencing its name o Cannot be expressed as a column constraint
NOT NULL
This constraint can only be a column constraint and cannot be named. It specifies that this column cannot hold NULL values.
Syntax: column_name data type NOT NULL,
PRIMARY KEY
Using the PRIMARY KEY constraint enforces entity integrity - Ensures that each entry has a unique data value and is not null in the primary key column
Syntax for column constraint:
PRIMARY KEY(column_name);
FOREIGN KEY
Using the FOREIGN KEY constraint enforces referential integrity using ON DELETE - ON DELETE CASCADE – if you delete the primary key record, the foreign key record is also deleted - ON DELETE SET NULL – if you delete the primary key record, the foreign key is set to NULL - ON DELETE NO ACTION – you can’t delete the primary key record if a foreign key record exists (checks integrity after deletion; default if ON DELETE is not specified) - ON DELETE RESTRICT – you can’t delete the primary key record if a foreign key record exists (checks integrity before deletion; not available in Oracle)
Syntax for column constraint:
FOREIGN KEY(column_name) REFERENCES table_name ON DELETE CASCADE
UNIQUE
Specifies that the column’s data value cannot be duplicated.
Syntax for column constraint: column_name data type UNQIUE,
4
Syntax for table constraint:
CONSTRAINT constraint_name UNIQUE(column1, column2)
DEFAULT
Defines a default value for a column
Example of a column constraint: emp_city VARCHAR2(255) DEFAULT ‘Melbourne’,
Example of a table constraint:
CONSTRAINT emp_city_dft DEFAULT ‘Melbourne’ FOR emp_city,
CHECK
Specifies the rules for values in the column
Example of a column constraint: emp_age SMALLINT CHECK(emp_age >= 18), emp_city VARCHAR(255) CHECK(emp_city IN ‘Melbourne’, ‘Geelong’, ‘Bendigo’),
Example of a table constraint:
CONSTRAINT inv_chk1 CHECK(inv_date > to_date(‘01-01-2019’, ‘dd-mm-yyyy’)
MULTIPLE CONTRAINTS
A column can have multiple constraints
Example: emp_postcode CHAR(4) DEFAULT ‘3160’ NOT NULL CHECK(emp_postcode IN ‘3160’, ‘3161’, ‘3162’),
ALTER AND DROP
DROP TABLE
The DROP TABLE command deletes a table permanently.
Syntax:
DROP TABLE table_name;
5
ALTER TABLE
The ALTER TABLE command is used to make changes to the table structure - It is followed by a keyword: ADD, MODIFY or DROP
Syntax:
ALTER TABLE table_name … …
ADD AND DROP COLUMN
Example of deleting a column:
ALTER TABLE Employee DROP COLUMN emp_age;
Example of adding a column:
ALTER TABLE Employee ADD(emp_dob DATE NOT NULL);
MODIFYING A COLUMN
You can change a column’s data type.
Example of modifying a column’s data type:
ALTER TABLE Employee MODIFY(emp_code CHAR(5));
However, you can’t change a column’s data type if it’s a foreign key and it has at least one record that is not null. This ensure referential integrity so that the primary and foreign key remains the same data type. In some cases, you could still change the data type if the existing data matches the new data type. Eg. Increasing the width of an attribute from 6 digits to 9 digits.
ADDING PRIMARY AND FOREIGN KEYS
If you created a table based on another table (using CREATE TABLE AS), the new table won’t include the primary and foreign key constraints. You can add them, using ALTER TABLE and ADD commands.
Example:
ALTER TABLE project ADD PRIMARY KEY(proj_code) ADD FOREIGN KEY(proj_manager) REFERENCES employee(emp_code);
6
DATABASE THEORY
CONTENTS Introduction to Databases ...... 1 The importance of databases ...... 1 Data à Information à Knowledge ...... 1 File system data processing ...... 1 Manual vs Computerised files systems ...... 1 Terminology ...... 2 Problems ...... 2 Database system ...... 3 Database management systems ...... 4 Types of databases ...... 5 The Relational Model ...... 6 Characteristics of a relational table ...... 6 Keys ...... 6 Dependencies ...... 6 Types of keys ...... 7 Integrity rules ...... 7 Relational algebra ...... 8 Relational Set operators ...... 8 Types of Joins ...... 9 The Database Life cycle ...... 10 Ansi/Sparc Database Architecture ...... 10 Terminology at different levels ...... 11 Conceptual Design ...... 12 Entity relationship modelling ...... 12 Attributes ...... 12 Types of attributes ...... 12 Relationships ...... 13 ERD Notations ...... 14 Chen’s Notation ...... 14 Crow’s Foot Notation ...... 15 Logical Design ...... 16 Relational Schema Notation ...... 16 Mapping conceptual model to Logical model ...... 16 Unified Modelling Language (UML) ...... 18 Normalisation ...... 19 First Normal Form (1NF) ...... 20 Second normal form (2NF) ...... 20 Third Normal Form (3NF) ...... 21 Attribute Synthesis ...... 21 Surrogate keys ...... 21 Improving the design ...... 22 SQL THEORY
CONTENTS
Introduction to SQL ...... 2 Data Types ...... 2 Data Definition Language (DDL) ...... 3 Create ...... 3 CREATE SCHEMA AUTHORISATION ...... 3 CREATE TABLE ...... 3 CREATE TABLE AS ...... 3 Constraints ...... 4 NOT NULL ...... 4 PRIMARY KEY ...... 4 FOREIGN KEY ...... 4 UNIQUE ...... 4 DEFAULT ...... 5 CHECK ...... 5 Multiple contraints ...... 5 ALTER AND DROP ...... 5 DROP TABLE ...... 5 ALTER TABLE ...... 6 ADD and DROP COLUMN ...... 6 Modifying a column ...... 6 Adding Primary and Foreign Keys ...... 6 Data Manipulation Language (DML) ...... 7 INSERT ...... 7 UPDATE ...... 7 DELETE ...... 8 SELECT ...... 8 WHERE ...... 8 ORDER BY ...... 8 COMMIT AND ROLLBACK ...... 9 Derived Attributes and Aliases ...... 9 Special Operators ...... 10 DISTINCT ...... 10 BETWEEN ...... 10 IS NULL ...... 10 LIKE ...... 10 IN ...... 11 EXISTS ...... 11 Comparison Operators ...... 12 Arithmetic operators ...... 12 Logical Operators ...... 13
Aggregate Functions ...... 13 COUNT ...... 13 MIN and MAX ...... 14 AVG ...... 14 SUM ...... 15 Grouping Data ...... 15 GROUP BY ...... 15 HAVING ...... 15 DDL and DML Summaries ...... 16 Advanced SQL ...... 18 Sequences ...... 18 JOIN ...... 19 CROSS JOIN ...... 19 NATURAL JOIN ...... 20 JOIN USING ...... 20 JOIN ON ...... 20 Outer Joins ...... 21 Subqueries ...... 22 WHERE Subqueries ...... 22 IN Subqueries ...... 22 HAVING Subqueries ...... 23 ANY and ALL Operators ...... 23 FROM Subqueries ...... 23 Column List Subqueries ...... 24 Correlated Subqueries ...... 24 Oracle Triggers ...... 24 SQL Functions ...... 26 Date and Time Functions ...... 26 Numeric Functions ...... 27 String Functions ...... 27 Conversion Functions ...... 28 Relational Set Operators ...... 29 UNION ...... 29 INTERSECT ...... 29 MINUS ...... 30 Views ...... 30 Transaction Management ...... 31 Transaction Properties ...... 32 Transaction Management ...... 32 Transaction Log ...... 32 Recovery ...... 33 Concurrency Control ...... 34 The Scheduler ...... 34 Locking Methods ...... 35
1
DATABASE INTERFACE THEORY
CONTENTS
Database Connectivity ...... 1 Native SQL Connectivity ...... 1 ODBC, DAO, and RDO ...... 1 How to use ODBC ...... 3 OLE-DB and ADO ...... 3 ADO.NET ...... 4 JDBC ...... 6 Database Internet Connectivity ...... 7 Web-to-Database Middleware ...... 7 Web server Interface ...... 7 Web Browser ...... 8 Client-side Extensions ...... 8 Web Application Servers ...... 9 Web Database Development ...... 9 ColdFusion ...... 9 PHP ...... 10 PHP Database Access ...... 11 Practical Considerations and Security ...... 11 Cloud Computing ...... 13 Cloud Implementations ...... 13 Characteristics of Cloud Services ...... 13 Type of Cloud Services ...... 14 Advantages and Disadvantages of Cloud Services ...... 14 SQL data Services ...... 15 Database Current and Future Trends ...... 16 Business Intelligence ...... 16 Big Data ...... 16 Fast Data Processing ...... 17