MySQL Database SQL syntax reference

Source: Internet
Author: User
Tags date format definition insert mysql one table mysql database oracle database
Mysql|sql Grammar | reference | data | database | Grammar reference one, data definition DDL (data definition Language)

Data-setting language refers to the language defined in the format and form of data, he is the first to be set up when each database is to be established, what is the form of data, the form of a field key, tables and tables of the relationship between the reference and so on, are in the beginning must be planned.

1, build the form:

CREATE TABLE table_name (
COLUMN1 datatype [NOT NULL] [NOT NULL primary key],
COLUMN2 datatype [NOT NULL],
...);

Description

DataType--is the format of the data, as detailed in the table.
Nut null--Can you allow the data to be empty (no data has been filled in yet).
Primary key--is the primary key for this table.

2. Change the form

ALTER TABLE TABLE_NAME
Add Column column_name datatype

Description: Adds a field (without deleting a field's syntax.)

ALTER TABLE TABLE_NAME
Add primary key (column_name)

Description: Change the definition of a table to set a field as the primary key.

ALTER TABLE TABLE_NAME
Drop primary KEY (column_name)

Description: Delete the definition of the primary key.

3, the establishment of the index

CREATE INDEX index_name on table_name (column_name)

Description: Index The field of a table to increase the speed of the query.

4, delete

Drop table_name
Drop Index_name

Ii. Data manipulation DML (data manipulation language)

After the data is defined, the next thing is the operation of the data. The operation of the data is nothing more than the addition of data (insert), query data (query), change data (update), delete data (delete) four modes, the following describes their syntax:

1. Additional Information:

INSERT INTO table_name (COLUMN1,COLUMN2,...)
VALUES (value1,value2, ...)

Description

1. If no column system is specified, the information will be filled in the order of the fields in the table.

2. The data form of the field and the information to be filled must match.

3.table_name can also be a landscape view_name.

INSERT INTO table_name (COLUMN1,COLUMN2,...)
Select Columnx,columny,... from another_table

Note: You can also go through a subquery (subquery) to fill in the other forms of information.

2, Inquiry information:

Basic Query

Select Column1,columns2,...
From table_name

Description: List all the specific field data of the TABLE_NAME

SELECT *
From table_name
where column1 = xxx
[and Column2 > YYY] [or Column3 <> zzz]

Description

1. ' * ' indicates that all of the fields are listed.

The 2.where is followed by a conditional type, which lists the eligible data.

Select Column1,column2
From table_name
ORDER BY Column2 [DESC]

Description: Order BY IS to specify a column to do the sort, [desc] refers to from large to small, if not specified, is small to large

Arranged

Group Query

Combined query means that the source of the query is not only a single form, but a combination of more than one table can get results.

SELECT *
From Table1,table2
where Table1.colum1=table2.column1

Description

1. Query the information in the two tables with the same column1 value.

2. Of course, two tables are compared to each other in a field with the same data form.

3. A complex query may be used in a number of forms.

Consolidated queries:

Select COUNT (*)
From table_name
WHERE column_name = xxx

Description

There are a few strokes of the information that meets the criteria.

Select SUM (column1)
From table_name

Description

1. Calculates the sum, the selected field must be a number shape that can be counted.

2. In addition to AVG () is the calculation of average, Max (), Min () to calculate the maximum minimum value of the integration of the query.

Select Column1,avg (Column2)
From table_name
GROUP BY Column1
Having avg (column2) > XXX

Description

1.group by: The Column1 for a set of column2 averages must be used with keywords such as AVG, SUM, and other consolidated queries.

2.having: Must be used with group by as a consolidation restriction.

Complex query

SELECT *
From table_name1
where exists (
SELECT *
From Table_name2
where conditions)

Description

The 1.where conditions can be another query.

2.exists here refers to the existence or not.

SELECT *
From table_name1
where Column1 in (
Select Column1
From Table_name2
where conditions)

Description

1. In the back face is a collection that indicates that the COLUMN1 exists within the set.

2. The data form of select must conform to Column1.

Other queries

SELECT *
From table_name1
where column1 like ' x percent '

Description: Like must correspond to the ' x ' of the back side to indicate a string with the beginning of X.

SELECT *
From table_name1
where column1 in (' xxx ', ' yyy ',..)

Description: In the back face is a collection, which means that the column1 exists within the set.

SELECT *
From table_name1
where column1 between XX and yy

Description: Between indicates that the value of Column1 is between XX and yy.

3. Change information:

UPDATE table_name
Set column1= ' xxx '
where Conditoins

Description

1. Change a field to set its value to ' xxx '.

2.conditions is the condition you want to meet, and if there is no where, the entire table's field will all be changed.

4, delete the information:

Delete from table_name
Where conditions

Note: Delete the eligible information.

Note: There are different expressions for different databases if there is a comparison of dates after the Where condition. Specifically as follows:

(1) In the case of an Access database: where mydate> #2000 -01-01#

(2) If the Oracle database is: where Mydate>cast (' 2000-01-01 ' as date) or: Where Mydate>to_date (' 2000-01-01 ', ' yyyy-mm-dd ')
Written in Delphi:

Thedate= ' 2000-01-01 ';
Query1.sql.add (' SELECT * FROM ABC where mydate>cast (' + ' ' ' +thedate+ ' ' + ' as Date ');

If the date-time type is compared:

where Mydatetime>to_date (' 2000-01-01 10:00:01 ', ' yyyy-mm-dd hh24:mi:ss ');



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.