Home Database Mysql Tutorial SQL Server中各种语句的完整语法

SQL Server中各种语句的完整语法

Jun 07, 2016 pm 04:21 PM
server whole statement grammar

下列语句部分是MS SQL语句,不可以在access中使用。 SQL分类: DDL数据定义语言(CREATE,ALTER,DROP,DECLARE) DML数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建

   下列语句部分是MS SQL语句,不可以在access中使用。

  SQL分类:

  DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)

  DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)

  DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

  首先,简要介绍基础语句:

  1、说明:创建数据库

  CREATE DATABASE database-name

  2、说明:删除数据库

  drop database dbname

  3、说明:备份sql server

  --- 创建 备份数据的 device

  USE master

  EXEC sp_addumpdevice 'disk', 'testBack', 'c:mssql7backupMyNwind_1.dat'

  --- 开始 备份

  BACKUP DATABASE pubs TO testBack

  4、说明:创建新表

  create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

  根据已有的表创建新表:

  A:create table tab_new like tab_old (使用旧表创建新表)

  B:create table tab_new as select col1,col2… from tab_old definition only

  5、说明:

  删除新表:drop table tabname

  6、说明:

  增加一个列:Alter table tabname add column col type

  注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

  7、说明:

  添加主键:Alter table tabname add primary key(col)

  说明:

  删除主键:Alter table tabname drop primary key(col)

  8、说明:

  创建索引:create [unique] index idxname on tabname(col….)

  删除索引:drop index idxname

  注:索引是不可更改的,想更改必须删除重新建。

  9、说明:

  创建视图:create view viewname as select statement

  删除视图:drop view viewname

  10、说明:几个简单的基本的sql语句

  选择:select * from table1 where 范围

  插入:insert into table1(field1,field2) values(value1,value2)

  删除:delete from table1 where 范围

  更新:update table1 set field1=value1 where 范围

  查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!

  排序:select * from table1 order by field1,field2 [desc]

  总数:select count * as totalcount from table1

  求和:select sum(field1) as sumvalue from table1

  平均:select avg(field1) as avgvalue from table1

  最大:select max(field1) as maxvalue from table1

  最小:select min(field1) as minvalue from table1

  11、说明:几个高级查询运算词

  A: UNION 运算符

  UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

  B: EXCEPT 运算符

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

  C: INTERSECT 运算符

  INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。

  注:使用运算词的几个查询结果行必须是一致的。

  12、说明:使用外连接

  A、left outer join:

  左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。

  SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

  B:right outer join:

  右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。

  C:full outer join:

  全外连接:不仅包括符号连接表的匹配行,,还包括两个连接表中的所有记录。

  其次,大家来看一些不错的sql语句

  1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)

  法一:select * into b from a where 11

  法二:select top 0 * into b from a

  2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)

  insert into b(a, b, c) select d,e,f from b;

  3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)

  insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件

  例子:..from b in '"&Server.MapPath(".")&"data.mdb" &"' where..

  4、说明:子查询(表名1:a 表名2:b)

  select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

  5、说明:显示文章、提交人和最后回复时间

  select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

  6、说明:外连接查询(表名1:a 表名2:b)

  select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

  7、说明:在线视图查询(表名1:a )

  select * from (SELECT a,b,c FROM a) T where t.a > 1;

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
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months 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)

How to quickly turn your Python code into an API How to quickly turn your Python code into an API Apr 14, 2023 pm 06:28 PM

When it comes to API development, you may think of DjangoRESTFramework, Flask, and FastAPI. Yes, they can be used to write APIs. However, the framework shared today allows you to convert existing functions into APIs faster. It is Sanic . Introduction to Sanic Sanic[1] is a Python3.7+ web server and web framework designed to improve performance. It allows the use of the async/await syntax added in Python 3.5, which can effectively avoid blocking and improve response speed. Sanic is committed to providing a simple and fast way to create and launch

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Feb 19, 2024 pm 02:36 PM

On the occasion of releasing the build 26040 version of Windows Server, Microsoft announced the official name of the product: Windows Server 2025. Also launched is the Windows11WindowsInsiderCanaryChannel version build26040. Some friends may still remember that many years ago someone successfully converted Windows NT from workstation mode to server mode, showing the commonalities between various versions of Microsoft operating systems. Although there are clear differences between Microsoft's current version of the server operating system and Windows 11, those who pay attention to the details may be curious: why Windows Server updated the brand,

New type alias syntax in PHP8.0 New type alias syntax in PHP8.0 May 14, 2023 pm 02:21 PM

With the release of PHP 8.0, a new type alias syntax has been added, making it easier to use custom types. In this article, we'll take a closer look at this new syntax and its impact on developers. What is a type alias? In PHP, a type alias is essentially a variable that references the name of another type. This variable can be used like any other type and declared anywhere in the code. The main function of this syntax is to define custom aliases for commonly used types, making the code easier to read and understand.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

The connection and difference between Go language and JS The connection and difference between Go language and JS Mar 29, 2024 am 11:15 AM

The connection and difference between Go language and JS Go language (also known as Golang) and JavaScript (JS) are currently popular programming languages. They are related in some aspects and have obvious differences in other aspects. This article will explore the connections and differences between the Go language and JavaScript, and provide specific code examples to help readers better understand these two programming languages. Connection: Both Go language and JavaScript are cross-platform and can run on different operating systems.

How to modify the Nginx version name to disguise any web server How to modify the Nginx version name to disguise any web server May 14, 2023 pm 09:19 PM

How to modify the default name of nginx, you can disguise it a little, or you can install Tip: Generally, modifications are made before nginx is compiled. After modification, the code needs to be recompiled as follows: scr/core/nginx.conf#definenginx_version"1.4.7"#definenginx_ver"nginx/"n

Microsoft releases Windows Server vNext preview version 25335 Microsoft releases Windows Server vNext preview version 25335 Jan 10, 2024 am 08:49 AM

While Microsoft released the Win11 preview update for the desktop, today it also released the Windows Server Long Term Service Channel (LTSC) preview Build 25335. As usual, Microsoft did not publish a complete change log, or even provide a corresponding blog post. Microsoft has adjusted the Windows Server preview version update log to make it the same as the Canary channel version. If no new content is introduced, the official blog post will not be posted. Note from IT Home: The server brand has not been updated and is still Windows Server 2022 in the preview version. In addition, Microsoft calls these versions Windows Server vNext instead of the Windows version that is already on the market.

See all articles