SQL Statement Basics

Source: Internet
Author: User
Tags create index

Follow the http://www.w3school.com.cn/sql/sql_syntax.asp tutorial to learn and make notes here.

Grammar:

SQL is not case sensitive!

If you are using MS Access and SQL Server 2000, you do not have to use semicolons after each SQL statement, although some database software requirements must use semicolons.

SQL can be divided into two parts: Data manipulation Language (DML) and data definition language (DDL).

SQL (Structured Query language) is the syntax for executing queries. However, the SQL language also contains syntax for updating, inserting, and deleting records.

The query and update Directives form the DML portion of SQL:

      • SELECT-Get data from a database table
      • Update-updating data in a database table
      • Delete-Deletes data from the database table
      • INSERT into-inserts data into a database table

The Data definition language (DDL) portion of SQL gives us the ability to create or delete tables. We can also define indexes (keys), specify links between tables, and impose constraints between tables.

The most important DDL statement in SQL:

      • Create database-Creating new databases
      • ALTER DATABASE-Modify databases
      • CREATE table-Creates a new table
      • ALTER TABLE-Change (change) database table
      • drop table-Delete tables
      • Create index-Creating indexes (search key)
      • Drop INDEX-Delete indexes

SELECT column name from table name
*From Persons
DISTINCTCompany from Orders      #关键词 DISTINCT is used to return only different values.
WHERE column operator values
WHERE City=‘Beijing‘     #例子中的条件值周围使用的是单引号。SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
operator Description
= Equals
<> Not equal to, in some versions of SQL, the operator <> can be written as! =.
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Between Within a range
Like Search for a pattern


Text value:

FirstName=‘Bush‘FirstName=Bush

Numerical:

Year>1965Year>‘1965‘

And and or can combine two or more conditions in a where sub-statement. If both the first condition and the second condition are true, the AND operator displays a record. If only one of the first and second conditions is true, the OR operator displays a record.

(ORFirstname= ' William '   AND  lastname= ' Carter '    #可以把 and and or together (using parentheses to form complex expressions)

The order BY statement is used to sort the result set based on the specified column. The order BY statement sorts records by default in ascending order. If you want to sort records in descending order, you can use the DESC keyword.

ORDER BYDESCASC   #以逆字母顺序显示公司名称,并以数字顺序显示顺序号

INSERT INTO statement inserts a new row into the table

INSERT into table name values (value 1, value 2,....)
INSERT into Persons VALUES (' Gates ', ' Bill ', ' xuanwumen ', ' Beijing ')

You can also specify the columns for which you want to insert data:

INSERT into table_name (column 1, column 2,...) Values (value 1, value 2,....)
INSERT into Persons (LastName, Address) VALUES (' Wilson ', ' champs-elysees ')

The Update statement is used to modify the data in the table.

UPDATE table name SET column name = new value WHERE Column name = value
UPDATE person SET address = ' Zhongshan. ', city = ' nanjing ' WHERE LastName = ' Wilson '     #修改地址 (address), and add the town name

Delete statement to remove rows from a table
DELETE From table name WHERE column name = value
DELETE from table_name      #在不删除表的情况下删除所有的行. This means that the structure, properties, and indexes of the table are complete

Or:

DELETE * FROM table_name      #在不删除表的情况下删除所有的行. This means that the structure, properties, and indexes of the table are complete


SQL Statement Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.