杂记 数据库相关
2011-09-24 11:26
429 查看
根据一张表删除另一张表的数据 :
Delete from E22009062501B
From E22009062502B A INNER JOIN E22009062501B B
ON A.ID= B.ID
Where A.Name=B.Name
sql2005附件数据库失败,提示:SQL2005附加数据库"失败........无法更新数据库"xxx",因为数据库是只读。
解决方法(引自网络):
提示:无法更新数据库"xxx",因为数据库是只读的.(Microsoft SQL Server,错误:3906),检查数据库文件非只读的,
后来把mdf的文件拷贝到C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data目录下添加就成功。
可我不习惯把数据库文件放到系统所在分区。郁闷中发现C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data目录安全选项中多了一SQLServer2005MsSQLUser$XXX$MSSQLSERVER的用户,他完全控制该目录。明白了。给目录D:\DBFile\加上对应的用户与操作权限。就oK了。
就是添加SQL用户对该文件的操作权限。
查询数据库字段及其类型
select
a.Name as TableName,
b.Name as ColumnName,
c.Name as TypeName,
b.max_length as Bits,
b.Precision as Ints,
b.Scale as Floats
---into BBB
from Sys.tables a
inner join Sys.columns b on a.object_id=b.object_id
inner join sys.Types c on c.User_Type_ID=b.User_Type_ID
where c.name='nvarchar' and b.max_length=-1
SQL2005 Manage Studio 还原
alter database 你的数据库名 set offline with rollback immediate
安装SQL2000,创建挂起的文件 解决方法:
打开注册表编辑器(或在命令行输入:regedit),在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中找到PendingFileRenameOperations项目,并删除它。
查询数据库中数据表的名称
select name from DataBase.dbo.sysobjects where xtype='u'
sql 默认值
alter table tb
add constraint df_a default 默认值 for username
-----设置默认值
ALTER TABLE [T_ReturnDetails] ADD DEFAULT '0' FOR [ReceiveStatus] WITH VALUES--//添加默认值
sql 删除约束
-----删除列约束
Alter table T_ReturnDetails --table_name表名
Drop constraint DF_T_ReturnDetails_IsReceive --constraint_name约束名
初级sql之根据一张表数据更新另一张表数据:
update Substaion
set Substaion.LocalName=b.ConnectionMode
from Substaion a,EquipmentConnectionMode b
where a.Id=B.MRID
根据字段名查询表
select a.name tableName,b.name columnName from sysobjects a,syscolumns b where a.id=b.id and b.name='columnName' and a.type='U'
是否数字
select isnumeric(RegUnitName) from Table where Column=17800
查询纯字母的行
select * from RegUnits where patindex('%[^A-z]%',RegUnitName)=0
创建唯一性约束:
CREATE UNIQUE INDEX Province_ToDay ON BrowseCount (ProvinceNo, ToDay) --CLUSTERED
查询区分大小写
select * from Users where UserName collate Chinese_PRC_CS_AS= 'YuTian'
日期转换
select CONVERT(varchar(10),getdate(),120)
更改列名
EXEC sp_rename 'T_Partner.PrinterDetect', 'IsPrinterDetect', 'COLUMN'
判断表是否存在
if not exists(select name from sysobjects where name=''+@name+'' and type='u')
begin
-- 创建
end
或
if object_id('[Table1]') is null
create table [Table1] ([col1] int,[col2] int)
[b]给表添加说明
[/b]
[b] EXECUTE sp_addextendedproperty N'MS_Description', '抓取信息的贴吧', N'user', N'dbo', N'table', N'BaiDu_TieBa', NULL, NULL
[/b]
[b][b]给列添加说明
[/b][/b]
[b][b]exec sp_addextendedproperty N'MS_Description', N'贴吧名称', N'user', N'dbo', N'table', N'BaiDu_TieBa', N'column', N'TieBaName'[/b][/b]
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'其他接收人姓名' , @level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'T_DoaMaterial',
@level2type=N'COLUMN',@level2name=N'TransToName'
[b][b][b]修改说明[/b][/b][/b]
execute sp_updateextendedproperty N'MS_Description',N'这是修改后的测试列1',N'SCHEMA',N'dbo',N'table',N'test',N'column',N'col1'
[b][b][b]删除说明
[/b][/b][/b]
-EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','TagsCommon',null,null
--EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','ZipAreaCode',null,null
Sql Server中判断表或者数据库是否存在
1.数据库
if exists(select 1 from master..sysdatabases where name='example')
print 'DataBase existed'
else
print 'Database not existed'
2.表
IF Exists(Select 1 From sysObjects Where Name ='表名' And Type In ('S','U'))
Print 'Exists Table'
Else
Print 'Not Exists Table'
在Sql Server2005中可以简化上述语句
如:
use example
go
if object_id('a','U') is not null
drop table a
go
注:a 是一个表,U代表是数据表类型
类似于U的类型代码,如下所示
对象类型:
AF = 聚合函数 (CLR)
C = CHECK 约束
D = DEFAULT(约束或独立)
F = FOREIGN KEY 约束
PK = PRIMARY KEY 约束
P = SQL 存储过程
PC = 程序集 (CLR) 存储过程
FN = SQL 标量函数
FS = 程序集 (CLR) 标量函数
FT = 程序集 (CLR) 表值函数
R = 规则(旧式,独立)
RF = 复制筛选过程
SN = 同义词
SQ = 服务队列
TA = 程序集 (CLR) DML 触发器
TR = SQL DML 触发器
IF = SQL 内联表值函数
TF = SQL 表值函数
U = 表(用户定义类型)
UQ = UNIQUE 约束
V = 视图
X = 扩展存储过程
IT = 内部表
还原数据库
RESTORE DATABASE [LexmarkMpsDoa]
FROM DISK = N'F:\Project\LexmarkMpsDoa\DataBase\LexmarkMpsDoa_backup_201202180100.bak'
WITH REPLACE
--删除默认值约束
DECLARE @tablename VARCHAR(100), @columnname VARCHAR(100), @tab VARCHAR(100)
SET @tablename='CssConsultation'---表名
SET @columnname='SolutionClass'---列名
declare @defname varchar(100)
declare @cmd varchar(100)
select @defname = name
FROM sysobjects so
JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = @tablename
AND so.xtype = 'D'
AND sc.colid =
(SELECT colid FROM syscolumns
WHERE id = object_id(@tablename) AND
name = @columnname)
SELECT @defname
select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname
if @cmd is null
print 'No default constraint to drop'
ELSE
exec (@cmd)
Delete from E22009062501B
From E22009062502B A INNER JOIN E22009062501B B
ON A.ID= B.ID
Where A.Name=B.Name
sql2005附件数据库失败,提示:SQL2005附加数据库"失败........无法更新数据库"xxx",因为数据库是只读。
解决方法(引自网络):
提示:无法更新数据库"xxx",因为数据库是只读的.(Microsoft SQL Server,错误:3906),检查数据库文件非只读的,
后来把mdf的文件拷贝到C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data目录下添加就成功。
可我不习惯把数据库文件放到系统所在分区。郁闷中发现C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data目录安全选项中多了一SQLServer2005MsSQLUser$XXX$MSSQLSERVER的用户,他完全控制该目录。明白了。给目录D:\DBFile\加上对应的用户与操作权限。就oK了。
就是添加SQL用户对该文件的操作权限。
查询数据库字段及其类型
select
a.Name as TableName,
b.Name as ColumnName,
c.Name as TypeName,
b.max_length as Bits,
b.Precision as Ints,
b.Scale as Floats
---into BBB
from Sys.tables a
inner join Sys.columns b on a.object_id=b.object_id
inner join sys.Types c on c.User_Type_ID=b.User_Type_ID
where c.name='nvarchar' and b.max_length=-1
SQL2005 Manage Studio 还原
alter database 你的数据库名 set offline with rollback immediate
安装SQL2000,创建挂起的文件 解决方法:
打开注册表编辑器(或在命令行输入:regedit),在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager中找到PendingFileRenameOperations项目,并删除它。
查询数据库中数据表的名称
select name from DataBase.dbo.sysobjects where xtype='u'
sql 默认值
alter table tb
add constraint df_a default 默认值 for username
-----设置默认值
ALTER TABLE [T_ReturnDetails] ADD DEFAULT '0' FOR [ReceiveStatus] WITH VALUES--//添加默认值
sql 删除约束
-----删除列约束
Alter table T_ReturnDetails --table_name表名
Drop constraint DF_T_ReturnDetails_IsReceive --constraint_name约束名
初级sql之根据一张表数据更新另一张表数据:
update Substaion
set Substaion.LocalName=b.ConnectionMode
from Substaion a,EquipmentConnectionMode b
where a.Id=B.MRID
根据字段名查询表
select a.name tableName,b.name columnName from sysobjects a,syscolumns b where a.id=b.id and b.name='columnName' and a.type='U'
是否数字
select isnumeric(RegUnitName) from Table where Column=17800
查询纯字母的行
select * from RegUnits where patindex('%[^A-z]%',RegUnitName)=0
创建唯一性约束:
CREATE UNIQUE INDEX Province_ToDay ON BrowseCount (ProvinceNo, ToDay) --CLUSTERED
查询区分大小写
select * from Users where UserName collate Chinese_PRC_CS_AS= 'YuTian'
日期转换
select CONVERT(varchar(10),getdate(),120)
更改列名
EXEC sp_rename 'T_Partner.PrinterDetect', 'IsPrinterDetect', 'COLUMN'
判断表是否存在
if not exists(select name from sysobjects where name=''+@name+'' and type='u')
begin
-- 创建
end
或
if object_id('[Table1]') is null
create table [Table1] ([col1] int,[col2] int)
[b]给表添加说明
[/b]
[b] EXECUTE sp_addextendedproperty N'MS_Description', '抓取信息的贴吧', N'user', N'dbo', N'table', N'BaiDu_TieBa', NULL, NULL
[/b]
[b][b]给列添加说明
[/b][/b]
[b][b]exec sp_addextendedproperty N'MS_Description', N'贴吧名称', N'user', N'dbo', N'table', N'BaiDu_TieBa', N'column', N'TieBaName'[/b][/b]
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'其他接收人姓名' , @level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'T_DoaMaterial',
@level2type=N'COLUMN',@level2name=N'TransToName'
[b][b][b]修改说明[/b][/b][/b]
execute sp_updateextendedproperty N'MS_Description',N'这是修改后的测试列1',N'SCHEMA',N'dbo',N'table',N'test',N'column',N'col1'
[b][b][b]删除说明
[/b][/b][/b]
-EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','TagsCommon',null,null
--EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','ZipAreaCode',null,null
Sql Server中判断表或者数据库是否存在
1.数据库
if exists(select 1 from master..sysdatabases where name='example')
print 'DataBase existed'
else
print 'Database not existed'
2.表
IF Exists(Select 1 From sysObjects Where Name ='表名' And Type In ('S','U'))
Print 'Exists Table'
Else
Print 'Not Exists Table'
在Sql Server2005中可以简化上述语句
如:
use example
go
if object_id('a','U') is not null
drop table a
go
注:a 是一个表,U代表是数据表类型
类似于U的类型代码,如下所示
对象类型:
AF = 聚合函数 (CLR)
C = CHECK 约束
D = DEFAULT(约束或独立)
F = FOREIGN KEY 约束
PK = PRIMARY KEY 约束
P = SQL 存储过程
PC = 程序集 (CLR) 存储过程
FN = SQL 标量函数
FS = 程序集 (CLR) 标量函数
FT = 程序集 (CLR) 表值函数
R = 规则(旧式,独立)
RF = 复制筛选过程
SN = 同义词
SQ = 服务队列
TA = 程序集 (CLR) DML 触发器
TR = SQL DML 触发器
IF = SQL 内联表值函数
TF = SQL 表值函数
U = 表(用户定义类型)
UQ = UNIQUE 约束
V = 视图
X = 扩展存储过程
IT = 内部表
还原数据库
RESTORE DATABASE [LexmarkMpsDoa]
FROM DISK = N'F:\Project\LexmarkMpsDoa\DataBase\LexmarkMpsDoa_backup_201202180100.bak'
WITH REPLACE
--删除默认值约束
DECLARE @tablename VARCHAR(100), @columnname VARCHAR(100), @tab VARCHAR(100)
SET @tablename='CssConsultation'---表名
SET @columnname='SolutionClass'---列名
declare @defname varchar(100)
declare @cmd varchar(100)
select @defname = name
FROM sysobjects so
JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = @tablename
AND so.xtype = 'D'
AND sc.colid =
(SELECT colid FROM syscolumns
WHERE id = object_id(@tablename) AND
name = @columnname)
SELECT @defname
select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname
if @cmd is null
print 'No default constraint to drop'
ELSE
exec (@cmd)
相关文章推荐
- 数据库相关学习
- Atitit 基于meta的orm,提升加速数据库相关应用的开发
- MSTR中数据库及项目源、项目的相关概念
- 8.4数据库相关的ActiveX控件
- pl\sql 数据库表空间的创建及相关语句
- 最新数据库相关技术整理
- 11月29日--数据库内容以及代码相关问题
- 3.4 数据库相关基础
- pg数据库相关问题。
- 数据库相关
- Penguins DbTools数据库管理移植差分工具(EXCEL相关) ver 20151020
- 黑马程序员之SQL学习笔记: 数据库相关知识 小总结
- 数据库全文索引相关问题
- (3期)传递字符,查找数据库相关表及字段值
- Mysql 数据库 库相关操作
- 数据库相关知识整理
- 数据库连接和相关操作
- discuz数据库迁移,改密码后,相关配置文件修改
- Java面试题 - 数据库相关
- Sql tempdb 临时数据库相关知识点