SQL Server
查询 card 的记录为两次以上的 card,记录数:
select count(card), card from TableName group by card having count(card) > 1
级联更新,级联删除:
ColumnName type not null constraint FK_Name foreign key(ColumnName) references PrimaryTable(ColumnName) on update/delete cascade
自动计算列:
create table TableName
(
ID int,
Price decimal(10,2) not null,
Number int not null,
Total as Price * number -- Total 不存储值。
)
insert into TableName values(1, 100, 123)
insert into TableName values(1, 100, 123)
insert into TableName values(2, 200, 456)
insert into TableName values(2, 200, 456)
select ID, Total=SUM(Total) from TableName where ID = 1 group by ID
修改现有商品数量:
begin
begin tran
declare @ForeignError int, @PrimaryError int
insert into ForeignTable values(@ID, @Number)
select @ForeignError=@@Error -- @@Error: 返回执行的上一个 Transact-SQL 语句的错误号。
update PrimaryTable set Number=Number ± @Number where ID=@ID --进货时用加号。
select @PrimaryError=@@Error
if (@ForeignError = 0 and @PrimaryError = 0)
commit tran
else
rollback tran
end
begin
begin tran
declare @ForeignError int, @PrimaryError int, @NewNumber int
update ForeignTable set Number=@Number, @NewNumber=@Number - Number where ID=@ID
select @ForeignError=@@Error
update PrimaryTable set Number=Number ± @NewNumber where ID=@ID --进货时用加号。
select @PrimaryError=@@Error
if (@ForeignError = 0 and @PrimaryError = 0)
commit tran
else
rollback tran
end
相关文档:
与持久表一样,优化器创建并维护临时表的分布统计信息,并跟踪它的基数。当索引临时表时,这种能力尤其重要。当优化器需要评估选择性时,它就可以根据这些分布统计信息生成经过优化的计划。这是临时表在性能方面不同于表变量的主要特性之一。
此外,因为临时表会维护统计信息,如果上次编译后被引用表有足够多的行发 ......
Next up in the Inside the Storage Engine series is a discussion of page structure. Pages exist to store records. A database page is an 8192-byte (8KB) chunk of a database data file. They are aligned on 8KB boundaries within the data files, starting at byte-offset ......
在数据库中,游标是一个十分重要的概念。游标提供了一种对从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标总是与一条T_SQL 选择语句相关联,因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指 ......
1.监控当前数据库谁在运行什么SQL语句
SELECT osuser, username, sql_text from v$session a, v$sqltext b
where a.sql_address =b.address order by address, piece;
2.分析表
analyze table tablename compute statistics for all indexes;
analyze table tablename compute statistics for all indexed col ......
/*
use master
go
if DB_ID('UserImage') is not null
drop database UserImage
create database UserImage
go
use UserImage
go
create table Images
(
Image_Name nvarchar(255) primary key,
Image_Data Image not null
)
go
create proc InsertImage
(
@Image_Name nv ......