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

Chapter 4 Structured Query Language SQL Pt.1 PDF

Uploaded by

risksentinel.obj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Chapter 4 Structured Query Language SQL Pt.1 PDF

Uploaded by

risksentinel.obj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

1

TOPIC 4
Structured Query Language (SQL)
2

4.1– Apply SQL commands to a database


3

4.1.1 Use of SQL


• SQL or Structured Query Language
• an English-like language
• used to communicate with a relational DBMS
whether to create or manipulate the database

• a standard language for:


– storing,
– manipulating
– and retrieving data in databases
4

4.1.2 SQL data types


Data types:
• defines what kind of value a column can hold:
– integer data
– character data
– monetary data
– date and time data
– binary strings, and so on

• 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.
might have different names in different database. even if the name is the
same, the size and other details may be different!
Always check the documentation!
5

4.1.2 SQL data types


MySQL data types

Three main data types:

Text Number

Date
6

4.1.2 SQL data types


MySQL data types: Text data types
Data type Description
CHAR(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified
in parenthesis. Can store up to 255 characters

VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is
specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be
converted to a TEXT type

TINYTEXT Holds a string with a maximum length of 255 characters


TEXT Holds a string with a maximum length of 65,535 characters
BLOB For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data

ENUM(x,y,z,etc.) Let you enter a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted
that is not in the list, a blank value will be inserted.Note: The values are sorted in the order you enter
them.
You enter the possible values in this format: ENUM('X','Y','Z')
SET Similar to ENUM except that SET may contain up to 64 list items and can store more than one choice
7

4.1.2 SQL data types


MySQL data types: Number data types

Data type Description


TINYINT(size) -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis

SMALLINT(size) -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may be specified in
parenthesis
MEDIUMINT(size) -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum number of digits may be specified in
parenthesis
INT(size) -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The maximum number of digits may be
specified in parenthesis
BIGINT(size) -9223372036854775808 to 9223372036854775807 normal. 0 to 18446744073709551615 UNSIGNED*. The
maximum number of digits may be specified in parenthesis

FLOAT(size,d) A small number with a floating decimal point. The maximum number of digits may be specified in the size
parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

DOUBLE(size,d) A large number with a floating decimal point. The maximum number of digits may be specified in the size
parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

DECIMAL(size,d) A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum number of digits may be
specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in
the d parameter
8

4.1.2 SQL data types


MySQL data types: Date data types

Data type Description


DATE() A date. Format: YYYY-MM-DD.
Note: The supported range is from '1000-01-01' to '9999-12-31'
DATETIME() *A date and time combination. Format: YYYY-MM-DD HH:MI:SS
Note: The supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch
('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD HH:MI:SS
Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC
TIME() A time. Format: HH:MI:SSNote: The supported range is from '-838:59:59' to '838:59:59'
YEAR() A year in two-digit or four-digit format.Note: Values allowed in four-digit format: 1901 to
2155. Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069
9

4.1.2 SQL data types


Microsoft Access Data Types

Data type Description Storage


Text Use for text or combinations of text and numbers. 255 characters maximum
Memo Memo is used for larger amounts of text. Stores up to 65,536 characters. Note: You cannot sort a memo field.
However, they are searchable

Byte Allows whole numbers from 0 to 255 1 byte


Integer Allows whole numbers between -32,768 and 32,767 2 bytes
Long Allows whole numbers between -2,147,483,648 and 2,147,483,647 4 bytes
Single Single precision floating-point. Will handle most decimals 4 bytes
Double Double precision floating-point. Will handle most decimals 8 bytes
Currency Use for currency. Holds up to 15 digits of whole dollars, plus 4 decimal places. Tip: You can choose which 8 bytes
country's currency to use
AutoNumber AutoNumber fields automatically give each record its own number, usually starting at 1 4 bytes

Date/Time Use for dates and times 8 bytes


Yes/No A logical field can be displayed as Yes/No, True/False, or On/Off. In code, use the constants True and False 1 bit
(equivalent to -1 and 0). Note: Null values are not allowed in Yes/No fields

Ole Object Can store pictures, audio, video, or other BLOBs (Binary Large OBjects) up to 1GB

Hyperlink Contain links to other files, including web pages


Lookup Wizard Let you type a list of options, which can then be chosen from a drop-down list 4 bytes
10

4.1.3 Types of SQL statements


4 types of sublanguages:
a. Data Definition Language (DDL)
b. Data Manipulation Language (DML)
c. Transaction Control Language (TCL)
d. Data Control Language (DCL)
11

4.1.3 Types of SQL statements


Types Use Statements

CREATE
- create, modify and delete database structure but USE
DDL not data ALTER
- normally used by DBA (database administrator) DROP

INSERT
allow user to retrieve, update, add, or delete data in a SELECT
DML
database UPDATE
DELETE
COMMIT
ROLLBACK
TCL manage changes made by DML statements
SAVEPOINT
SET TRANSACTION
control the user access to the database, tables, views, GRANT
DCL REVOKE
procedures, functions and packages
12

4.1.4 DDL statements in defining table structure

DDL statements
• to build and modify the structure of your tables
and other objects in the database
• it takes effect immediately when executed
i. CREATE - to create database and table
ii. USE - to use specific database
iii. ALTER - to specify PK and FK constraints
- to make other modifications to the table structure.
iii. DROP - to eliminate any object created before
13

4.1.5 Types of constraints in DDL statements

CONSTRAINT
o an optional part of a CREATE TABLE statement or
ALTER TABLE statement
o a rule to which data must conform
o constraint names are optional

2 levels : column-level & table-level


14

4.1.5 Types of constraints in DDL statements

Student
StudentID First_Name Last_Name Age
F1001 Amir Faiz 19
F1002 Nur Aliah 19

table-level
column-level
15

4.1.5 Types of constraints in DDL statements


Criteria Column-level Table-level
Function a rule to which data must conform
Refer to only one column one or more columns in
column the table
Specify do not specify a column specify the names of the
column name (except CHECK columns to which they
Difference
name constraints) apply
No. of refer to the column that they CHECK constraints can
column follow refer to 0 or more
referred columns in the table
NOT NULL – must have a
-
value
PRIMARY KEY : unique identifier for a record
Constraints
UNIQUES : values in the column must be unique
FOREIGN KEY : establishes a relationship
CHECK : value must be in the specified list
16

4.1.5 Types of constraints in DDL statements


NOT NULL
CREATE TABLE Student (
StudentID int NOT NULL,
LastName varchar(50) NOT NULL,
FirstName varchar(50) NOT NULL,
Age int
);

PRIMARY KEY
CREATE TABLE Student (
StudentID int NOT NULL,
LastName varchar(50) NOT NULL,
FirstName varchar(50) NOT NULL,
Age int,
PRIMARY KEY (StudentID)
);
17

4.1.5 Types of constraints in DDL statements


UNIQUE
CREATE TABLE Student (
StudentID int NOT NULL UNIQUE,
LastName varchar(50) NOT NULL,
FirstName varchar(50),
Age int, Note:
); You can have many UNIQUE
constraints per table, but only
@
one PRIMARY KEY constraint
CREATE TABLE Student ( per table.
ID int NOT NULL,
LastName varchar(50) NOT NULL,
FirstName varchar(50),
Age int,
CONSTRAINT UC_Student UNIQUE (StudentID,LastName)
);
18

4.1.5 Types of constraints in DDL statements


FOREIGN KEY
CREATE TABLE Student (
StudentID int NOT NULL,
LastName varchar(50) NOT NULL,
FirstName varchar(50) NOT NULL,
Age int, PRIMARY KEY (StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);

CHECK
CREATE TABLE Student (
StudentID int NOT NULL, LastName varchar(50) NOT NULL,
FirstName varchar(50) NOT NULL,
Age int, CHECK (Age >= 18),
PRIMARY KEY (StudentID)
);
19

4.1.6 Functions of basic DDL commands


a) CREATE
b) USE
c) ALTER
d) DROP
20

