Chapter 4 Structured Query Language SQL Pt.1 PDF
Chapter 4 Structured Query Language SQL Pt.1 PDF
TOPIC 4
Structured Query Language (SQL)
2
Text Number
Date
6
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
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
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
Ole Object Can store pictures, audio, video, or other BLOBs (Binary Large OBjects) up to 1GB
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
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
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
Student
StudentID First_Name Last_Name Age
F1001 Amir Faiz 19
F1002 Nur Aliah 19
table-level
column-level
15
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
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
Syntax:
CREATE DATABASE <Database_name>
Database created.
21
Syntax:
CREATE TABLE <Table_name>
Table created.
Student
22
Syntax:
USE <database_name>
Database used.
25
15.21
27
15.1
141.2
28
Table altered.
Table dropped.
30
Database dropped.
31
Extra
• A table can have at most one
PRIMARY KEY constraint
• A table can have multiple UNIQUE
constraints.
32
Syntax:
INSERT INTO table (column1,column2,..)
VALUES (value1,value2,..);
1 row created.
1 row created.
1 row created.
S_ID S_Name S_Address S_CGPA
1 row created.
S_ID S_Name S_Address S_CGPA
18DNS2001 Alia Husna Pahang 3.59
QUESTION
COURSE
Course_ID Course_Name CreditHour
Syntax:
SELECT column1, column2, ...
FROM table_name;
S_ID S_CGPA
18DNS2001 3.59
18DNS2002
18DNS2003 3.17
18DNS2004 3.33
45
Syntax:
UPDATE table
SET column = value, column = value
WHERE condition;
UPDATE STUDENT
SET S_Address = ‘Perak’
WHERE S_ID = ‘18DNS2002’
UPDATE STUDENT
SET S_Address = ‘Melaka’
UPDATE STUDENT
SET S_Address = ‘Perak’, S_CGPA = 4.00
WHERE S_ID = ‘18DNS2002’;
QUESTION
COURSE
Course_ID Course_Name CreditHour
FP301 Database 3.0
Syntax:
DELETE FROM table
WHERE condition;