• SQL Server 2008 设计与实现笔记(一)


    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','123'),
                ('A00001','123');
    
    --查看约束(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 cascadeset nullset default
    View Code
  • 相关阅读:
    “显示桌面”代码
    Jquery 判断CheckBox是否选中
    Jquery 得到隐藏列的值
    Jquery 得到DataGrid单击单元格后得到主键列值
    RadioButtonList的项增加onClick事件
    正则表达式 替换除中文、字母、数字以外的字符
    正则表达式中 中文 Unicode字符(转)
    Jquery 设置table、DataGrid等的某列单击的方法
    GridView自动生成列的隐藏
    AutoCAD利用VB交互创建应用程序交互
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/3147990.html
Copyright © 2020-2023  润新知