4.1.6 Functions of basic DDL commands


a) CREATE - Create a database

Syntax:
CREATE DATABASE <Database_name>

CREATE DATABASE PMS; PMS

Database created.
21

4.1.6 Functions of basic DDL commands


a) CREATE - Create a table

Syntax:
CREATE TABLE <Table_name>

CREATE TABLE Student; PMS

Table created.
Student
22

4.1.6 Functions of basic DDL commands


CREATE TABLE table_name
(
column_name1 data_type(size),
SYNTAX:
column_name2 data_type(size),
...
);

CREATE TABLE Student


(
StudentID int,
EXAMPLE: LastName varchar(50),
FirstName varchar(50),
Age int(30)
);
23

4.1.6 Functions of basic DDL commands


Table and columns created.
Student
StudentID LastName FirstName Age
24

4.1.6 Functions of basic DDL commands


b) USE - switch to a database.

Syntax:
USE <database_name>

USE PMS; PMS

Database used.
25

4.1.6 Functions of basic DDL commands


c) ALTER - to add, delete, or modify columns in an
existing table
• use the ALTER and ADD/MODIFY/DROP keyword
and specify:
– Table name
– New or existing field name(s)
– Field type
26

4.1.6 Functions of basic DDL commands


c) ALTER
Syntax:
ALTER TABLE table ALTER TABLE STUDENT
ADD (Column datatype) ADD Test numeric (4,2);
[, Column datatype]…; Table altered.

