Chart5

create database MovieRental;

select name, SUSER_SNAME(sid) as [login]
from sys.database_principals
where name='dbo'; alter authorization on Database::MovieRental to easy5; /*
架构(schema)
*/
create SCHEMA Inventory;
GO
create SCHEMA People;
Go
create schema Rentals;
GO
create schema Alt;
GO select name,
SCHEMA_NAME(schema_id) as schemaName,
USER_NAME(principal_id) as principal
from MovieRental.sys.schemas; /*
Create Table
*/
create table Inventory.Movie
(
MovieId int not null,
Name nvarchar(20) not null,
ReleaseDate date null,
Description nvarchar(200) null,
GenrentId int not null,
MovieRatingId int not null
); --IDENTITY只能在如下情况下建立:
--在创建表时创建新的IDENTITY列
--在现有表中创建新的IDENTITY列 --不能 把已经存在的列,修改为IDENTITY列
Drop Table Inventory.Movie;
GO
create table Inventory.Movie
(
MovieId int not null Identity(0,1),
Name nvarchar(20) not null,
ReleaseDate date null,
Description nvarchar(200) null,
GenrentId int not null,
MovieRatingId int not null
); create table Inventory.Movie
(
MovieId int not null,
Name nvarchar(20) not null,
ReleaseDate date null,
Description nvarchar(200) null,
GenrentId int not null,
MovieRatingId int not null
); alter table Inventory.Movie select table_name
from MovieRental.INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA ='Inventory'; create table Inventory.MovieRating
(
MovieRatingId int not null,
Code nvarchar(20) not null,
Description nvarchar(200) null,
AllowYouthRentalFlag bit not null
); Drop table Inventory.MovieRating;
Go
create table Inventory.MovieRating
(
MovieRatingId int not null identity(0,1),
Code nvarchar(20) not null,
Description nvarchar(200) null,
AllowYouthRentalFlag bit not null
); create table Inventory.Genre
(
GenreId int not null,
Name nvarchar(20) not null
);
insert into Inventory.Genre(GenreId, Name)
values(1,'Comedy'),
(2,'Drama'),
(3,'Thriller'),
(4,'Documentary'); drop table Inventory.Genre;
GO
create table Inventory.Genre
(
GenreId int not null identity(0,1),
Name nvarchar(20) not null
); --insert into Inventory.Genre(GenreId, Name)
--values(1,'Comedy'),
--(2,'Drama'),
--(3,'Thriller'),
--(4,'Documentary'); insert into Inventory.Genre(Name)
values
('Comedy'),
('Drama'),
('Thriller'),
('Documentary'); /*------------------------------------------------
constraint(约束)
*/
-----------------------
--主键(PK)primay key create table Inventory.MovieFormat(
MovieFormatId int not null identity(1,1)
constraint PKInventory_MovieFormat primary key clustered, Name nvarchar(20) not null
); insert into inventory.MovieFormat(Name)
values('Video Tape'),
('DVD'); alter table Inventory.Movie
add constraint PKInventory_Movie primary key clustered(MovieId); alter table Inventory.MovieRating
add constraint PKInventory_MovieRating primary key clustered(MovieRatingId); alter table Inventory.Genre
add constraint PKInventory_Genre primary key clustered(GenreId); -------------------------------------
--候选键(AK)Unique
create table Inventory.Personality
(
PersonalityId int not null identity(1,1)
constraint PKInventory_Personality primary key,
FirstName nvarchar(20) not null,
LastName nvarchar(20) not null,
NameUniqueifier nvarchar(5) not null, constraint AKInventory_Personality_PersonName
unique(FirstName, LastName,NameUniqueifier)
); alter table Inventory.Genre
add constraint AKInventory_Genre_Name unique(Name); alter table Inventory.MovieRating
add constraint AKInventory_MovieRating_Code unique(code); alter table Inventory.Movie
add constraint AKinventory_movie_NameAndData unique nonclustered(Name,ReleaseDate); ---------------------------------------
--选择唯一性(AFK) unique index
drop table alt.employee;
Go
create table alt.employee
(
employeeId int not null identity(1,1)
constraint PKalt_employee primary key,
employeeNumber nvarchar(10) not null
constraint AKalt_employee_employeeName Unique,
insurancePolicyNumber nvarchar(20) null
); --Sql server 2008 通过“经筛选的索引”实现“选择唯一性”
create unique index AKFalt_employee_insurancePlicyNumber
on alt.employee(insurancePolicyNumber)
where insurancePolicyNumber is not null; --InsurancePolicyNumber列的值:not null的值必须唯一,null可以有多个 --123属于not null:只能唯一,不能重复,执行出错
--insert into alt.employee(employeeNumber, insurancePolicyNumber)
-- values('A00001','123'),
-- ('A00002','123'); insert into alt.employee(employeeNumber, insurancePolicyNumber)
values('A00003',null),
('A00004',null); create table alt.employee2
(
employeeId int not null identity(1,1)
constraint PKalt_employee2 primary key,
employeeNumber nvarchar(10) not null
constraint AKalt_employee_employeeName2 Unique,
insurancePolicyNumber nvarchar(20) null
);
--Sql server 2008 通过“创建索引视图”实现“选择唯一性”
create view alt.employee2_InsuancePolicyNumberUniquess
with schemabinding
as
select insurancePolicyNumber
from alt.employee2
where insurancePolicyNumber is not null; insert into alt.employee2(employeeNumber, insurancePolicyNumber)
values ('A00001',''),
('A00001',''); --查看约束(constraint)
select TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
--where CONSTRAINT_SCHEMA = 'Inventory'
order by CONSTRAINT_NAME, TABLE_NAME --默认值约束(DFL)default
create table Rentals.MovieRental
(
MoviecRentalId int not null identity(1,1)
constraint PKRentals_MovieRental primary key,
ReturnDate date not null
constraint DELRentals_MovieRental_ReturnDate default(GetDate()),
ActualReturnDate date null, ); alter table Rentals.MovieRental
add constraint DELRentals_MovieRental_ActualReturnDate
default(DateAdd(DAY,4,GetDate()))
for ActualReturnDate; alter table Rentals.MovieRental
add customerId int not null; insert into Rentals.MovieRental(customerId)
values(1); ----------------------------------
--外键(FK)
--联级4种方式: no action \cascade\set null\set default

