DD Chapter 4
DD Chapter 4
DATABASE DESIGN
CHAPTER 4
STRUCTURED QUERY LANGUAGE
(SQL)
1
APPLY SQL COMMANDS TO A DATABASE
4.1.1 STATE THE USE OF STRUCTURED QUERY LANGUAGE (SQL).
4.1.2 EXPLAIN THE SQL DATA TYPES
4.1.3 IDENTIFY THE TYPES OF SQL STATEMENTS:
4.1.4 IDENTIFY THE DDL STATEMENTS IN DEFINING THE TABLE STRUCTURE
4.1.5 IDENTIFY THE TYPES OF CONSTRAINTS IN DDL STATEMENTS: COLUMN CONSTRAINTS
AND TABLE CONSTRAINTS
4.1.6 USE THE FUNCTIONS OF THE BASIC DDL COMMANDS:
4.1.7 IDENTIFY THE USES OF DML STATEMENTS
4.1.8 USE THE FUNCTIONS OF THE FOLLOWING DML COMMANDS:
4.1.9 USE THE FUNCTIONS SQL ADVANCED COMMANDS:
4.1.10 USE THE FUNCTIONS SQL AGGREGATE FUNCTIONS:
2
STRUCTURED QUERY LANGUAGE
4
EXPLAIN SQL DATA TYPES
Each column in a database table is required to have a name and a data type.
An SQL developer must decide what type of data that will be stored inside each
column when creating a table.
The data type is a guideline for SQL to understand what type of data is expected
inside of each column, and it also identifies how SQL will interact with the
stored data.
In MySQL there are three main data types: string, numeric, and date and time.
Note: Data types might have different names in different database. And even if the
name is the same, the size and other details may be different! Always check the
documentation! 5
STRING DATA TYPES
6
NUMERIC DATA TYPES
7
DATE & TIME DATA TYPES
8
9
TYPES OF SQL
11
TYPES OF SQL
12
TYPES OF SQL
13
DDL STATEMENTS IN DEFINING THE TABLE
STRUCTURE
DDL statements are used to build and modify the structure of your tables and
other objects in the database. When you execute a DDL statement, it takes effect
immediately.
DDL commands :
CREATE
USE
ALTER
DROP
14
TYPES OF CONSTRAINTS IN DDL STATEMENTS
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a
table.
This ensures the accuracy and reliability of the data in the table.
If there is any violation between the constraint and the data action,
the action is aborted.
Constraints can be :
column level
table level. 15
Column level constraints apply to a column, and table level constraints apply to
the whole table.
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
FOREIGN KEY - Uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
INDEX - Used to create and retrieve data from the database very quickly
16
CONNECTING TO MYSQL USING COMMAND
PROMPT FOR WINDOWS
17
USE FUNCTIONS OF THE BASIC DDL
COMMANDS
CREATING DATABASE AND TABLE
Create database
Syntax: CREATE DATABASE database_name;
Example : CREATE DATABASE Company;
Create table
Syntax:
USE
Syntax: USE database_name;
Example : USE Company;
Create table
Syntax:
);
CREATE TABLE : EXAMPLE
CREATE TABLE worker
(
workerno VARCHAR (5) NOT NULL,
workername VARCHAR (15),
position VARCHAR (15),
address VARCHAR (20),
entrydate DATE,
tel_no VARCHAR (7),
salary DECIMAL (7,0),
20
PRIMARY KEY (workerno)
);
WORKER TABLE
21
ANOTHER TABLE : BUILDING
CREATE TABLE building
(
buildno VARCHAR(5) NOT NULL,
address VARCHAR(20),
city VARCHAR(10),
buildtype VARCHAR(10) ,
roomno INTEGER,
rent DECIMAL(4,0),
workerno VARCHAR(5),
PRIMARY KEY (buildno),
22
FOREIGN KEY (workerno) REFERENCES worker (workerno)
);
BUILDING TABLE
23
ALTER TABLE
ALTER TABLE statement is used to add or drop columns in
an existing table.
Syntax:
ALTER TABLE table_name
ADD column_name datatype
DROP DATABASE
Syntax: DROP database database_name;
Example: DROP DATABASE company;
DROP TABLE
Syntax: DROP TABLE table_name;
Example: DROP TABLE worker;
26
<PRIMARY KEY> = ??
WORKER TABLE
29
BUILDING TABLE
30
SELECT STATEMENT
With SQL, we can query a database and have a result set returned.
All queries are based on the SELECT command.
Syntax:
SELECT column_name(s)
FROM table_name;
31
SELECT STATEMENT
SELECT *
FROM worker;
Result: will display the entire table.
32
SELECT STATEMENT
33
UPDATE STATEMENT
34
UPDATE STATEMENT
35
EXAMPLE
Building
UPDATE building
SET roomno = 4
WHERE buildno = 'B04';
36
EXAMPLE
UPDATE building
SET roomno=5, rent=475
WHERE buildno = 'B04'
37
DELETE STATEMENT
39
EXAMPLE
To delete a row – deleting building ‘B06’ record.
42
DISTINCT STATEMENT
result
43
DISTINCT STATEMENT
If we use:
SELECT DISTINCT address FROM worker;
Result:
44
CALCULATED FIELD
Example:
SELECT workerno, workername, salary /2
FROM worker;
Result:
45
RENAME COLUMN
46
WHERE STATEMENT
47
WHERE STATEMENT
Operator Description
= Equal
<> Not equal
> Greater than *in some versions of SQL, <>
< Less than operator may be written as !=
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you
want to return for at least one of 48
the columns
SIMPLE QUERIES…
49
SIMPLE QUERIES…
WHERE & OR
50
SIMPLE QUERIES…
WHERE, AND & BETWEEN
51
SIMPLE QUERIES…
WHERE & IN
List the Director and Vice Director.
52
List of data that were taken from other table.
For Example :
RESULT :
55
WILDCARD STATEMENT
LIKE - used to specify a search for a pattern in a column.
A "%" sign can be used to define wildcards (missing letters in the pattern) both before and
Using LIKE:
a) The following SQL statement will return persons with first names that start with an 'O':
SELECT *
FROM Persons
WHERE FirstName 56
LIKE 'O%' ;
WILDCARD STATEMENT
57
WILDCARD STATEMENT
58
WILDCARDS STATEMENT %
b)The following SQL statement will return persons with first names that end with an
'a':
SELECT * FROM Staff
WHERE FirstName
LIKE '%a' ;
c)The following SQL statement will return persons with first names that contain the
pattern 'la':
SELECT *
FROM Staff
WHERE FirstName
59
LIKE '%la%' ;
WILDCARDS STATEMENT _
• Consider the following table :
60
• The following SQL statement selects all customers with a City starting
with any character, followed by "ondon":
Example
SELECT * FROM Customers
WHERE City LIKE '_ondon';
61
• The following SQL statement selects all customers with a City
starting with "L", followed by any character, followed by "n",
followed by any character, followed by "on":
Example
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';
62
SIMPLE QUERIES…
NULL
63
SIMPLE QUERIES…
NULL
Result:
64
ORDER BY STATEMENT
65
ORDER BY STATEMENT
ORDERS table:
Company OrderNumber
Sega 3412
ABC Shop 5678 SELECT Company, OrderNumber
W3Schools 6798 FROM Orders
W3Schools 2312 ORDER BY Company;
66
ORDER BY STATEMENT
Result:
Company OrderNumber
ABC Shop 5678 SELECT Company, OrderNumber
Sega 3412 FROM Orders
W3Schools 6798 ORDER BY Company, OrderNumber;
W3Schools 2312
67
ORDER BY STATEMENT
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412 SELECT Company, OrderNumber
W3Schools 2312 FROM Orders
W3Schools 6798 ORDER BY Company DESC;
68
ORDER BY STATEMENT
Result:
Company OrderNumber
W3Schools 2312
Sega 3412
ABC Shop 5678
W3Schools 6798
70
INTERSECT STATEMENT
The INTERSECT clause in SQL is used to combine two SELECT statements but
the dataset returned by the INTERSECT statement will be the intersection of the
data-sets of the two SELECT statements.
In simple words, the INTERSECT statement will return only those rows which
will be common to both of the SELECT statements.
71
Note: The number and type of fields present in the two data-sets must be same and similar.
INTERSECT STATEMENT
• Syntax :
72
EXAMPLE
• Consider the following tables and sample queries:
FROM Customers
= Orders.Customer_id
INTERSECT
ON Customers.ID =
Orders.Customer_id;
73
EXAMPLE
74
GROUP BY STATEMENT
75
EXAMPLE
Sales table:
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
76
Is this correct? Why?
EXAMPLE
77
HAVING STATEMENT
FUNCTION OUTPUT
COUNT Return the number of rows containing not null values
SUM The sum of all values for a selected attribute in a given
column
AVG Return average of values in a specified column
MAX Return largest of value in a specified column
MIN Return smallest of valuesin a specified column
80
AGGREGATE FUNCTIONS
81
AGGREGATE FUNCTIONS
COUNT EXAMPLE:
Calculate the number of employees who lives in Bangi.
82
EXPLAIN THE DIFFERENCE…
Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 6798
W3Schools 2312
SELECT COUNT(company)
SELECT COUNT
AS Company (DISTINCT company) AS
FROM customer_order; Company FROM
customer_order;
83
AGGREGATE FUNCTIONS
SUM EXAMPLE:
SELECT worker no, SUM( rent ) AS RENT
FROM building
WHERE workerno = 'A02‘;
84
AGGREGATE FUNCTIONS
85