zoukankan      html  css  js  c++  java
  • 数据库的SQL语句创建和主外键删除操作

    复制代码
    1 create table UserType 
    2 
    3 (  
    4 
    5 Id int  primary key identity(1,1),  Name nvarchar(25) not null 
    6 
    7 ) go
    复制代码
    复制代码
    1 create table UserInfo
    2 
    3  (  
    4 
    5 Id int primary key identity(1,1),  LoginPwd varchar(50) not null,  LoginName varchar(50) not null,  Name varchar(25) not null,  Gender bit not null default 1 check(Gender=1 or Gender=0),  Email varchar(25) not null,  Adress nvarchar(50) not null,  Phone int not null,  BlogDespt nvarchar(50) default '这个人很懒,什么都没有留下',  UserTypeId int foreign  key  references UserType (Id) 
    6 
    7 ) 
    8 
    9 go
    复制代码
    复制代码
    1 create table BlogType 
    2 
    3 (  
    4 
    5 Id int primary key identity(1,1),  Name nvarchar(30) not null 
    6 
    7 )
    8 
    9  go
    复制代码
    复制代码
    1 create table Blog
    2 
    3  ( 
    4 
    5  Id int primary key identity(1,1),  Title Nvarchar(100) not null,  Summary nvarchar(max),  Essay text,  BlogData datetime not null default getdate(),  Clicks int not null default 0,  BlogTypeId int foreign key references BlogType (Id),  UserId int foreign key references UserInfo (Id)
    6 
    7  ) go
    复制代码
    复制代码
    1 create table Comment
    2 
    3  (  
    4 
    5 Id int primary key identity(1,1),  CommentText text not null,  Commenttary int not null default 0,  CommentDate datetime not null default getdate(),  CommentBlog int foreign key references Blog (Id) 
    6 
    7 ) go
    复制代码
    复制代码
     1 insert into UserType (Name)values('管理员') insert into UserType(Name)values('普通用户')
     2 
     3 insert into UserInfo (LoginPwd,LoginName,Name,Gender,Email,Adress,Phone,BlogDespt,UserTypeId) values ('123', 'Admin', '当时明月', 0, 'admin@blog.com', '北京市-海淀区', '13811389272','我是管理员,欢迎光临我的个人博客', 1)
     4 
     5 insert into BlogType (Name)values('体育') insert into BlogType (Name)values('财经') insert into BlogType (Name)values('房产') insert into BlogType (Name)values('娱乐') insert into BlogType (Name)values('计算机技术') insert into BlogType (Name)values('其他')
     6 
     7 insert into Blog(Title,Summary,Essay,BlogTypeId,Clicks) values('这个我的第一篇博文','开通博客','大家好,我刚刚开通了博客,希望可以在这里交到更多的朋友!',6,0)
     8 
     9  
    10 
    11 insert into Comment(CommentText,Commenttary,CommentBlog) values('好',0,1) go
    12 
    13 insert into Comment(CommentText,Commenttary,CommentBlog) values('很好!',0,1) go
    复制代码

    SQL Server中有主外键约束关系的表删除问题作者:博博 orderforcard中存在外键对应表client中的主键。当用户买卡时会在client表中添加记录,当交易被撤消时client中的记录要删除同时orderforcard表中的记录也要随之删除。这时可以采用下面的方法。

    第一种(这种方法不好): 1、禁用约束 alter   table   ×××   nocheck   constraint   all 2、删除数据 delete   from   ××× 3、恢复约束 alter   table   ×××   check   constraint   all 第二种方法:     采用级联的方法,当含有主键的表中的数据删除时,外键表的数据自动进行删除操作。 alter table dbo.OrderForCard    add constraint FK_ORDERFOR_REFERENCE_CLIENT foreign key (ClientId)       references dbo.Client (ClientId)          on delete cascade  这样在进行删除数据的时候就不用两张表中的数据依次进行删除了而直接删除主表中的记录就可以了,含有外键的记录自动就随之删除了。

    SQL级联删除——删除主表同时删除从表——同时删除具有主外键关系的表  create table a ( id  varchar(20) primary key, password varchar(20) not null )

    create table b ( id int identity(1,1)  primary key, name varchar(50) not null, userId varchar(20), foreign key (userId) references a(id) on delete cascade ) 表B创建了外码userId 对应A的主码ID,声明了级联删除 测试数据: insert a values ('11','aaa') insert a values('23','aaa') insert b values('da','11') insert b values('das','11') insert b values('ww','23') 删除A表内id为‘11’的数据,发现B表内userId 为“11”也被数据库自动删除了,这就是级联删除 delete a where id='11'

    create table A 
    ( 
    Id int primary key identity,
    names nvarhcar(50),
    foreign key(Id) references B(Id) --b表的id作为a表的外键
    ) 
    create table B
    (
    Id int primary key identity,
    names
    )
  • 相关阅读:
    java学习笔记(5)
    java学习笔记(4)
    java学习笔记(3)
    java学习笔记(2)
    java学习笔记(1)
    很棒的Nandflash资料
    Tx2440_Lcd
    git-github学习心得
    多文档编辑器
    假设检验
  • 原文地址:https://www.cnblogs.com/bosamvs/p/5674504.html
Copyright © 2011-2022 走看看