Lecture Database management systems Chapter 8 SQL. SQL commands can be classified in to three types Data Definition Language commands (DDL), Data Manipulation Language commands (DML), Data Control Language commands (DCL).
Trang 1SQL
Trang 2Introduction
SQL commands can be classified in to three types:
Data Definition Language commands (DDL)
Data Manipulation Language commands (DML)
Data Control Language commands (DCL)
Trang 3user- real, double precision. Floating point and precision floating point numbers, with machine- dependent precision
double- float(n). Floating point number, with user-specified precision of at least n digits
Trang 4Domain types
String:
char(n) Fixed length character string, with
user-specified length n.
varchar(n) Variable length character
strings, with user-specified maximum length n
nvarchar(n) similar to varchar, except it
uses Unicode and therefore doubles the amount of space required to store the data
Text: holds data that is longer than 8,000 characters
Trang 5Domain types
Datetime:
datetime store not only a date, but also a
time alongside it
smalldatetime
Trang 6Create Table Construct
An SQL relation is defined using the create table command:
r is the name of the relation
each Ai is an attribute name in the schema of relation r
Di is the data type of values in the domain of attribute Ai
create table r
(A1 D1, A2 D2, , A n D n , (integrity-constraint1), ,
(integrity-constraintk))
Trang 7Create Table Construct
Primary key: dept_no
Candidate key: dept_name
CREATE TABLE department
(
dept_no char(3) primary key, dept_name varchar(36) unique not null, location varchar(20)
)
Trang 10Truncate table
The TRUNCATE TABLE command removes all the rows from the table The truncate table also releases the storage space used
by the table
The syntax of TRUNCATE command is:
TRUNCATE TABLE tablename
Trang 11Alias
Tables listed in the FROM clause can be given an
alternative name
An alias is created by:
Typing the name of the table
Pressing the space bar
Typing the name of the alias
One reason for using an alias is simplicity
A second reason for using an alias is that it is needed
when joining a table to itself, called a self-join
Trang 12Data Manipulation Language
Adding a New Row to the Table
INSERT INTO tablename
VALUES ( 'value1' , 'value2' , , 'valuen' )
Updating the Data in the Table
UPDATE table name
WHERE condition ;
Deleting Row from the Table
DELETE FROM table name
WHERE condition ;
Trang 17Operators
Comparison operators (=, <, <=, <>, and so on)
String comparisons (LIKE, NOT LIKE)
Logical operators (AND, OR, NOT)
Ranges (BETWEEN and NOT BETWEEN)
Lists of values (IN and NOT IN)
Unknown Values (IS NULL and IS NOT NULL)
Exists in Subquery (EXISTS and NOT EXISTS)
Use DISTINCT to eliminate duplicates
[ TOP (expression ) [PERCENT] [ WITH TIES ] ]
Trang 18SELECT statement
The SQL syntax to see all the columns of the table is:
SELECT * FROM table name
Syntax of SELECTION Operation:
SELECT * FROM table name
WHERE condition;
Syntax of PROJECTION Operation
SELECT column name1, column name2, Column name N
FROM table name
Syntax for SELECTION and PROJECTION
SELECT column name1, column name 2 column nameN
FROM table name
WHERE condition;
Trang 19DATEPART(YY,getda te())
- DATEDIFF(X,Y,Z)
- DAY(),MONTH(),YE AR()
Trang 20SELECT data
SELECT * FROM DANHMUCSACH
Trang 22SELECT - COUNT
COUNT (*) Function:
return the number of rows of the relation
Command will take NULL values into account
COUNT (DISTINCT attribute_name):
Return the number of Rows of the relation, by eliminating duplicate values
Trang 23SELECT - COUNT
SELECT COUNT(*) TONGSOSACH
FROM DANHMUCSACH
WHERE MANHOM = 'n001'
Trang 24MAX, MIN, AVG, and SUM
SELECT MAX (attribute name)
FROM table name;
SELECT MIN (attribute name)
FROM table name;
SELECT AVG (attribute name)
FROM table name;
Trang 25SELECT - GROUP BY
The GROUP BY clause is used to group rows to compute group-statistics
The syntax of GROUP BY command is:
SELECT attribute name, aggregate function FROM table name
GROUP BY attribute name;
Trang 27 The syntax of HAVING command is:
SELECT attribute name, aggregate function FROM table name
GROUP BY attribute name
HAVING condition;
Trang 29SELECT - SORTING
The SQL command ORDER BY is used to sort the result in ascending or descending order
The syntax of ORDER BY command to
arrange the result in ascending/
descending order is:
SELECT *
FROM table name
ORDER BY attribute name ASC/ DESC;
Trang 30SELECT - SORTING
SELECT * FROM NHANVIEN
ORDER BY TENNV
Trang 31Join Types
JOIN or INNER JOIN
Each row in table to left will be joined with one or more rows in table to right
Any rows in left table that do not have
corresponding rows in the right table will not
be in the result table
LEFT JOIN or LEFT OUTER JOIN
Any rows in left table that do not have
corresponding rows in the right table will be
in the result table with null values for fields from the right table
Trang 32Join Types
RIGHT OUTER JOIN
Keeps stray rows from the right table, filling columns from the left table with NULL values
if no corresponding data
Trang 33Join Types
SELECT MAHD , D MASACH , SOLUONG
FROM CHITIETHOADON CT JOIN DANHMUCSACH
DM
ON D MASACH = C MASACH
OR
SELECT MAHD , D MASACH , SOLUONG
FROM CHITIETHOADON CT, DANHMUCSACH DM WHERE CT MASACH = DM MASACH
Trang 34Set Operations
UNION Operation
If we have two relations R and S then the set
UNION operation contains tuples that either
Trang 38STORE PROCEDURE
A stored procedure is a group of SQL statements that is compiled one time, and then can be executed many times
Transact- This increases performance when the
stored procedure is executed because the Transact-SQL statements do not have to
be recompiled
Trang 39STORE PROCEDURE
Trang 40STORE PROCEDURE
Execute store procedure
EXECUTE ProductName [<parameter>[, …n][ OUTPUT ]]
Ex: EXECUTE PTBACNHAT 1,2
Trang 41STORE PROCEDURE
CREATE PROC PTBACNHAT
AS IF(@A=0) BEGIN
IF(@B=0) BEGIN
PRINT 'PHUONG TRINH VO SO NGHIEM' END
ELSE BEGIN
PRINT 'PHUONG TRINH VO NGHIEM' END
END
ELSE BEGIN
PRINT 'NGHIEM PT = '+ CAST((-@B*1.0/@A)AS CHAR) END