0% found this document useful (0 votes)
4 views

Wk3a - Sql-Review - Creating Database Objects

Uploaded by

aikhomuremen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Wk3a - Sql-Review - Creating Database Objects

Uploaded by

aikhomuremen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

DATABASE MANAGEMENT II

Dr. Bilkisu L. Muhammad-Bello

Week 3 Database Management II


1
SQL - REVIEW

2
Week 3 Database Management II
What is SQL?

SQL is a database language designed for the retrieval and


management of data in a relational database.
SQL is the standard language for database management.
All the RDBMS systems like MySQL, MS Access, Oracle, Sybase,
Postgres, and SQL Server use SQL as their standard database
language.
SQL programming language uses various commands for different
operations. We will learn about the like DCL, TCL, DQL, DDL and
DML commands in SQL with examples.

3
Week 3 Database Management II
Why Use SQL?

It helps users to access data in the RDBMS system.


It helps you to describe the data.
It allows you to define the data in a database and manipulate that
specific data.
With the help of SQL commands in DBMS, you can create and
drop databases and tables.
SQL offers you to use the function in a database, create a view,
and stored procedure.
You can set permissions on tables, procedures, and views.

4
Week 3 Database Management II
Types of SQL

Data Definition Language


(DDL)
Data Manipulation Language
(DML)
Data Control Language(DCL)
Transaction Control
Language(TCL)
Data Query Language (DQL)

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;

To modify an existing column in the table:


ALTER TABLE MODIFY(COLUMN DEFINITION....);
For example:
Alter table syllabus add subject varchar;

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?

Data Manipulation Language (DML) allows you to modify the


database instance by inserting, modifying, and deleting its data. It
is responsible for performing all types of data modification in a
database.
There are three basic constructs which allow database program
and user to enter data and information are:
Here are some important DML commands in SQL:
INSERT
UPDATE
DELETE

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:

This command is used to update or modify the value of a column


in the table.
Syntax:
UPDATE table_name SET [column_name1=
value1,...column_nameN = valueN] [WHERE CONDITION]

For example:
UPDATE students
SET FirstName = 'Jane', LastName= 'Nelson'
WHERE StudID = 2012909;
13
Week 3 Database Management II
DELETE:

This command is used to remove one or more rows from a table.


Syntax:
DELETE FROM table_name [WHERE condition];

For example:
DELETE FROM students
WHERE FirstName = 'Jane';

14
Week 3 Database Management II
Data Control Language

DCL (Data Control Language) includes commands like GRANT


and REVOKE, which are useful to give “rights & permissions.”
Other permission controls parameters of the database system.
Examples of DCL commands:
Commands that come under DCL:
Grant
Revoke

15
Week 3 Database Management II
Grant:

This command is use to give user access privileges to a database.


Syntax:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER,
ANOTHER_USER;

For example:
GRANT SELECT ON Users TO Tom@localhost;

16
Week 3 Database Management II
Revoke:

It is sometimes useful to take back permissions from the user.


The REVOKE command handles this
Syntax:
REVOKE privilege_nameON object_nameFROM {user_name |
PUBLIC |role_name}
For example:
REVOKE SELECT, UPDATE ON student FROM BCA, MCA;

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

Week 3 Database Management II


22
Introduction
In practice, many SQL users do not have to worry about creating a database
as they use interactive SQL or programmatic SQL to access a database.
The database administrator is responsible for creating databases and giving
permissions for users to retrieve or update stored data in typical corporate
databases.
As such, SQL users in typical corporate databases are not given permission by
the database administrator to create a new database or modify the structure of
the existing tables.
It is however important that you know how to create a database and database
objects because as you get more comfortable with SQL, you might want to
create your own private tables or a personal database for yourself.
You’ll learn to create a database, tables and also define the database structure.

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

A table is a database object used to store data.


