数据库操作的一些基本问题
1、往数据库中插入一条记录时(insert into table values('column1','column2'…))怎样在插入空值的时候让其自动补充为默认值??(因为属性列较多,所以不想对单独的列进行添加
,希望能够自动把插入的空值转换为默认值)希望各位前辈能够帮忙解决一下;
2、有一个表的主码是两个属性列组合而成的,我想用

--试试,不知道行不行
create table TableTestPrimayKey(
SNO varchar(50),
BOOKNO varchar(50),
BTIEM DATETIME,
REMARK VARCHAR(200)
)
INSERT INTO TableTestPrimayKey VALUES(NEWID(),NEWID(),GETDATE(),NEWID())
GO 100;
SELECT TOP 5 *
FROM TableTestPrimayKey
WHERE SNO NOT IN (SELECT A.SNO
FROM (SELECT TOP 10 SNO, BOOKNO
FROM TableTestPrimayKey
ORDER BY BTIEM) A
)
AND BOOKNO NOT IN (SELECT A.BOOKNO
FROM (SELECT TOP 10 SNO, BOOKNO
FROM TableTestPrimayKey
ORDER BY BTIEM) A)
;with aaa as
(
select row_number() over(order by col1,col2) as rowindex,* from table1
)
select top (n-m+1) rowindex from aaa where rowindex not in (select top m-1 from aaa)
第一个问题,按楼主说的意思,貌似必须用触发器,用默认值解决不了问题,因为楼主说的是插入空值,而不是未插入值.如果这个列在插入语句中存在,但插入的值是空值,那必须用触发器更改.
create trigger updatenull on tb
INSTEAD OF INSERT
as
begin
--可适用于批量插入
--先插入不空的行
insert into tb select * from inserted where col2 is not null
--再插入为空时的行,设Col2为检测的列,默认值为'xx'
insert into tb select col1,'xx',col3,..... from inserted where col2 is null
end
go
[解决办法]
第二个问题,如果是分页,那必须另产生一个序列号,用这个序列号来进行分页.可用row_number()函数,也可以用identity函数获得临时表再分页.
select ....此处用分页的设置 from(
select row_number()over(order by 学号,书号) as rn,* from ...
)t