delete from [table] where ID not in (select max(ID) as ID from [table] group by 学号)
2.和上边类似,列出学号重复的记录数:
select max(学号),count(学号) from [table] group by 学号 having count(学号)>0
3.列出数据表的字段select name from syscolumns where id=object_id('表名')
4.收缩数据库(会大大减小数据库和日志):dbcc shrinkdatabase(数据库名)
5.以前匹配如:2006开头的字段,都用left(字段,4)='2006',据说这样效率低,改写成这样:
select 学号 from [表名] where 入学时间 like '2006%'
6.table1记录学生密码,table2记录学生信息,根据table2的学号,自动添加没有的学号到table1中INSERT INTO [table1]
(学号, 姓名, 密码)
SELECT 学号, 姓名, '111111' AS 密码
FROM [table2]
WHERE (NOT EXISTS
(SELECT 学号
FROM [table1]
WHERE [table2].学号 = [table1].学号))
(学号, 姓名, 密码)
SELECT 学号, 姓名, '111111' AS 密码
FROM [table2]
WHERE (NOT EXISTS
(SELECT 学号
FROM [table1]
WHERE [table2].学号 = [table1].学号))
7.接上一个,删除table1中在table2里没有记录的学号
DELETE FROM [table1] WHERE (NOT EXISTS
(SELECT 学号 FROM [table2]
WHERE [table1].学号 = [table2].学号))
(SELECT 学号 FROM [table2]
WHERE [table1].学号 = [table2].学号))