Home Database Mysql Tutorial 经典SQL语句

经典SQL语句

Jun 07, 2016 pm 05:39 PM
classic statement

一.数据库管理SQL语句 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 5、说明:删除新表: drop table tabname

一.数据库管理SQL语句

1、说明:创建数据库
CREATE DATABASE database-name
2、说明:删除数据库
drop database dbname
3、说明:创建视图:create view viewname as select statement
  删除视图:drop view viewname
5、说明:删除新表: drop table tabname
6、修改数据库的名称:sp_renamedb 'old_name', 'new_name'
7、说明:复制表(只复制结构,源表名:a 新表名:b)
  法一:select * into b from a where 11(仅用于SQlServer)
  法二:select top 0 * into b from a
8、说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
 insert into b(a, b, c) select d,e,f from a
9、说明:跨数据库之间表的拷贝(具体数据使用绝对路径)
 insert into b(a, b, c) select d,e,f from a in ‘具体数据库’ where 条件
 例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..

10、说明:列出数据库里所有的表名
 select name from sysobjects where type='U' // U代表用户
11、说明:列出表里的所有的列名
 select name from syscolumns where id=object_id('TableName')
12、说明:初始化表table1
 TRUNCATE TABLE table1

13、获取当前数据库中的所有用户表
select Name from sysobjects where xtype='u' and status>=0
14、查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'

15、压缩数据库
dbcc shrinkdatabase(dbname)

16:查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

二. 远程数据库操作

不同服务器数据库之间的数据操作
--创建链接服务器
exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin  'ITSV ', 'false ',null, '用户名 ', '密码 '
--查询示例
select * from ITSV.数据库名.dbo.表名
--导入示例
select * into 表 from ITSV.数据库名.dbo.表名
--以后不再使用时删除链接服务器
exec sp_dropserver  'ITSV ', 'droplogins '
 
--连接远程/局域网数据(openrowset/openquery/opendatasource)
--1、openrowset
--查询示例
select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
--生成本地表
select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
 
--把本地表导入远程表
insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
select *from 本地表
--更新本地表
update b
set b.列A=a.列A
 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b
on a.column1=b.column1
--openquery用法需要创建一个连接
--首先创建一个连接创建链接服务器
exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
--查询
select *
FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')
--把本地表导入远程表
insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')
select * from 本地表
--更新本地表
update b
set b.列B=a.列B
FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a 
inner join 本地表 b on a.列A=b.列A
 
--3、opendatasource/openrowset
SELECT   *
FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta
--把本地表导入远程表
insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名
select * from 本地表

三. 查询语句

EXCEPT 运算符中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。

但不包含后面的空格

为起始位置,虚拟主机,

right(char_expr,int_expr) 返回字符串右边第int_expr个字符,服务器空间,还用left于之相反

isnull( check_expression , replacement_value )如果check_expression為空,則返回replacement_value的值,不為空,美国空间,就返回check_expression字符操作类

1、说明:包括所有在     (select a from tableA ) except (select a from tableB) except (select a from tableC)

2.按姓氏笔画排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多

3、 date      num
2005-01-01      1
2005-01-01      3
2005-01-02      5
要求用一个SQL查出如下结果
2005-01-01      4
2005-01-02      5
合计            9

select date, sum(num) as num from table group by date
union
selec '合计'as date, sum(num) as num from table

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

How to implement the statement to create a table in MySQL? How to implement the statement to create a table in MySQL? Nov 08, 2023 pm 08:21 PM

How to implement the statement to create a table in MySQL? In the MySQL database, creating a table is one of the most important operations. The statement to create a table needs to take into account various factors such as the table structure, field types, constraints, etc. to ensure the accuracy and completeness of data storage. The following will introduce in detail how to create a table statement in MySQL, including specific code examples. First, we need to connect to the MySQL database server. You can use the following command to connect: mysql-uusername-p Next, enter the password

How to implement the statement to revoke user permissions in MySQL? How to implement the statement to revoke user permissions in MySQL? Nov 08, 2023 pm 01:04 PM

How to implement the statement to revoke user permissions in MySQL? In MySQL database, we often need to manage user permissions. However, sometimes we may need to revoke the permissions of certain users to ensure the security of the database. This article will introduce how to use specific code examples to implement the method of revoking user permissions in MySQL. First, we need to log in to the MySQL database server and switch to a user with sufficient permissions, such as root. We can then use the REVOKE statement to reclaim the user

The purpose of SQL ALTER statement The purpose of SQL ALTER statement Feb 19, 2024 pm 05:01 PM

The function of the SQL ALTER statement requires specific code examples. In a database management system, the ALTER statement is a SQL command used to modify database objects. Through the ALTER statement, we can modify database objects such as tables, columns, indexes, and views, including adding, deleting, modifying, and other operations. The following will introduce the common usage of the ALTER statement in detail and provide relevant code examples. ALTERTABLE statement is used to modify the structure of the table. You can add, delete, modify columns, constraints, indexes, etc.

How to implement the statement to change user password in MySQL? How to implement the statement to change user password in MySQL? Nov 08, 2023 am 09:05 AM

MySQL is a commonly used relational database system used to manage and store data. In MySQL, user passwords are one of the important factors in protecting database security. In daily management of the database, it is often necessary to change the user's password to ensure the security of the database. So, how to implement the statement of changing user password in MySQL? This article will provide you with specific code examples. Change the MySQL user password through the ALTERUSER statement. The ALTERUSER statement is MySQL8.0 and above.

To understand flow control statements in Python, you need to master several situations To understand flow control statements in Python, you need to master several situations Jan 20, 2024 am 08:06 AM

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

See all articles