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

MySQL--data development classics and solutions

Aug 08, 2018 pm 12:02 PM
mysql

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

See all articles