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. yii php 图片上传与生成缩略图

    今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图              ($pic_action_url = $this->createAbsoluteUrl('h ...

  2. Qt 扫描进程列表以及获取进程信息

    使用方法: QMap<QString,qint64> app_pid; getAllAppPidList( app_pid ); #include <tlhelp32.h>// ...

  3. July 5th, Week 28th Tuesday, 2016

    If you smile when no one else is around, you really mean it. 独处的时候你的笑容才是发自内心的笑容. Human beings are so ...

  4. Callable 和 Future接口 学习

    * Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务. * Callable和Runnable有几点不同: * (1)C ...

  5. yii2.0框架安装心得

    yii2.0安装心得 能够搜索到这篇文章的朋友相信是对yii框架有兴趣的,但是我不得不吐槽的是,这个安装过程确实让人头疼,接下来就让大家见证一下这个纠结的过程 根据官网的说法,安装这个框架需要用到co ...

  6. 12.享元模式(Flyweight Pattern)

    using System; using System.Collections; namespace ConsoleApplication5 { class Program { /// <summ ...

  7. 菜鸟学Linux命令:端口查看和操作命令

    >>端口和进程 端口不是独立存在的,它是依附于进程的.某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了.下次若某个进程再次开启,则相应的端口也再次开启. >> ...

  8. 【翻译十】java-固定锁和同步

    Intrinsic Locks and Synchronization Synchronization is built around an internal entity known as the  ...

  9. HDU5115 Dire Wolf(区间DP)

    渐渐认识到区域赛更侧重的是思维及基本算法的灵活运用,而不是算法的量(仅个人见解),接下来要更多侧重思维训练了. 区间DP,dp[i][j]表示从i到j最终剩余第i 与第j只的最小伤害值,设置0与n+1 ...

  10. HDU 1227 Fast Food

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...