SQL Server 2008 设计与实现笔记(一)的更多相关文章

  1. 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)

    <Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...

  2. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化

    一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...

  3. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1

    (5)SELECT   (5-2) DISTINCT    (5-3)TOP(<top_specifications>)   (5-1)<select_list> (1)FRO ...

  4. 《Microsoft SQL Server 2008 Internals》读书笔记

    http://www.cnblogs.com/downmoon/archive/2010/01/26/1656411.html

  5. sql server 2008 设计时 不允许保存更改

    什么 都不说了 上图

  6. 《Microsoft SQL Server 2008 Internals》读书笔记--目录索引

    http://blog.csdn.net/downmoon/article/details/5256548 https://sqlserverinternals.com/companion/

  7. sql server 2008 创建新数据库报错、创建表报错、更改表的设计报错

    一:创建数据库报错如下: 二:解决,将软件以管理员身份运行 三:创建表报错如下图: 四:解决办法,在你创建的数据库下面的安全里,找到你创建的用户,属性,添加权限,红色标注,然后确定: 五:更改表的设计 ...

  8. MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九

    <Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次   SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...

  9. sql server 2008笔记

    sql server 2008开启远程访问数据库 1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策 ...

随机推荐

  1. 在VS2010中使用附加进程的方式调试IIS中的页面

    h3{background:#333333; } 准备篇-配置IIS环境 在发布网站之前,需要安装iis环境! 之后点击确定即可! 发布网站至IIS-附加到进程调试 1.       用VS2010将 ...

  2. 在web界面调用水晶报表导出文件时莫名错误

    原因是水晶报表未破解版有字段限制,不能超过90(具体个数没仔细测)个字段. 建议那些select *的朋友检查一下字段个数

  3. markdown编辑

    有用的技巧之,如何实现首行空两格,把输入法切换成全角(shift+space),就可以实现一个两字符的空格了. 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 w s r s r s 文本 ...

  4. 腾讯QQ群数据下载方法(7000万个qq群资料全泄漏)

    仔细读完一定能找到自己需要的东西 据新华网报道,国内知名安全漏洞监测平台乌云20日公布报告称,腾讯QQ群关系数据被泄露,网上可以轻易就能找到数据下载链接,根据这些数据,通过QQ号可以查询到备注姓名.年 ...

  5. 如何找出MySQL数据库中的低效SQL语句

    面对业务的迅猛发展,DBA的一项重要工作就是及时发现数据库中的低效SQL语句,有的可以立刻着手解决(比如缺少合适的索引),有的需要尽快反馈给开发人员进行修改. MySQL数据库有几个配置选项可以帮助我 ...

  6. linux中FTP自动备份VPS脚本

    服务器多了,网站也越来越多,总觉得不整个备份心里放不下心,并且有好几次rm的操作失误,造成难以挽回的损失.并且大多数的VPS提供商是不提供自动备份功能或者此功能收费价格略高.所以自己还是有必要把这个工 ...

  7. [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)

    下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...

  8. HTML5之 Microdata微数据

    - 为何需要微数据 长篇加累版牍,不好理解 微标记来标注其中内容,让其容易识辨 - RDFa Resource Description Framework http://www.w3.org/TR/m ...

  9. 关于webview嵌入swf

    有的机子CPU不支持swf播放的,不知道你是不是中奖了.. webview 加载swf很简单:         if(versionDouble>=2.2){//版本低于2.2的系统无法播放sw ...

  10. WMB代理无法正常启动时的解决方案。

     情况:执行了启动命令,查看状态的时候 还是停止..再启动,又说已经启动了..   解决方案:正常情况在hosts文件加一条记录:主机IP和hostname的映射关系就好了