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. java课后作业

    课后作业之字串加密: 设计思想: 1.输入要加密的英文子串str 2.定义num=str的字符串长度 3.将字符串转化为单个字符 4.每个字符+3,向后移3个 5.定义str1,将新得到的每个字符加到 ...

  2. July 3rd, Week 28th Sunday, 2016

    2016-07-03 First-hand experience is often necessary for personal growth. 亲身体验通常是成长所必须的. Although som ...

  3. function与感叹号

    原文链接:https://swordair.com/function-and-exclamation-mark/ 最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2 ...

  4. WhaleSong

    Chasingwaves by myself in theocean of endless sorrow Makingwishes that i will find myherd tomorrow 5 ...

  5. Orleans是什么 (一)

    官网:http://dotnet.github.io/orleans/ 文档:http://dotnet.github.io/orleans/What's-new-in-Orleans 源码:http ...

  6. CLR via C#(08)-操作符

    对于操作符,我们并不陌生,例如+,-,*,%等二元操作符,以及++,!等一元操作符.但是对于非基元类型,我们需要通过一些自定义方法才能使用这些操作符.今天主要和大家分享关于操作符重载和转换操作符的知识 ...

  7. 理解Java中的final和static关键字

    回顾这两个关键字前,先考虑一个问题: Static变量存储在JVM中的位置,或者说static变量是如何被加载的? JVM会把类的静态方法和静态变量在类加载的过程中读入方法区(Method Area) ...

  8. Android Matrix

    转自 :http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#code Matrix的数学原理 平移变换 旋转变换 缩放变换 错切 ...

  9. c++ 读取txt文件并输出到控制台

    代码如下: #include "stdafx.h" #include<iostream> #include<fstream> #include<cst ...

  10. cmder

    添加cmder到右键菜单 Cmder.exe /REGISTER ALL 打开配置快捷键 win+alt+p 文字重叠 main->font->去掉monospace的勾 λ符号修改 找到 ...