一阶段: about sql

7
阶阶阶 一: About SQL SQL has 3 struct models ,inner model( include s Stored file ) ,outer model(includes view) a nd model(includes base table). SQL 阶 3 阶阶阶 阶阶阶阶 阶阶阶阶阶 阶 阶阶阶阶 阶阶阶阶阶 阶阶阶阶 阶阶阶阶阶阶 ,(),()() base t able existed in the database .A base table ha ve a Stored file.but a base table may has man y indexs which also stored in the Stored file. 阶阶阶阶阶阶 阶阶阶阶阶阶 阶阶阶阶阶阶阶阶阶阶 阶阶阶阶阶阶阶阶阶阶阶 一一,。。 However view isn’t existed in the database .i t is derived from the base table .so ,we can say it is just a invisival table. 阶阶阶阶阶阶阶阶阶阶阶阶 阶阶阶阶阶阶阶阶阶阶 阶阶阶阶阶阶 阶阶阶阶 ,,一。

Upload: chynna

Post on 19-Jan-2016

79 views

Category:

Documents


1 download

DESCRIPTION

一阶段: About SQL. SQL has 3 struct models ,inner model( includes Stored file ) ,outer model(includes view) and model(includes base table). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 一阶段: About SQL

一阶段: About SQLSQL has 3 struct models ,inner model( includes Stored file ) ,outer model(includes view) and model(includes base table).

SQL 有 3 个模式,内模式(存储文件),外模式(包括视图)和模式(包括基本表) base table existed in the database .A base table have a Stored file.but a base table may has many indexs which also stored in the Stored file.

一个表只对应一个存储文件,但可以对应多个索引。索引也存在存储文件中。However view isn’t existed in the database .it is derived from the base table .so ,we can say it is just a invisival table.

视图不独立存在于数据库中,是从基本表导出来的,可以说它是一个虚表。

Page 2: 一阶段: About SQL

For example ,if hava two tables like these

字段名称 说 明

cardID 卡号 主键

curType

货币种类 必填,默认为 RMB

savingType

存款类型 活期 / 定活两便 / 定期

openDate

开户日期 必填,默认为系统当前日期

openMoney

开户金额 必填,不低于 1 元

balance

余额 必填,不低于 1 元 , 否则将销户

pass 密码 必填IsReportLoss

是否挂失

必填,是 / 否值,默认为”否”

customerID

顾客编号 外键,必填

字段名称 说 明

customerID

顾客编号

自动编号(标识列),从 1 开始,主键

customerName

开户名 必填

PID身份证号

必填,只能是 18位或 15 位,身份证号唯一约束

telephone联系电话

必填,格式为 xxxx-xxxxxxxx 或手机号 13 位

address居住地址 可选输入

Page 3: 一阶段: About SQL

第三阶段:举例子

SELECT * FROM cardInfo WHERE (DATEDIFF(Day,getDate(),openDate)<DATEPART(weekday,openDate))SELECT DISTINCT cardID FROM transInfo WHERE transMoney=(SELECT Max(transMoney) FROM transInfo)SELECT customerName as 客户姓名 ,telephone as 联系电话 FROM userInfo WHERE customerID IN (SELECT customerID FROM cardInfo WHERE IsReportLoss=1)SELECT,.., FROM userInfo INNER JOIN cardInfo ON userInfo.customerID =cardInfo.customerID WHERE balance<200

If you had created a view , And if you want to select informations fr

om the tables ,you can easily get the information throught the

views.如果你已经定义了一个视图,你可以通过视图很容易的得到你想查询的信

息,如右图:

If you had created a view , And if you want to select informations fr

om the tables ,you can easily get the information throught the

views.如果你已经定义了一个视图,你可以通过视图很容易的得到你想查询的信

息,如右图:

This is the view

Page 4: 一阶段: About SQL

第四阶段 : 展示如何建立课本第 82 页中的 3个表

字段名称 说 明Sno 学号 主键Sname 姓名 必填Ssex 性别 必填Sage 年龄 必填Sdept 所在系 必填

Create table [dbo].[Student][Sno] [nchar] (20) not null,[Sname] [nchar] (20) not null,[Ssex] [nchar] (20) not null,[Sage] [int] (20) not null,[Sdept] [nvarchar] (20) not null,

Alter table [dbo].[Student]Add Constraint PK_SnoPramiry key (Sno)

一 建 Student 表 二 增加表主键约束 PK_Sno

Page 5: 一阶段: About SQL

Create table [dbo].[Course][Cno] [nchar] (20) not null,[Cname] [nchar] (20) not null,[Cpno] [nchar] (20) not null,[Ccredit] [int] (20) not null,

Alter table [dbo].[Student]Add Constraint PK_SnoPramiry key (Cno)

一 建 Course 表 二 增加表主键约束 PK_Sno

字段名称 说 明Cno 课程号 主键Cname 课程名 必填Cpno 先行课 必填Ccredit 学分 必填

Page 6: 一阶段: About SQL

Create table [dbo].[SC][Cno] [nchar] (20) not null,[Sno] [nchar] (20) not null,[Grade] [int] (20) not null,

Alter table [dbo].[Student]Add Constraint FK_CnoForeign key (Cno)References Course ( Cno )Add Constraint FK_SnoForeign key (Sno)References Student(Sno)

一 建 SC 表

字段名称 说 明Cno 课程号 外键Sno 课程名 外键Grade 先行课 必填

二 增加表外键约束

Page 7: 一阶段: About SQL

My speech is over. Thank you !!