Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components management studio.
-
Upload
lesley-garrett -
Category
Documents
-
view
237 -
download
0
Transcript of Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components management studio.
Chapter 7
SQLHUANG XUEHUA
SQL
SQL server2005 introduction
Install components
management studio
SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification of the Database Joined Relations**
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.
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.
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).
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.
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, ....... )
数据类型 符号标识 数据类型 符号标识整数型 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
Domain Types in SQL
char(n). Fixed length character string, with user-specified length n. varchar(n). Variable length character strings, with user-specified maximu
m length n. int. Integer (a finite subset of the integers that is machine-dependent). smallint. Small integer (a machine-dependent subset of the integer domai
n type). numeric(p,d). Fixed point number, with user-specified precision of p digi
ts, 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 le
ast n digits.
Integrity Constraints on Tables
not null primary key (A1, ..., An )
Example: Declare branch_name as the primary key for branch. create table branch
(branch_name char(15), branch_city char(30) not null, assets integer, primary key (branch_name))
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
Summary
From this lecture you can learn the basic syntax of data definition language.
Create table Primary key Unique Not null Default Foreign key
Any Questions?If there are any outstanding questions you can ask me one-to-one after the lecture OR privately in my office.
Exercises
Create the company schema on the machine