Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.

Similar presentations


Presentation on theme: "Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio."— Presentation transcript:

1 Chapter 7 SQL HUANG XUEHUA

2

3 SQL

4 SQL server2005 introduction Install components  management studio

5 SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification of the Database Joined Relations**

6 History IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory Renamed Structured Query Language (SQL) ANSI and ISO standard SQL:  SQL-86  SQL-89  SQL-92  SQL:1999  SQL:2003 Not all examples here may work on your particular system.

7 Sub-languages of SQL The Data Definition Language (DDL) is a computer language for defining data structures. The DDL group is made up of theses statements:  CREATE causes an object (a table, for example) to be created within the database.  ALTER statement permits the user to modify an existing object in various ways -- for example, adding a column to an existing table.  RENAME causes a table’s name to be changed  TRUNCATE deletes all data from a table (non-standard, but common SQL statement).  DROP causes an existing object within the database to be deleted, usually irretrievably.

8 Sub-languages of SQL SQL is made up of three major sub-languages, which are Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). The Data Manipulation Language (DML) is a computer language used to retrieve or modify the content of tables within the database. The DML group is made up of theses statements:  SELECT is used to retrieve or display tuples from the table(s) that satisfy a given condition.  INSERT is used to add tuples (rows) to an existing table  UPDATE is used to modify the values of a set of existing table rows.  DELETE removes zero or more existing rows from a table  All the members in this group modifies the content of the table except the Select statement, which only display the content of the table(s).

9 Sub-languages of SQL The Data Control Language (DCL) is a computer language and a subset of SQL, used to control access to data in a database. The DCL group is made up of theses statements:  GRANT to allow specified users to perform specified tasks  REVOKE to cancel previously granted or denied permissions  Some of the following things or privileges that can be GRANTED TO or REVOKED FROM a user or role are CONNECT, SELECT, INSERT, UPDATE, DELETE, and USAGE.

10 Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations) of a database Syntax: CREATE DATABASE database_name ; CREATE TABLE table_name ( column_name1 data_type constraint, column_name2 data_type,....... )

11 数据类型符号标识数据类型符号标识 整数型 bigint 、 int 、 smallint 、 tinyint 精确数值型 decimal 、 numeric 浮点型 float 、 real 货币型 money 、 smallmoney 位型 bit 字符型 char 、 varchar Unicode 字 符型 nchar 、 nvarchar 文本型 text 、 ntext 二进制型 binary 、 varbinary 日期时间类型 datetime 、 smalldatetime 时间戳型 timestamp 图像型 image 其他 cursor 、 sql_variant 、 table 、 uniqueidentifier Data type

12 Domain Types in SQL char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximum length n. 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 user-specified precision of p digits, with d digits to the right of decimal point. real, double precision. Floating point and double-precision floating point numbers, with machine-dependent precision. decimal(n,s). Floating point number, with user-specified precision of at least n digits.

13 Integrity Constraints on Tables not null primary key (A 1,..., A n ) Example: Declare branch_name as the primary key for branch.create table branch (branch_namechar(15), branch_citychar(30) not null, assetsinteger, primary key (branch_name))

14 Drop and Alter Table Constructs The drop table command deletes all information about the dropped relation from the database. The alter table command is used to add attributes to an existing relation: alter table r add A D where A is the name of the attribute to be added to relation r and D is the domain of A.  All tuples in the relation are assigned null as the value for the new attribute. The alter table command can also be used to drop attributes of a relation: alter table r drop A where A is the name of an attribute of relation r  Dropping of attributes not supported by many databases 

15 Summary From this lecture you can learn the basic syntax of data definition language. Create table Primary key Unique Not null Default Foreign key

16 Any Questions? If there are any outstanding questions you can ask me one-to-one after the lecture OR privately in my office.

17 Exercises Create the company schema on the machine


Download ppt "Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio."

Similar presentations


Ads by Google