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. supersr--NSURLConnection iOS2.0苹果原生请求

    get请求1:   NSURL*url = [NSURLURLWithString:@"http://127.0.0.1/demo.json"];        NSURLRequ ...

  2. [Android Pro] AES加密

    reference to :http://blog.csdn.net/wfung_kwok/article/details/7766427 package com.secufity.aes; impo ...

  3. [Android Pro] Android以root起一个process[shell脚本的方法]

    reference to :  http://***/Article/11768 有时候我们写的app要用uid=0的方式启动一个process,framework层和app层是做不到的,只有通过写脚 ...

  4. C++面试之GetMemory问题

    http://blog.csdn.net/zhuxiaoyang2000/article/details/8084629 #include <iostream> #include < ...

  5. 再来一发!DB2 应用程序如何从数据库取数据 Fetch

    The FETCH statement positions a cursor on the next row of its result table and assigns the values of ...

  6. Jmeter测试JDBC

    Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{dbname} Postg ...

  7. php 复习

    <?php 一.php基础语法1.输出语句:echo print print_r var_dump() 2.php是弱类型语言强制转换类型: (类型)变量 settype(变量,类型) 3.变量 ...

  8. Redis不同数据类型的的数据结构实现

    我们知道Redis支持五种数据类型, 分别是字符串.哈希表(map).列表(list).集合(set)和有序集合,和Java的集合框架类似,不同数据类型的数据结构实也是不一样的. >>Re ...

  9. Jquery自定义扩展方法(二)--HTML日历控件

    一.概述 研究了上节的Jquery自定义扩展方法,自己一直想做用jquery写一个小的插件,工作中也用到了用JQuery的日历插件,自己琢磨着去造个轮子--HTML5手机网页日历控件,废话不多说,先看 ...

  10. 【翻译十一】java-原子性操作

    Atomic Access In programming, an atomic action is one that effectively happens all at once. An atomi ...