create table UserType 

 (  

 Id int  primary key identity(,),  Name nvarchar() not null 

 ) go
 create table UserInfo

  (  

 Id int primary key identity(,),  LoginPwd varchar() not null,  LoginName varchar() not null,  Name varchar() not null,  Gender bit not null default  check(Gender= or Gender=),  Email varchar() not null,  Adress nvarchar() not null,  Phone int not null,  BlogDespt nvarchar() default '这个人很懒,什么都没有留下',  UserTypeId int foreign  key  references UserType (Id) 

 ) 

 go
 create table BlogType 

 (  

 Id int primary key identity(,),  Name nvarchar() not null 

 )

  go
 create table Blog

  ( 

  Id int primary key identity(,),  Title Nvarchar() not null,  Summary nvarchar(max),  Essay text,  BlogData datetime not null default getdate(),  Clicks int not null default ,  BlogTypeId int foreign key references BlogType (Id),  UserId int foreign key references UserInfo (Id)

  ) go
 create table Comment

  (  

 Id int primary key identity(,),  CommentText text not null,  Commenttary int not null default ,  CommentDate datetime not null default getdate(),  CommentBlog int foreign key references Blog (Id) 

 ) go
 insert into UserType (Name)values('管理员') insert into UserType(Name)values('普通用户')

 insert into UserInfo (LoginPwd,LoginName,Name,Gender,Email,Adress,Phone,BlogDespt,UserTypeId) values ('', 'Admin', '当时明月', , 'admin@blog.com', '北京市-海淀区', '','我是管理员,欢迎光临我的个人博客', )

 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('其他')

 insert into Blog(Title,Summary,Essay,BlogTypeId,Clicks) values('这个我的第一篇博文','开通博客','大家好,我刚刚开通了博客,希望可以在这里交到更多的朋友!',,)

 insert into Comment(CommentText,Commenttary,CommentBlog) values('好',,) go

 insert into Comment(CommentText,Commenttary,CommentBlog) values('很好!',,) 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'

数据库的SQL语句创建和主外键删除操作的更多相关文章

  1. 经典SQL语句大全_主外键_约束

    一.基础(建表.建约束.关系) 约束(Constraint)是Microsoft SQL Server 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件(有关数据完整 ...

  2. SQLserver创建与主外键的看法

    一个.背景 最初研究的相关内容数据库.仅仅是正式.从来没有练过,只能慢慢漂流,现在做的客房时,,非常多的知识需要使用视图,慢的实践. 视图:我理解的就是一张表.它把我们所须要的某个表或某几个表中的部分 ...

  3. SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束

    创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...

  4. MySQL数据库执行sql语句创建数据库和表提示The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have it working

    MySQL创建数据库 只想sql文件创建表时候提示 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to ha ...

  5. SQL Server语句创建数据库和表——并设置主外键关系

    简单的创建数据库的 SQL 语句: use master go if exists(select * from sysdatabases where name='Test') begin select ...

  6. SQL Server 【附】创建"商品管理数据库"、"学生选课数据库"的SQL语句

    附:(创建“商品管理数据库”的SQL语句) --建立"商品管理数据库"数据库-- create database 商品管理数据库 on(name='商品管理数据库_m', file ...

  7. 2-05使用SQL语句创建数据库2

    使用SQL语句创建多个数据文件和日志文件: USE master--指向当前使用的数据库 GO--批处理的标志 CREATE DATABASE E_Market--创建E_market数据库 ON P ...

  8. 用SQL语句创建和删除Access数据库中的表;添加列和删除列

    用SQL语句创建和删除Access数据库中的表;添加列和删除列 Posted on 2009-08-11 13:42 yunbo 阅读(1240) 评论(0) 编辑 收藏 用SQL语句创建和删除Acc ...

  9. sql server 2008 数据库管理系统使用SQL语句创建登录用户步骤详解

    介绍了sql server 2008 数据库管理系统使用SQL语句创建登录用户步骤详解 --服务器角色: --固定服务器角色具有一组固定的权限,并且适用于整个服务器范围. 它们专门用于管理 SQL S ...

随机推荐

  1. 在Eclipse中手动安装pydev插件,eclipse开发python环境配置

    最近在学习Python,因为我是做java的,用惯了eclipse,所以就想用eclipse开发python,但是配置开发环境的时候发现按照网上的配置大多不行,而且都是用的在线安装,很垃圾,没办法,自 ...

  2. 【XLL API 函数】 xlFree

    用于释放使用 Excel4,Excel4v,Excel12,Excel12v 分配的 XLOPER/XLOPER12 占用的内存资源. xlFree 函数释放辅助内存和重置指针为NULL但不释放XLO ...

  3. SQL Server 2008 R2 安装出错:Could not open key

    安装Sql Server 2008 R2 企业版出现错误提示无法继续安装,错误提示为: Could not open key: UNKNOWN\Components\7ABFE44842C12B390 ...

  4. September 29th 2016 Week 40th Thursday

    Prosperity discovers vice, adversity virtue. 得意时露瑕疵,逆境中见品质. I wish I would have someone like you, fr ...

  5. September 8th 2016 Week 37th Thursday

    The secret of high-impact business is early preparation. 高效商务,赢在未雨绸缪. Early and best preparation is ...

  6. window 环境安装MongoDB

    强制安装mongodb服务 命令 sc create MongoDB binPath= "D:\MongoDB\Server\3.2\bin\mongod.exe --service --d ...

  7. Rotate partitions in DB2 on z

    Rotating partitions   You can use the ALTER TABLE statement to rotate any logical partition to becom ...

  8. Memcached驱动(C#)

    using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.IO; usin ...

  9. 攻城狮在路上(壹) Hibernate(十八)--- 管理Hibernate的缓存

    一般Session的缓存被称为Hibernate的第一级缓存,SessionFactory的外置缓存是一个可配置的缓存插件,称为Hibernate的第二级缓存.一.缓存的基本原理: 1.持久化层的缓存 ...

  10. Oracle11g在使用exp导出时不导出空表问题的解决办法

    11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...