内容摘要 创建带主键和约束的表 创建带组合主键和外键的表 1. 创建带主键和约束的表 Student (学生表) CREATE TABLE Student ( sclass ) NOT NULL, --- 限制非空 snumb ) PRIMARY KEY, --- 设置为主键 sname ) NOT NULL, --- (下)设置默认值男,约束选项为男女 sgender ) DEFAULT '男' CONSTRAINT consgender CHECK (sgender IN('男', '女')…
IF EXISTS (SELECT * FROM sys.all_objects  WHERE  type_desc= N'主键名')begin --删除主键 alter table 表名 drop constraint  主键名alter table 表名 add constraint 主键名 primary key (字段名)end…
游标cursor,我想大多数人都在sql server里面用过.当一个表数据量不太大的时候,游标还是可以用的,毕竟游标是循环一个表中每一行数据的最简便办法.但是如果你用一个游标去循环一个没有主键或唯一键的表会发生什么呢? 我们来看看这个例子,我们声明了一个临时表#Foo并插入了一行数据,这个表没有主键,然后我们使用了一个名叫ID的游标去更新这个表[Name]列的数据,执行下面的语句看看会发生什么? CREATE TABLE #Foo( ,), ) NULL, ) NULL, [ProvinceI…
0.目录 1.定义 1.1 什么是主键和外键 1.2 主键和外键的作用 1.3 主键.外键和索引的区别 2.主键(primary key) 2.1 通过SSMS设置主键 2.2 通过SQL语句设置主键 3.外键(foreign key) 3.1 通过SSMS设置外键 3.2 通过SQL语句设置外键 4.SQL语句向已存在表设置主键和外键 4.1 已存在表设置主键 4.2 已存在表设置外键 1.定义 1.1 什么是主键和外键 关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能…
--删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter table Stu_PkFk_SC drop constraint FK_c --添加外键 语法:alter table 表名 add constraint 外键约束名 foreign key(列名) references 引用外键表(列名) 如: alter table Stu_PkFk_Sc …
--删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter table Stu_PkFk_SC drop constraint FK_c --添加外键 语法:alter table 表名 add constraint 外键约束名 foreign key(列名) references 引用外键表(列名) 如: alter table Stu_PkFk_Sc…
primary key 1.最简单的: CREATE TABLE t1( id int not null, name char(20)); 2.带主键的: a:CREATE TABLE t1( id int not null primary key, name char(20)); b:复合主键CREATE TABLE t1( id int not null, name char(20), primary key (id,name)); 3.带默认值的: CREATE TABLE t1( id…
原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral 一.动态SQL 相信大家在用mybatis操作数据库时时都会碰到一个问题,假如现在我们有一个关于作者的list authorList,需要根据authorList里已有的作者信息在数据库中查询相应作者的博客信息.那么最容易想到的做法就是遍历authorList,获取相应的信息查询数据库. for(int i=0;I &l…
select utc.column_name, utc.data_type, utc.data_length, utc.data_precision, utc.data_Scale, utc.nullable, utc.data_default, ucc.comments, utc.table_name from user_tab_columns utc, user_col_comments ucc where utc.table_name = ucc.table_name and utc.co…
http://blog.csdn.net/zh2qiang/article/details/5323981 SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入. 1. SQLServer 自增主键创建语法:identity(seed, increment)其中seed 起始值increment 增量示例:create table student(      id int identity(1,1),      name varchar(100)) 2. 指定自…