StudentID LastName FirstName Age Test

15.21
27

4.1.6 Functions of basic DDL commands


c) ALTER
Syntax:
ALTER TABLE table ALTER TABLE STUDENT
MODIFY Test numeric (4,1);
MODIFY (Column datatype)
[, Column datatype]…; Table altered.

StudentID LastName FirstName Age Test

15.1
141.2
28

4.1.6 Functions of basic DDL commands


c) ALTER
Syntax:
ALTER TABLE table ALTER TABLE STUDENT
DROP (Column); DROP Test;

Table altered.

StudentID LastName FirstName Age


29

4.1.6 Functions of basic DDL commands


d) DROP
Syntax:
DROP TABLE table ; DROP TABLE STUDENT;

Table dropped.
30

4.1.6 Functions of basic DDL commands


d) DROP
Syntax:
DROP DATABASE database; DROP DATABASE PMS;

Database dropped.
31

Extra
• A table can have at most one
PRIMARY KEY constraint
• A table can have multiple UNIQUE
constraints.
32

4.1.7 Uses of DML statements


Uses:

• statement that modify data stored in database


objects (tables)
• allow user to retrieve, update, add, or delete data
in a database
• contains SELECT, INSERT, UPDATE and DELETE
statements
33

4.1.8 Functions of DML commands


a) INSERT
b) SELECT
c) UPDATE
d) DELETE
34

4.1.8 Functions of DML commands


a) INSERT

• Use the INSERT keyword and specify:


– Table name
– Field names (optional)
– Values for each field

Syntax:
INSERT INTO table (column1,column2,..)
VALUES (value1,value2,..);

**This will insert only one row.


35

4.1.8 Functions of DML commands


a) INSERT - example

INSERT INTO Student (S_ID,S_Name,S_Address,S_CGPA)


VALUES
('18DNS2001', ‘Alia Husna', ‘Pahang’, 3.59);

1 row created.

S_ID S_Name S_Address S_CGPA

18DNS2001 Alia Husna Pahang 3.59


36

4.1.8 Functions of DML commands


a) INSERT - example (NULL)

INSERT INTO Student (S_ID,S_Name,S_Address,S_CGPA)


VALUES
('18DNS2002', ‘’Shafiq, ‘Kedah’, NULL);

1 row created.

S_ID S_Name S_Address S_CGPA

18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL


37

4.1.8 Functions of DML commands


a) INSERT - example (without column name)

INSERT INTO Student


VALUES
('18DNS2003', ‘David', ‘Melaka, 3.17);

1 row created.
S_ID S_Name S_Address S_CGPA

18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2003 David Melaka 3.17


38

4.1.8 Functions of DML commands


a) INSERT - example (not accordingly)

INSERT INTO Student (S_CGPA,S_Name,S_ID)


VALUES
(3.33, ‘Swee Lan’, '18DNS2004',);

1 row created.
S_ID S_Name S_Address S_CGPA
18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2003 David Melaka 3.17

18DNS2004 Swee Lan NULL 3.33


39

4.1.8 Functions of DML commands


Student
S_ID S_Name S_Address S_CGPA
18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2003 David Melaka 3.17

18DNS2004 Swee Lan NULL 3.33


40

4.1.8 Functions of DML commands


a) INSERT - example (multiple rows)

INSERT INTO Student (S_ID,S_Name,S_Address,S_CGPA)


VALUES
('18DNS2001', ‘Alia Husna', ‘Pahang’, 3.59),
('18DNS2002', ‘Shafiq’, ‘Kedah’, NULL),
('18DNS2004', ‘Swee Lan’, NULL, 3.33);
3 rows created.
S_ID S_Name S_Address S_CGPA
18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2004 Swee Lan NULL 3.33


41

4.1.8 Functions of DML commands


a) INSERT - example
42

