SQL - Data Definiton
SQL - Data Definiton
Language (DDL)
SECD2523 Database
Semester 1 2020/2021
Learning Objective
At the end of this module, students should be able to:
• Perform basic operations in a DBMS software.
• Construct SQL statements to:
• Create and delete tables
• Perform changes to table structure
• Include integrity constraints to tables
10-Nov-21 2
Introduction to DBMS
• DBMS:
• A software system that enables users to define, create, and maintain the database and that
provides controlled access to this database.
• DBMS perform the tasks through commands (a.k.a queries) written in Structured Query Language
(SQL)
10-Nov-21 3
Structured Query Language
• A database language that allows users to:
• Create database and relation structures.
• Perform basic data management tasks (insertion, modification and deletion of data from relations)
• Perform simple and complex queries.
• Two major components:
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
10-Nov-21 4
Data Definition Language (DDL)
• Definition:
• A language that allows DBA or user to describe and name the entities, attributes, and relationships
required for the application, together with any associated integrity and security constraints.
• DDL allows database objects (schemas, domains, tables, views, etc) to be created,
modified or deleted. Examples of DDL statements:
• CREATE SCHEMA … DROP SCHEMA …
• CREATE TABLE … DROP TABLE …
• ALTER TABLE …
10-Nov-21 5
Integrity Enhancement Feature
• SQL provides some facilities for integrity control to protect the database from becoming
inconsistent.
• Required Data
• Using the NOT NULL constraint for column to ensure the column must contain a valid value.
• Entity Integrity
• Using PRIMARY KEY constraint for a column with unique, non-null value. Only ONE primary key per table.
• Use UNIQUE constraint for column with unique values. Allow to be NULL but can assign to multiple columns.
• Referential Integrity
• Using FOREIGN KEY constraint to link to parent table that containing the matching attribute. A table can have
multiple foreign keys.
10-Nov-21 6
Dealing with Tables
• Define database structure and controlling access to data
1. CREATE TABLE:
• to create table.
2. ALTER TABLE:
• to modify the structure of the existing tables.
3. DROP TABLE:
• to delete the existing tables.
10-Nov-21 7
Creating Tables
• General SQL syntax for creating a table:
10-Nov-21 8
CREATE TABLE
• Creating a table without constraint.
CREATE TABLE tableName (
columnName dataType [ DEFAULT value ]
[, column2Name datatype [ DEFAULT value]]);
[ ] : optional
10-Nov-21 9
CREATE TABLE
• Creating a table with constraints.
• Constraints at column level.
CREATE TABLE tableName
(columnName dataType [CONSTRAINT constraintName] constraintType
[DEFAULT value]
[, column2Name datatype [CONSTRAINT constraintName] constraintType [
DEFAULT value]]);
[ ] : optional
10-Nov-21 10
CREATE TABLE
• Creating a table with constraints.
• Constraints at table level.
CREATE TABLE tableName
(columnName dataType [DEFAULT value]
[, column2Name datatype[ DEFAULT value]]
[, CONSTRAINT constraintName constraintType (columnName,…)]);
[ ] : optional
10-Nov-21 11
Common Data Type
• Common Data Type (in Oracle)
• Characters or String: CHAR(size), VARCHAR(size)
• Dates: DATE
• Numeric:
• Integer: INTEGER, NUMBER(p) p: precision
• Fixed number: NUMBER(p,s) p: precision, s: scale
• Floating-number: NUMBER, FLOAT
10-Nov-21 12
Common SQL Data Type
• Overview
10-Nov-21 13
Constraints
• Types of constraints:
• PRIMARY KEY
• FOREIGN KEY
• UNIQUE
• Ensure all data values stored in the column are unique.
• Differ from PRIMARY KEY since it allows NULL values.
• CHECK
• Checks a specific condition during the execution of the query.
• NOT NULL
10-Nov-21 14
Examples (CREATE TABLE)
• Given the structure of the Department table as follows:
Department Employees
Attribute Datatype Attribute Datatype
10-Nov-21 15
Examples (CREATE TABLE)
• Create table without constraint:
CREATE TABLE Department (
deptNo NUMBER(5),
deptName VARCHAR(20),
address VARCHAR(30),
city VARCHAR(15)
);
CREATE TABLE Employees (
empID NUMBER,
fName VARCHAR(10),
lName VARCHAR(20),
deptNo NUMBER(5)
);
• In this case, constraints (e.g.: primary key) can be added using ALTER TABLE command.
10-Nov-21 16
Examples (CREATE TABLE)
• Create table with constraints:
CREATE TABLE Department (
deptNo NUMBER(5) PRIMARY KEY,
deptName VARCHAR(20) NOT NULL,
address VARCHAR(30),
city VARCHAR(15)
);
10-Nov-21 17
Altering Tables
• To change the structure of a table once it has been created.
• General Syntax:
ALTER TABLE tableName Add new column
ADD columnName dataType );
Add new constraint
ALTER TABLE tableName
ADD CONSTRAINT constraintName constraintType(columnName);
10-Nov-21 18
Altering Tables
• General syntax for altering tables
10-Nov-21 19
ALTER TABLE - Examples
• Add new column “Address” into Employee table.
ALTER TABLE Employee
ADD Address VARCHAR(40);
• Change datatype of “Address” to VARCHAR(20) and set the column as NOT NULL
ALTER TABLE Employee
MODIFY Address VARCHAR(20) NOT NULL;
10-Nov-21 20
ALTER TABLE - Examples
• Adding constraint after a table has been created.
• Set the attribute of “EmployeeID” as the PRIMARY KEY for the Employee table.
10-Nov-21 21
Examples (ALTER TABLE)
• If both tables are created without constraint.
• Constraints must be added using ALTER TABLE
Department Employees
Attribute Datatype Attribute Datatype
10-Nov-21 22
Examples (ALTER TABLE)
• For Department table, primary key and the NOT NULL constraint for deptName attribute
ALTER TABLE Department
ADD CONSTRAINT pk_Department PRIMARY KEY(deptNo)
MODIFY deptName VARCHAR(20) NOT NULL;
• For Employees table, primary key, NOT NULL constraints and foreign key to be added.
10-Nov-21 23
Deleting Table
• General Syntax:
DROP TABLE tableName;
10-Nov-21 24
Restore Dropped Table
• Dropped tables are actually placed in a recycle bin and can be restored.
• Syntax:
10-Nov-21 25
Other Useful Statements
• Viewing list of tables in a user schema:
• USER_TABLES: oracle’s data dictionary object that stores information of tables created
under a specific user (schema)
• To view the list of tables:
• Supported in Oracle.
10-Nov-21 26
Other Useful Statements
• Viewing table structures using DESC or DESCRIBE.
DESCRIBE tableName;
• Syntax:
• Example: View the table structure of Employee table.
DESCRIBE Employee;
10-Nov-21 27
Other Useful Statements
• Create table from existing database tables, using the AS clause and subqueries
• Syntax:
10-Nov-21 28
Exercise
10-Nov-21 29