sql server 定义主键
drop table father;
create table father(
id int identity(1,1) primary key,
name varchar(20) not null,
age int not null
)
drop table mother;
create table mother(
id int identity(1,1),
name varchar(20) not null,
age int not null,
husband int not null
)
alter table mother
add constraint FK_ID foreign key(husband) references father(id)
如果 在father中没能定义主键,
drop table father;
create table father(
id int identity(1,1), (未定义 primary key)
name varchar(20) not null,
age int not null
)
则会报如下异常:
在被引用表 'father' 中没有与外键 'FK_ID' 的引用列的列表匹配的主键或候选键
相关文档:
SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别:
IN:确定给定的值是否与子查询或列表中的值相匹配。
IN 关键字使您得以选择与列表中的任意一个值匹配的行。
当要获得居住在 California、Indiana 或 Maryland 州的所有作者的姓名和州的列表时,就需要下列查询:
SELECT ProductID, ProductName from Northwind.dbo.Pro ......
在SQL Server中使用NewID()方法产生随机集
例如考试系统中的随机出题
刚开始想到的是Random类
但是Random效率有点低
后来想到了在数据库里的newid()
于是采用了下边方法:
select top 5 * from tablename order by newid()
在此标记一下 ......
簡體數據庫轉繁體數據庫的問題 拜托了
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE DatabaseName COLLATE Chinese_Taiwan_Stroke_CI_AS
ALTER DATABASE DatabaseName SET MULTI_USER WITH ROLLBACK ......
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用 ......