Wk3a - Sql-Review - Creating Database Objects
Wk3a - Sql-Review - Creating Database Objects
2
Week 3 Database Management II
What is SQL?
3
Week 3 Database Management II
Why Use SQL?
4
Week 3 Database Management II
Types of SQL
6
Week 3 Database Management II
What is DDL?
Data Definition Language helps you to define the database structure
or schema. Examples of DDL commands in SQL are:
CREATE:
CREATE statements is used to define the database structure schema:
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
For example:
Create database university;
Create table students (id int, name varchar, …);
Create view for_students AS (query);
7
Week 3 Database Management II
DDL (cont.)
DROP
Drops commands remove tables, views and databases from
RDBMS.
Syntax
DROP object_type object_name;
For example:
Drop view for_students;
Drop database university;
Drop table student;
8
Week 3 Database Management II
DDL (cont.)
ALTER
Alters command allows you to alter the structure of the database.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
9
Week 3 Database Management II
DDL (cont.)
TRUNCATE:
This command is used to delete all the rows from the table and
free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE table students;
10
Week 3 Database Management II
What is Data Manipulation Language?
11
Week 3 Database Management II
INSERT:
This command is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, .... valueN);
For example:
INSERT INTO students (StudID, FIrstName, LastName) VALUES
(2021098, 'Tom', Erichsen');
12
Week 3 Database Management II
UPDATE:
For example:
UPDATE students
SET FirstName = 'Jane', LastName= 'Nelson'
WHERE StudID = 2012909;
13
Week 3 Database Management II
DELETE:
For example:
DELETE FROM students
WHERE FirstName = 'Jane';
14
Week 3 Database Management II
Data Control Language
15
Week 3 Database Management II
Grant:
For example:
GRANT SELECT ON Users TO Tom@localhost;
16
Week 3 Database Management II
Revoke:
17
Week 3 Database Management II
Transaction Control Language
Transaction control language or TCL commands deal with the transaction
within the database.
Commit
This command is used to save all the transactions to the database.
Syntax:
Commit;
For example:
DELETE FROM Students
WHERE RollNo =25;
COMMIT;
18
Week 3 Database Management II
Transaction Control Language (cont)
Rollback
Rollback command allows you to undo transactions that have not
already been saved to the database.
Syntax:
ROLLBACK;
19
Week 3 Database Management II
Transaction Control Language (cont)
SAVEPOINT
This command helps you to sets a savepoint within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example:
SAVEPOINT RollNo;
20
Week 3 Database Management II
Data Query Language (DQL)
Data Query Language (DQL) is used to fetch the data from the database. It uses only one
command:
SELECT:
This command helps you to select the attribute based on the condition described by the
WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT FirstName
FROM Student
WHERE RollNo > 15;
21
Week 3 Database Management II
Creating a Database
23
Week 3 Database Management II
The SQL Data Definition Language
The SQL Data Definition Language is a set of statements that are
responsible for changing the structure of a database.
The core of the Data Definition Language is based on three SQL verbs;
CREATE, DROP and ALTER.
The Data Definition Language statements are used for the following tasks:
Defining and creating new tables.
Removing a table that is no longer needed.
Changing the definition of existing tables.
Defining a virtual table (or view) of data.
Establishing security controls for a database.
Building an index to make table access faster.
Controlling the physical storage of data by the DBMS.
24
Week 3 Database Management II
Table Definitions
26
Week 3 Database Management II
Creating a Table (CREATE TABLE)
The CREATE TABLE statement is used to define a new table in the database.
The various clauses of the statement define the elements of the table
definition. The basic syntax is as follows:
CREATE TABLE table_name (column_definition
(table-constraint_definition))
The columns in the column definition list are separated by commas and
enclosed in parentheses. The column definition is specified as follows:
column_name data type DEFAULT value NOT NULL
The table constraints definition is specified as follows:
CONSTRAINT constraint name
There are four different constraints that could be defined; primary-key,
foreign-key, uniqueness and check constraints.
27
Week 3 Database Management II
Constraints
The primary-key constraint is specified as follows:
PRIMARY KEY (column-name)
The foreign-key constraint is specified as follows:
FOREIGN KEY (column-name) REFERENCES table-name (column-
name) MATCH [FULL|PARTIAL] ON DELETE [CASCADE|SET NULL|SET
DEFAULT| NO ACTION] ON UPDATE [CASCADE|SET NULL|SET
DEFAULT|NO ACTION]
The uniqueness constraint is specified as follows:
UNIQUE (column-name)
The check constraint is specified as follows:
CHECK (search-condition)
28
Week 3 Database Management II
CREATE TABLE (cont)
29
Week 3 Database Management II
Example:
30
Week 3 Database Management II
Removing a Table (DROP TABLE)
Tables that are no longer needed in a database can be removed with
the DROP TABLE statement.
When the DROP TABLE statement removes a table from the database,
the table definition and all its contents are lost permanently.
The data in the table are not recoverable and a new CREATE TABLE
statement will be needed to recreate the table definition. The syntax for
the drop table statement is as follows:
DROP TABLE table_name [CASCADE|RESTRICT]
The CASCADE and RESTRICT options are requirements in the SQL2
standard to be included in a DROP TABLE statement. Most DBMS products
however accept the DROP TABLE statement without the options.
31
Week 3 Database Management II
Changing a Table Definition (ALTER TABLE)
Users might need to store additional information about the entities
represented in a database table after the table has been in use for
some time.
The ALTER TABLE statement can be used to change the structure of the
existing table.
The following changes can be made to a table using the ALTER TABLE
statement:
Adding a column definition to an existing table.
Dropping a column from an existing table.
Changing the default value for a column.
Adding or dropping a primary key or foreign key for an existing table.
Adding or dropping a uniqueness or check constraint for an existing table.
32
Week 3 Database Management II
The syntax for the ALTER TABLE statement
33
Week 3 Database Management II
Assertions
34
Week 3 Database Management II
Domains
35
Week 3 Database Management II
Indexes (CREATE/DROP INDEX)
Indexes are one of the physical storage structures provided by most SQL-based
DBMSs.
An index is a structure that provides rapid access to the rows of a table based on
the values of one or more columns of the table. The DBMS uses the index in a
similar way we use indexes in books.
The index basically stores data values and pointers to the rows where those data
values occur on a table.
Data values are arranged in ascending or descending order in an index for the
DBMS to quickly search the index to find a particular data value.
The DBMS follows the pointer to locate the row containing the value.
The existence of indexes is however transparent to SQL users accessing a table.
This is because SQL query statements do not indicate whether there are indexes
or not.
36
Week 3 Database Management II
Indexes Advantages
37
Week 3 Database Management II
Indexes Disadvantages
38
Week 3 Database Management II
CREATE INDEX Syntax
39
Week 3 Database Management II
Managing other Database Objects
The SQL Data Definition Language is centered around the CREATE,
DROP and ALTER verbs which are used to manipulate database objects
such as tables, views, indexes etc.
There are other database objects peculiar to different DBMS brands. The
DDL verbs are also used to form additional DDL statements for creating,
destroying and modifying these other database objects.
Examples of SQL DDL statements supported by Oracle for some of the
additional Database objects in Oracle are as follows:
CREATE/DROP CLUSTER: Used to manage cluster of tables for performance
tuning.
CREATE/DROP/ALTER FUNCTIONS: Used for user defined functions
CREATE/DROP/ALTER ROLE: Used to manage user roles within the database
40
Week 3 Database Management II
Summary
The CREATE DATABASE statement available in some DBMS products such as
MySQL, Sybase and Microsoft SQL Server.
The CREATE TABLE statement is used to create a table and define its columns,
primary key and foreign keys.
The DROP TABLE statement is used to remove permanently a table that has been
previously created.
The ALTER TABLE statement can be used to modify the structure of a table that
has been previously created.
Indexes speed up database queries.
The CREATE INDEX and DROP INDEX statements are used to define and remove
indexes in a database.
Most DBMS products support the CREATE, DROP and ALTER statements used for
managing other database objects available in various DBMS brands.
41
Week 3 Database Management II
Hands-On: Exercise
NAME DOB
43
Week 3 Database Management II
CAR
44
Week 3 Database Management II