当前位置:首页 - SQL

常用数据库sql

作者:高景洋 日期:2014-08-01 11:05:35 浏览次数:3860
1、随机读取数据库中的相应数据
select top 15 * from users order by Newid()

2、添加列
alter table dbo.[3Dtb] add productid int  
添加列并设置默认值
alter table Admin add AdminType int not null Default(0)

3、修改列
alter table users alter column LoginCode varchar(100)

4、distinct去除重复数据
select distinct TyreBrand from Tyre

5、删除列
ALTER   TABLE   t   DROP   COLUMN   c

6、清理数据库日志
DUMP TRANSACTION 数据库名 WITH NO_LOG

7、将一个表中的数据复制到另一个表中

select * into KQKH_Item20120215 from KQKH_Item (库中不存在指定表)


insert into A.TableName(Url,WebsiteID,SyncDataTypeID,SyncOperationTypeID,Detail,EnteredDate,MsgLevel) 
(select Url,WebsiteID,SyncDataTypeID,SyncOperationTypeID,Detail,EnteredDate,MsgLevel from B.TableName)( 库中已存在指定表)


8、给现有字段添加默认值
ALTER TABLE dbo.商品 add CONSTRAINT  DF_product_isdeleted DEFAULT 0  FOR IsDeleted

9、将现有字段设置非空
ALTER TABLE dbo.商品 ALTER COLUMN IsDeleted  bit  NOT NULL

10、删除约束
ALTER TABLE dbo.商品 drop CONSTRAINT DF_product_isdeleted 

11、
delete from dbo.商品
where ID in(
SELECT MAX(ID) AS ID
FROM dbo.商品
GROUP BY Url HAVING COUNT(*) > 1

12、

数据库内某一字段重复数据

SELECT 辅助采集字段1 FROM dbo.商品 
where 所属网站=40 and 辅助采集字段1 like '%abc%' 
GROUP BY 辅助采集字段1 
HAVING COUNT(*)>1


本文永久性链接:
<a href="http://r4.com.cn/art45.aspx">常用数据库sql</a>
当前header:Host: r4.com.cn X-Host1: r4.com.cn X-Host2: r4.com.cn X-Host3: 127.0.0.1:8080 X-Forwarded-For: 18.223.241.111 X-Real-Ip: 18.223.241.111 X-Domain: r4.com.cn X-Request: GET /art45.aspx HTTP/1.1 X-Request-Uri: /art45.aspx Connection: close Accept: */* User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com) Cookie: aoji_uuid=8ad36748b8d94dd088f3c4b3dbac67fd_20241222; aoji_session_uuid=b9b80f7785c846b6adb216b4f0173c23_20241222 Accept-Encoding: gzip, br, zstd, deflate