Home > Database > Mysql Tutorial > MySQL--data development classics and solutions

MySQL--data development classics and solutions

php是最好的语言
Release: 2018-08-08 12:02:49
Original
1386 people have browsed it

Data Development-Classic

  • 1. Sort by surname strokes:

Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多
Copy after login
  • 2. Database encryption:

select encrypt('原始密码')select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同
Copy after login
  • 3. Retrieve the fields in the table:

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)
Copy after login
  • 4 .Check the hard disk partition:

EXEC master..xp_fixeddrives
Copy after login
  • 5. Compare tables A and B to see if they are equal:

if (select checksum_agg(binary_checksum(*)) from A)
     =
    (select checksum_agg(binary_checksum(*)) from B)
print '相等'elseprint '不相等'
Copy after login
  • 6. Kill all profiler processes:

DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocessesWHERE program_name IN('SQL profiler',N'SQL 事件探查器')
EXEC sp_msforeach_worker '?'
Copy after login
  • 7. Record search:

开头到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到结尾记录
Select Top N * From 表 Order by ID Desc
Copy after login

Case example 1: There are more than 10,000 records in a table. The first field of the table, RecID, is a self-increasing field. Write a SQL statement to find the 31st to 40th records of the table.
select top 10 recid from A where recid not in(select top 30 recid
from A) Analysis: If written like this, some problems will occur, if recid has a logical index in the table.
select top 10 recid from A where... is searched from the index, while the subsequent select top 30 recid from A is searched in the data table, so due to the order in the index, it may be inconsistent with the data Inconsistencies in the table will result in the query not obtaining the original data.

Solution

1,用order by select top 30 recid from A order by ricid 如果该字段不是自增长,就会出现问题2,在那个子查询中也加条件:select top 30 recid from A where recid>-1例2:查询表中的最后以条记录,并不知道这个表共有多少数据,以及表结构。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
Copy after login
  • 9: Get all user tables in the current database

select Name from sysobjects where xtype='u' and status>=0
Copy after login
  • 10: Get all fields of a table

select name from syscolumns where id=object_id('表名')select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')
两种方式的效果相同
Copy after login
  • 11: View views, stored procedures, and functions related to a table

select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
Copy after login
  • 12: View all stored procedures in the current database

select name as 存储过程名称 from sysobjects where xtype='P'
Copy after login
  • 13: Query all stored procedures created by the user Database

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
Copy after login
  • 14: Query the fields and data types of a certain table

select column_name,data_type from information_schema.columnswhere table_name = &#39;表名&#39;
Copy after login
  • 15: Data operations between different server databases

--创建链接服务器exec sp_addlinkedserver   &#39;ITSV &#39;, &#39; &#39;, &#39;SQLOLEDB &#39;, &#39;远程服务器名或ip地址 &#39;exec sp_addlinkedsrvlogin  &#39;ITSV &#39;, &#39;false &#39;,null, &#39;用户名 &#39;, &#39;密码 &#39;
Copy after login
--查询示例select * from ITSV.数据库名.dbo.表名
Copy after login
--导入示例select * into 表 from ITSV.数据库名.dbo.表名
--以后不再使用时删除链接服务器exec sp_dropserver  &#39;ITSV &#39;, &#39;droplogins &#39;
Copy after login
  • -Connect remote/LAN data (openrowset/openquery/opendatasource)

--1、openrowset--查询示例select * from openrowset( &#39;SQLOLEDB &#39;, &#39;sql服务器名 &#39;; &#39;用户名 &#39;; &#39;密码 &#39;,数据库名.dbo.表名)--生成本地表select * into 表 from openrowset( &#39;SQLOLEDB &#39;, &#39;sql服务器名 &#39;; &#39;用户名 &#39;; &#39;密码 &#39;,数据库名.dbo.表名)
Copy after login
  • – Import the local table into the remote table

insert openrowset( &#39;SQLOLEDB &#39;, &#39;sql服务器名 &#39;; &#39;用户名 &#39;; &#39;密码 &#39;,数据库名.dbo.表名)select *from 本地表
Copy after login
  • – Update the local table

update bset b.列A=a.列A from openrowset( &#39;SQLOLEDB &#39;, &#39;sql服务器名 &#39;; &#39;用户名 &#39;; &#39;密码 &#39;,数据库名.dbo.表名)as a inner join 本地表 bon a.column1=b.column1
Copy after login
  • –Openquery usage requires creating a connection

  • ##
    --首先创建一个连接创建链接服务器exec sp_addlinkedserver   &#39;ITSV &#39;, &#39; &#39;, &#39;SQLOLEDB &#39;, &#39;远程服务器名或ip地址 &#39;
    Copy after login
    --查询select *FROM openquery(ITSV,  &#39;SELECT *  FROM 数据库.dbo.表名 &#39;)
    Copy after login
    --把本地表导入远程表insert openquery(ITSV,  &#39;SELECT *  FROM 数据库.dbo.表名 &#39;)select * from 本地表
    Copy after login
    --更新本地表update bset b.列B=a.列BFROM openquery(ITSV,  &#39;SELECT * FROM 数据库.dbo.表名 &#39;) as a 
    inner join 本地表 b on a.列A=b.列A
    Copy after login
  • –3, opendatasource/openrowset

  • SELECT   *FROM   opendatasource( &#39;SQLOLEDB &#39;,  &#39;Data Source=ip/ServerName;User ID=登陆名;Password=密码 &#39; ).test.dbo.roy_ta
    --把本地表导入远程表insert opendatasource( &#39;SQLOLEDB &#39;,  &#39;Data Source=ip/ServerName;User ID=登陆名;Password=密码 &#39;).数据库.dbo.表名select * from 本地表
    Copy after login
    SQL Server基本函数
    1.字符串函数 长度与分析用
    1,datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格
    2,substring(expression,start,length) 取子串,字符串的下标是从“1”,start为起始位置,length为字符串长度,实际应用中以len(expression)取得其长度
    3,right(char_expr,int_expr) 返回字符串右边第int_expr个字符,还用left于之相反
    4,isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,就返回check_expression字符操作类
    5,Sp_addtype自定義數據類型
    例如:EXEC sp_addtype birthday, datetime, &#39;NULL&#39;
    6,set nocount {on|off}
    使返回的结果中不包含有关受 Transact-SQL 语句影响的行数的信息。如果存储过程中包含的一些语句并不返回许多实际的数据,则该设置由于大量减少了网络流量,因此可显著提高性能。SET NOCOUNT 设置是在执行或运行时设置,而不是在分析时设置。SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。
    Copy after login
        SET NOCOUNT 
        为 OFF 时,返回计数
        常识
    
        在SQL查询中:from后最多可以跟多少张表或视图:256在SQL语句中出现 Order by,查询时,先排序,后取在SQL中,一个字段的最大容量是8000,而对于nvarchar(4000),由于nvarchar是Unicode码。
    Copy after login
    Related recommendations:

    MYSQL Classic Statement Collection - Development Chapter

    ##MySQL database manual installation method and Chinese solution

    The above is the detailed content of MySQL--data development classics and solutions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template