Lecture Database management systems Chapter 8 SQL

41 234 0
Lecture Database management systems  Chapter 8 SQL

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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).

SQL 7/11/2011 Introduction  SQL commands can be classified in to three types:  Data Definition Language commands (DDL)  Data Manipulation Language commands (DML)  Data Control Language commands (DCL) 7/11/2011 Domain types  Numeric:  int Integer (a finite subset of the integers that is machine-dependent)  smallint Small integer (a machine-dependent subset of the integer domain type)  numeric(p,d) Fixed point number, with userspecified precision of p digits, with n digits to the right of decimal point  real, double precision Floating point and doubleprecision floating point numbers, with machinedependent precision  float(n) Floating point number, with user-specified precision of at least n digits 7/11/2011 Domain 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 7/11/2011 Domain types  Datetime:  datetime store not only a date, but also a time alongside it  smalldatetime 7/11/2011 Create Table Construct  An SQL relation is defined using the create table command: create table r (A1 D1, A2 D2, , An Dn, (integrity-constraint1), , (integrity-constraintk))  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 7/11/2011 Create Table Construct  department (dept_no, dept_name, location)  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) ) 7/11/2011 Integrity Constraints  Constraints           7/11/2011 Null Not null Unique Primary key References (Foreign key) Check Default On delete cascade On update cascade On delete set null Delete table  Delete the whole table (include all data) in database  DROP TABLE TableName  Ex:  DROP TABLE DEPARTMENT 7/11/2011 Truncate 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 7/11/2011 10 SELECT - HAVING  The HAVING command is used to select the group In other words HAVING restricts the groups according to a specified condition  The syntax of HAVING command is: SELECT attribute name, aggregate function FROM table name GROUP BY attribute name HAVING condition; HAVING  SELECT MAHD, SUM(SOLUONG) FROM CHITIETHOADON GROUP BY MAHD HAVING SUM(SOLUONG)>100 SELECT - 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; SELECT - SORTING  SELECT * FROM NHANVIEN ORDER BY TENNV Join 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 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 not have corresponding rows in the right table will be in the result table with null values for fields from the right table Join Types  RIGHT OUTER JOIN  Keeps stray rows from the right table, filling columns from the left table with NULL values if no corresponding data Join 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 Set Operations  UNION Operation  If we have two relations R and S then the set UNION operation contains tuples that either occurs in R or S or both  UNION/ UNION ALL  INTERSECT Operation  The intersection operation returns the tuples that are common to the two relations  EXCEPT Operation:  EXCEPT returns any distinct values from the left query that are not also found on the right query View  View: a virtual collection of records from existing tables  Being used for security and/or performance issues View  Creating view CREATE VIEW [schema_name.]view_name [(column[, n])] [ WITH [ , n ] ] AS select_statement [ WITH CHECK OPTION ] [ ; ] ::={[ENCRYPTION] [SCHEMABINDING] [ VIEW_METADATA ]}  Removing view DROP VIEW { view_name } [ , n ] View  Creating view  CREATE VIEW VWSACH AS SELECT TOP 10* FROM DANHMUCSACH ORDER BY TENSACH STORE PROCEDURE  A stored procedure is a group of TransactSQL statements that is compiled one time, and then can be executed many times  This increases performance when the stored procedure is executed because the Transact-SQL statements not have to be recompiled STORE PROCEDURE STORE PROCEDURE  Execute store procedure EXECUTE ProductName [[, …n][ OUTPUT ]]  Ex: EXECUTE PTBACNHAT 1,2 STORE PROCEDURE  CREATE PROC PTBACNHAT @A INT, @B INT AS IF(@A=0) BEGIN IF(@B=0) BEGIN END ELSE BEGIN END ELSE BEGIN END END PRINT 'PHUONG TRINH VO SO NGHIEM' PRINT 'PHUONG TRINH VO NGHIEM' PRINT 'NGHIEM PT = '+ CAST((-@B*1.0/@A)AS CHAR) ... 18 SELECT statement Maths - AVG() - MIN() - MAX() - SUM() - COUNT() - SQUARE() - SQRT() - ROUND() 7/11/2011 String - ASCII() - CHAR() - UPPER() - LOWER() - LEN() - LTRIM() - RTRIM() - LEFT() -. .. float(n) Floating point number, with user-specified precision of at least n digits 7/11/2011 Domain types  String:  char(n) Fixed length character string, with user-specified length n  varchar(n)... longer than 8, 000 characters 7/11/2011 Domain types  Datetime:  datetime store not only a date, but also a time alongside it  smalldatetime 7/11/2011 Create Table Construct  An SQL relation

Ngày đăng: 15/05/2017, 10:33

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan