376421_LEC07_BasicSQL_sol
376421_LEC07_BasicSQL_sol
Lecture 7
Learning Outcomes
In this chapter, you will learn:
The basic commands and functions of SQL
How to use SQL for data administration (to
create tables, indexes, and views)
How to use SQL for data manipulation (to add,
modify, delete, and retrieve data)
How to use SQL to query a database for useful
information
• Update rules?
Restrict
No Action
Cascade (for MySQL)
11
SQL Constraints
NOT NULL constraint
Ensures that column does not accept nulls
UNIQUE constraint
Ensures that all values in column are unique
DEFAULT constraint
Assigns value to attribute when a new row is
added to table
CHECK constraint
Validates data when attribute value is entered
Example:
CREATE TABLE VENDOR
(v_code char(5) not null,
v_name varchar(20),
v_contact varchar(15),
v_areacode char(5),
v_phone varchar(10),
v_state char(2),
v_order char(1)
primary key (v_code));
Examples:
SELECT *
FROM PRODUCT
WHERE P_PRICE BETWEEN 50.00 AND 100.00;
SELECT *
FROM PRODUCT
WHERE P_PRICE > 50.00 AND P_PRICE < 100.00;
Examples:
SELECT P_CODE, P_DESCRIPT, V_CODE
FROM PRODUCT
WHERE V_CODE IS NULL;
Time function:
It is one of the data type with format: HH:MM:SS
SELECT time_format( current_time(), '%h:%i %p');
h: refers to hour, from 0 – 12
i: refers to minute, from0 – 60
p: means AM or PM
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_INDATE >= ‘2010-01-16’
AND P_INDATE <= ‘2010-02-10’;
55 TDB2111 Database Systems December 24, 2024
Advanced Data Updates - Results