计算机科学与软件工程学院
数据库系统概论
An Introduction to Database System
第三章关系数据库标准语言SQL
An Introduction to Database System
第三章关系数据库标准语言SQL SQL概述
SQL的特点
1. 综合统一: 包含了数据定义、数据操纵、数据控制
2、两种使用方式:独立使用或嵌入高级语言中使用
3. 非过程化:只提操作要求,不必描述操作过程和步骤,也不需要导航
4. 语言简洁,易学易用、好学好用:在ANSI标准中,只包含了94个英文单词,核心功能6个动词,语法接近英语口语
An Introduction to Database System
语言简捷,易学易用
An Introduction to Database System
定义语句格式
CREATE TABLE 表名
(字段1,类型[ not null] [unique],字段2,类型[ not null] [unique]…)
Primary key 和 unique 的区别?
Unique:限定非空的值唯一,没有限定该值不能为空.
Type:
Numric 数值
smallint 半字长二进制整数 INT(integer) 全字
Decimal(DEC) 十进制小数型
Float 双字长浮点数 Real 实数
Double precision 双精度 Character(CHAR) 字符型
An Introduction to Database System
定义语句格式
[例1] 建立一个“学生”表Student,它由学号Sno、姓名Sname、性别Ssex、年龄Sage、所在系Sdept五个属性组成。其中学号不能为空,值是唯一的,并且姓名取值也唯一。
CREATE TABLE Student
(Sno CHAR(5) NOT NULL UNIQUE,
Sname CHAR(20) UNIQUE,
Ssex CHAR(1) ,
Sage INT,
Sdept CHAR(15))
An Introduction to Database System
例:
Create table customer(
name char(20) not null,
street char(30),
city char(30),
primary key (name));
定义语句格式
An Introduction to Database System
例:
Create table branch(
name char(15)not null,
city char(30),
assets decimal ,
primary key (name),
check (assets>=0));
定义语句格式
An Introduction to Database System
Create table account (
branch_ number char(10),
branch_ name char*15),
branch_balance int ,
primary key(number),
foreign key (name)
reference branch(branch_name),
Check(balance>=0));
定义语句格式
An Introduction to Database System
Create table deposite (
customer_name char(20) not null,
account_ number char(10)not null,
primary key(customer_name,account_number),
foreign key(customer_name)reference customer(customer_name),
foreign key(account_number)
reference account(account_number));
外码可以定义多次,建立关系表之间的联系
定义语句格式
An Introduction to Database System
[例2] 建立一个“学生选课”表SC,它由学号Sno、o,修课成绩Grade组成,其中(Sno, Cno)为主码。
Create table sc( sno char(5) ,
cno char(3) ,
grade int,
Primary key (Sno, Cno)
2016-2017学年度高中物理 第八章 气体章末整合提升课件 新人教版选修3-3 来自淘豆网www.taodocs.com转载请标明出处.