MySQL basic SQL statement Encyclopedia (Basic Text)

Source: Internet
Author: User
Tags create index

 1. Description: Create Database

CREATE DATABASE Database-name

  2. Description: Delete Database

Drop Database dbname

 3. Description: Back up SQL Server

---to create a device that backs up data

Use master

EXEC sp_addumpdevice ' disk ', ' testback ', ' C:\mssql7backup\MyNwind_1.dat '

---start Backup

BACKUP DATABASE pubs to Testback

  4. Description: Create a new table

CREATE TABLE TabName (col1 type1 [NOT NULL] [primary key],col2 type2 [NOT NULL],..)

To create a new table from an existing table:

A:create table tab_new like Tab_old (create new table with old table)

B:create table tab_new as Select Col1,col2 ... from tab_old definition only

 5. Description: Delete new table

drop table TabName

 6. Description: Add a column

Alter table tabname Add column col type

Note: Columns cannot be deleted after they are added. DB2 the column plus the data type can not be changed, the only change is to increase the length of the varchar type.

  7. Description: Add primary key: Alter table TabName Add primary key (COL)

Description: Delete primary key: Alter table tabname drop primary key (COL)

 8. Description: Create INDEX: [unique] index idxname on tabname (col ...)

Drop INDEX: Idxname

Note: The index is immutable and you must remove the rebuild if you want to change it.

  9. Description: Creating view: Create VIEW viewname AS SELECT statement

Delete view: Drop View ViewName

  10, Description: A few simple basic SQL statements

Select: SELECT * FROM table1 where range

Insert: INSERT INTO table1 (field1,field2) VALUES (value1,value2)

Delete: Delete from table1 where range

Updated: Update table1 set field1=value1 where range

Find: SELECT * FROM table1 where field1 like '%value1% '---the syntax of like is very subtle, check the information!

Sort: SELECT * FROM table1 ORDER by FIELD1,FIELD2 [DESC]

Total: Select count as TotalCount from table1

Sum: Select SUM (field1) as Sumvalue from table1

Average: Select AVG (field1) as Avgvalue from table1

Maximum: Select Max (field1) as MaxValue from table1

Min: select min (field1) as MinValue from table1

  11. Description: Several advanced query operation words

A:union operator

The UNION operator derives a result table by combining the other two result tables (for example, TABLE1 and TABLE2) and eliminating any duplicate rows in the table. When all is used with the Union (that is, union ALL), duplicate rows are not eliminated. In both cases, each row of the derived table is either from TABLE1 or from TABLE2.

B:except operator

The EXCEPT operator derives a result table by including all rows in TABLE1 but not in TABLE2 and eliminating all duplicate rows. When all is used with EXCEPT (EXCEPT all), duplicate rows are not eliminated.

C:intersect operator

The INTERSECT operator derives a result table by including only rows in TABLE1 and TABLE2 and eliminating all duplicate rows. When all is used with INTERSECT (INTERSECT all), duplicate rows are not eliminated.

Note: Several query result rows that use an operation word must be consistent.

  12. Description: Use external connection

A, left (outer) join:

Left OUTER join (left JOIN): The result set includes a matching row for the join table and all rows of the left join table.

Sql:select a.a, A.B, A.C, B.C, B.D, B.f from a left off JOIN b on a.a = B.C

B:right (outer) Join:

Right outer join (right Join): The result set includes both the matching join row for the join table and all rows of the right join table.

C:full/cross (outer) Join:

Full outer joins: Includes not only the matching rows of the symbolic join table, but also all the records in the two join tables.

  13, group: GROUP By:

A table, once the grouping is complete, you can only get group-related information after the query.

Group-related information: (statistical information) Count,sum,max,min,avg grouping criteria)

When grouping in SQL Server: You cannot group by a field of type Text,ntext,image

The fields in the SELECTE statistic function cannot be combined with ordinary fields;

  14, the operation of the database:

Separate database: sp_detach_db; Additional database: sp_attach_db followed by indicates that the append requires a full path name

 15. How to modify the name of the database:

Sp_renamedb ' Old_name ', ' new_name '

MySQL basic SQL statement Encyclopedia (Basic Text)

Related Article

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.