MySQL--數(shù)據(jù)開(kāi)發(fā)經(jīng)典與處理方案
發(fā)表時(shí)間:2023-07-16 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]數(shù)據(jù)開(kāi)發(fā)-經(jīng)典1.按姓氏筆畫(huà)排序:Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //從...
數(shù)據(jù)開(kāi)發(fā)-經(jīng)典
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //從少到多
select encrypt('原始密碼')select pwdencrypt('原始密碼')select pwdcompare('原始密碼','加密后密碼') = 1--相同;否則不相同 encrypt('原始密碼')select pwdencrypt('原始密碼')select pwdcompare('原始密碼','加密后密碼') = 1--相同;否則不相同
declare @list varchar(1000),@sql nvarchar(1000)
select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'set @sql='select '+right(@list,len(@list)-1)+' from 表A' exec (@sql)
EXEC master..xp_fixeddrives
if (select checksum_agg(binary_checksum(*)) from A)
=
(select checksum_agg(binary_checksum(*)) from B)
print '相等'elseprint '不相等'
DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocessesWHERE program_name IN('SQL profiler',N'SQL 事件探查器')
EXEC sp_msforeach_worker '?'
開(kāi)頭到N條記錄Select Top N * From 表
-------------------------------N到M條記錄(要有主索引ID)Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc
----------------------------------N到結(jié)尾記錄
Select Top N * From 表 Order by ID Desc
案例 例如1:一張表有一萬(wàn)多條記錄,表的第一個(gè)字段 RecID 是自增長(zhǎng)字段, 寫(xiě)一個(gè)SQL語(yǔ)句, 找出表的第31到第40個(gè)記錄。
select top 10 recid from A where recid not in(select top 30 recid
from A) 分析:如果這樣寫(xiě)會(huì)產(chǎn)生某些問(wèn)題,如果recid在表中存在邏輯索引。
select top 10 recid from A where……
是從索引中查找,而后面的select top 30 recid from A則在數(shù)據(jù)表中查找,這樣由于索引中的順序有可能和數(shù)據(jù)表中的不一致,這樣就導(dǎo)致查詢(xún)到的不是本來(lái)的欲得到的數(shù)據(jù)。
解決方案
1,用order by select top 30 recid from A order by ricid 如果該字段不是自增長(zhǎng),就會(huì)出現(xiàn)問(wèn)題2,在那個(gè)子查詢(xún)中也加條件:select top 30 recid from A where recid>-1例2:查詢(xún)表中的最后以條記錄,并不知道這個(gè)表共有多少數(shù)據(jù),以及表結(jié)構(gòu)。set @s = 'select top 1 * from T where pid not in (select top ' + str(@count-1) + ' pid from T)'print @s exec sp_executesql @s
select Name from sysobjects where xtype='u' and status>=0
select name from syscolumns where id=object_id('表名')select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')
兩種方式的效果相同
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
select name as 存儲(chǔ)過(guò)程名稱(chēng) from sysobjects where xtype='P'
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
或者select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
select column_name,data_type from information_schema.columnswhere table_name = '表名'
--創(chuàng)建鏈接服務(wù)器exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '遠(yuǎn)程服務(wù)器名或ip地址 'exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用戶(hù)名 ', '密碼 '
--查詢(xún)示例select * from ITSV.數(shù)據(jù)庫(kù)名.dbo.表名
--導(dǎo)入示例select * into 表 from ITSV.數(shù)據(jù)庫(kù)名.dbo.表名
--以后不再使用時(shí)刪除鏈接服務(wù)器exec sp_dropserver 'ITSV ', 'droplogins '
--1、openrowset--查詢(xún)示例select * from openrowset( 'SQLOLEDB ', 'sql服務(wù)器名 '; '用戶(hù)名 '; '密碼 ',數(shù)據(jù)庫(kù)名.dbo.表名)--生成本地表select * into 表 from openrowset( 'SQLOLEDB ', 'sql服務(wù)器名 '; '用戶(hù)名 '; '密碼 ',數(shù)據(jù)庫(kù)名.dbo.表名)
insert openrowset( 'SQLOLEDB ', 'sql服務(wù)器名 '; '用戶(hù)名 '; '密碼 ',數(shù)據(jù)庫(kù)名.dbo.表名)select *from 本地表
update bset b.列A=a.列A from openrowset( 'SQLOLEDB ', 'sql服務(wù)器名 '; '用戶(hù)名 '; '密碼 ',數(shù)據(jù)庫(kù)名.dbo.表名)as a inner join 本地表 bon a.column1=b.column1
--首先創(chuàng)建一個(gè)連接創(chuàng)建鏈接服務(wù)器exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '遠(yuǎn)程服務(wù)器名或ip地址 '
--查詢(xún)select *FROM openquery(ITSV, 'SELECT * FROM 數(shù)據(jù)庫(kù).dbo.表名 ')
--把本地表導(dǎo)入遠(yuǎn)程表insert openquery(ITSV, 'SELECT * FROM 數(shù)據(jù)庫(kù).dbo.表名 ')select * from 本地表
--更新本地表update bset b.列B=a.列BFROM openquery(ITSV, 'SELECT * FROM 數(shù)據(jù)庫(kù).dbo.表名 ') as a
inner join 本地表 b on a.列A=b.列A
SELECT *FROM opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陸名;Password=密碼 ' ).test.dbo.roy_ta
--把本地表導(dǎo)入遠(yuǎn)程表insert opendatasource( 'SQLOLEDB ', 'Data Source=ip/ServerName;User ID=登陸名;Password=密碼 ').數(shù)據(jù)庫(kù).dbo.表名select * from 本地表
SQL Server基本函數(shù)
1.字符串函數(shù) 長(zhǎng)度與分析用
1,datalength(Char_expr) 返回字符串包含字符數(shù),但不包含后面的空格
2,substring(expression,start,length) 取子串,字符串的下標(biāo)是從“1”,start為起始位置,length為字符串長(zhǎng)度,實(shí)際應(yīng)用中以len(expression)取得其長(zhǎng)度
3,right(char_expr,int_expr) 返回字符串右邊第int_expr個(gè)字符,還用left于之相反
4,isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,就返回check_expression字符操作類(lèi)
5,Sp_addtype自定義數(shù)據(jù)類(lèi)型
例如:EXEC sp_addtype birthday, datetime, 'NULL'
6,set nocount {on off}
使返回的結(jié)果中不包含有關(guān)受 Transact-SQL 語(yǔ)句影響的行數(shù)的信息。如果存儲(chǔ)過(guò)程中包含的一些語(yǔ)句并不返回許多實(shí)際的數(shù)據(jù),則該設(shè)置由于大量減少了網(wǎng)絡(luò)流量,因此可顯著提高性能。SET NOCOUNT 設(shè)置是在執(zhí)行或運(yùn)行時(shí)設(shè)置,而不是在分析時(shí)設(shè)置。SET NOCOUNT 為 ON 時(shí),不返回計(jì)數(shù)(表示受 Transact-SQL 語(yǔ)句影響的行數(shù))。
SET NOCOUNT
為 OFF 時(shí),返回計(jì)數(shù)
常識(shí)
在SQL查詢(xún)中:from后最多可以跟多少?gòu)埍砘蛞晥D:256在SQL語(yǔ)句中出現(xiàn) Order by,查詢(xún)時(shí),先排序,后取在SQL中,一個(gè)字段的最大容量是8000,而對(duì)于nvarchar(4000),由于nvarchar是Unicode碼。
相關(guān)推薦:
MYSQL經(jīng)典語(yǔ)句大全——開(kāi)發(fā)篇
MySQL數(shù)據(jù)庫(kù)手動(dòng)安裝方法與中文解決方案
以上就是MySQL--數(shù)據(jù)開(kāi)發(fā)經(jīng)典與解決方案的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門(mén)到精通的SQL知識(shí)。