QUESTION
COURSE
Course_ID Course_Name CreditHour

Choose the CORRECT SQL query to INSERT data into


COURSE table:
A. INSERT COURSE VALUES (‘FP301’, ‘Database’, ‘3.0’);
B. INSERT INTO COURSE VALUES (FP301, Database, 3.0);
C. INSERT INTO COURSE VALUES (FP301, ‘Database’, 3.0);
D. INSERT INTO COURSE (Course_ID, Course_Name,
CreditHour) VALUES (‘FP301’, ‘Database’, 3.0);
43

4.1.8 Functions of DML commands


b) SELECT - to select data from a database

• data returned is stored in a result table, called the result-


set
• Use the SELECT keyword and specify:
– Table name
– Field name(s)

Syntax:
SELECT column1, column2, ...
FROM table_name;

**Only select particular field from the table


44

4.1.8 Functions of DML commands


b) SELECT (particular field/s)

SELECT S_ID, S_CGPA FROM Student;

S_ID and S_CGPA fields selected.

S_ID S_CGPA
18DNS2001 3.59

18DNS2002

18DNS2003 3.17

18DNS2004 3.33
45

4.1.8 Functions of DML commands


b) SELECT (all fields)
Syntax:
SELECT * FROM table_name;

**This will select all the fields available in the table


46

4.1.8 Functions of DML commands


b) SELECT (all fields)
Syntax:
SELECT * FROM Student;

All fields selected.

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2003 David Melaka 3.17

18DNS2004 Swee Lan NULL 3.33


47

4.1.8 Functions of DML commands


c) UPDATE - to modify the existing records in a table

• Use the UPDATE and SET keyword and specify:


– Table name
– Field name(s)
– WHERE clause (optional)

Syntax:
UPDATE table
SET column = value, column = value
WHERE condition;

**Update more than one row at a time, if required


48

4.1.8 Functions of DML commands


c) UPDATE - example

UPDATE STUDENT
SET S_Address = ‘Perak’
WHERE S_ID = ‘18DNS2002’

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Pahang 3.59
18DNS2002 Shafiq Kedah NULL

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Perak NULL


49

4.1.8 Functions of DML commands


c) UPDATE - example

UPDATE STUDENT
SET S_Address = ‘Melaka’

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Pahang 3.59
18DNS2002 Shafiq Perak NULL

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Melaka 3.59

18DNS2002 Shafiq Melaka NULL


50

4.1.8 Functions of DML commands


c) UPDATE - example

UPDATE STUDENT
SET S_Address = ‘Perak’, S_CGPA = 4.00
WHERE S_ID = ‘18DNS2002’;

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Melaka 3.59
18DNS2002 Shafiq Melaka NULL

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Melaka 3.59

18DNS2002 Shafiq Perak 4.00


51

QUESTION
COURSE
Course_ID Course_Name CreditHour
FP301 Database 3.0

Choose the CORRECT SQL query to UPDATE Course_Name =


‘Database Design’ where Course_ID = ‘FP301’?

A. UPDATE COURSE SET Course_ID;


B. UPDATE COURSE SET Course_Name = Database Design
WHERE COURSE_ID = FP301;
C. UPDATE COURSE SET Course_Name = ‘Database Design’
WHERE COURSE_ID = ‘FP301’;
D. UPDATE COURSE Course_Name = ‘Database Design’
WHERE COURSE_ID = ‘FP301’;
52

4.1.8 Functions of DML commands


d) DELETE - to delete existing records in a table

• Use the DELETE FROM keyword and specify:


– Table name
– WHERE clause

Syntax:
DELETE FROM table
WHERE condition;

*If no rows are deleted, a message “0 rows deleted” is returned.


53

4.1.8 Functions of DML commands


d) DELETE (particular row)

DELETE FROM Student


WHERE S_CGPA = 3.17;

S_ID S_Name S_Address S_CGPA


18DNS2001 Alia Husna Pahang 3.59

18DNS2002 Shafiq Kedah NULL

18DNS2004 Swee Lan NULL 3.33


54

4.1.8 Functions of DML commands


d) DELETE (all record)

DELETE FROM Student;

S_ID S_Name S_Address S_CGPA

You might also like