The table is the most important structure in relational databases.
In multi-user databases, tables are mostly created by the
database administrator and then used by the users.
Users often find it more convenient to define their own tables to
store personal data or data extracted from other tables.
These tables may last for just a single interactive SQL session or
may last for weeks or even months.

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)

When a CREATE TABLE statement is executed by a user, that user


becomes the owner of the newly created table.
The table name must be a valid SQL name and must not conflict
with the name of any of the existing tables in the database.
The newly created table is empty but the INSERT statement can
be used to insert data into the table.

29
Week 3 Database Management II
Example:

CREATE TABLE Orders(O_Id INTEGER NOT NULL,


OrderDate DATE NOT NULL,
OrderPrice MONEY NOT NULL,
Customer VARCHAR(10) NOT
NULL,
PRIMARY KEY (O_Id))

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

ALTER TABLE table-name ADD column-definition


ALTER column-name SET|DROP DEFAULT value
DROP column-name CASCADE|RESTRICT
ADD [primary-key|foreign-key|uniqueness|check
constraints]
DROP CONSTRAINT column-name
CASCADE|RESTRICT
The clauses specified in the syntax above are specified by the SQL
standard, however, not all DBMS brands support these clauses.

33
Week 3 Database Management II
Assertions

An assertion can be defined as a database constraint that


restricts the contents of the database as a whole.
It is specified as a search condition that restricts the contents of
multiple tables and the data relationships amongst them.
Assertions are specified as part of the overall database definition
using the SQL2 CREATE ASSERTION statement.
The DROP assertion statement is also used to remove assertions
that are no longer needed in a database.

34
Week 3 Database Management II
Domains

A domain can be defined as a named collection of data values


that can effectively function as an additional data type for use in
database definitions.
The CREATE DOMAIN statement is used to create a domain.
After the creation, the domain name can be referenced in a
column definition as if it were a data type.
Domains that are no longer needed in a database can be dropped
using the DROP DOMAIN statement.

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

The major advantage of having an index is that it greatly speeds


up the execution of SQL statements with search conditions that
refer to columns that have been indexed.
It is therefore advisable to create an index for columns that are
frequently used in search conditions.

37
Week 3 Database Management II
Indexes Disadvantages

Indexes however consume disk space, which is a major


disadvantage if there isn’t enough disk space available.
Another disadvantage of indexes is that they require constant
update each time the indexed column is updated on a table and
when a new row is inserted on a table.
Many DBMS products automatically create an index for the
primary key of tables, or, any column that has been defined with
the uniqueness constraint.

38
Week 3 Database Management II
CREATE INDEX Syntax

The CREATE INDEX statement is used to create an index, and is


supported by most DBMS brands.
The syntax is as follows:
CREATE INDEX UNIQUE INDEX index-name ON table-
name (column-name ASC|DESC)
The DROP INDEX statement is also used to remove indexes that
are no longer needed in a database.

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

Create a database named “yourFullNamedb” and Create the


DRIVER and CAR tables. Eg. bilkisudb
Ensure you include the following Data Integrity Constraints:
The primary key column in DRIVER table is name
The primary key column in CAR table is regno
owner is a foreign key in CAR table and it references name in DRIVER
table.
After creating the tables, populate the tables by inserting the
records for each tuple/row as depicted.
We will practice writing simple and complex queries based on
these tables in the subsequent classes.
42
Week 3 Database Management II
DRIVER

NAME DOB

Jim Smith 11 Jan 1980

Bob Smith 23 Mar 1981

Bob Jones 3 Dec 1986

43
Week 3 Database Management II
CAR

REGNO MAKE COLOUR PRICE OWNER

F611 AAA FORD RED 12000 Jim Smith

J111 BBB SKODA BLUE 11000 Jim Smith

A155 BDE MERCEDES BLUE 22000 Bob Smith

K555 GHT FIAT GREEN 6000 Bob Jones

SC04 BFE SMART BLUE 13000

44
Week 3 Database Management II

You might also like