Detailed commonly used SQL statements

A, DDL

The DEFINITION LANGUAGES the DATA (data definition language), these statements define different segments of data, databases, tables, columns, indexes and other database objects. Common statement keywords including CREATE, DROP, ALTER , etc.

1. Database definitions

1.1 View database
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
1.2 Creating a database
# 创建名为learn_mysql的数据库
mysql> CREATE DATABASE learn_mysql;
Query OK, 1 row affected (0.00 sec)
1.3 Delete Database
# 删除数据库
mysql> DROP DATABASE learn_mysql;
Query OK, 1 row affected (0.27 sec)
1.4 Database
# 使用learn_mysql数据库
mysql> USE learn_mysql;
Database changed

2. Data table definition

2.1 Lists the current database of all data tables
mysql> SHOW TABLES;
+-----------------------+
| Tables_in_learn_mysql |
+-----------------------+
| test1                 |
+-----------------------+
1 row in set (0.39 sec)
2.2 to create a data table
mysql>  CREATE TABLE `test1` (
	# 字段名 数据类型 约束条件
  	-> `id` int(11) NOT NULL, 
  	-> `name` char(10) NOT NULL,
  	-> `age` int(11) DEFAULT NULL
  	# ENGINE(存储引擎) CHARSET(字符集)
	-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8	
2.3 see table below for details (field information)
mysql> DESC test1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   |     | NULL    |       |
| name  | char(10) | NO   |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
2.4 View Table Definition
mysql> SHOW CREATE TABLE test1;
+-------+------------------------------------------------------------------+
| Table | Create Table                                                     |
+-------+------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
            `id` int(11) NOT NULL,
            `name` char(10) NOT NULL,
            `age` int(11) DEFAULT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8 							|
+-------+------------------------------------------------------------------+
1 row in set (0.00 sec)
2.5 Delete table

DROP TABLE table_name

2.6 modify the table
  1. Modify the field type

    ALTER TABLE table_name MODIFY [COLUMN] col_def [FIRST | AFTER col_name]

  2. Add field

    ALTER TABLE table_name ADD [COLUMN] col_def [FIRST | AFTER col_name]

  3. Delete field

    ALTER TABLE table_name DROP [COLUMN] col_name

  4. Rename the field

    ALTER TABLE table_name CHANGE old_col_name col_def

  5. Modify the table name

    ALTER TABLE table_name RENAME [TO] new_name

Two, DML

Manipulation Language the Data (data manipulation statements), to add, delete, update and query the database record and checks data integrity. Common statements include keywords INSERT, DELETE, UPDATE, SELECT and so on.

1. Insert data (the INSERT)

1.1 Insertion into record

INSERT INTO table_name(col_name,…) VALUES(col_value,…);

1.2 insert multiple records

INSERT INTO table_name(col_name,…) VALUES(col_value,…),(col_value,…),…;

2. modify the data (the UPDATE)

The data in Table 2.1 amendments

UPDATE table_name SET field1=val1,field2=val2,… [WHERE CONDITION];

More than 2.2 modify table data simultaneously

UPDATE t1,t2,…,tn SET t1.field1=val1 ,t2.filed1=val2,…[WHERE CONDITION];

3. Delete data (DELETE)

3.1 Delete single data table

DELETE FROM TABLE [WHERE CONDITION];

Over 3.2 meter data deleted

DELETE FROM t1,t2,…,tn [WHERE CONDITION];

4. Query

4.1 simple query
  1. Query all records

    SELECT * FROM table_name

  2. Record deduplication

    SELECT DISTINCT col_name (can *) FROM table_name

    # 查询所有记录
    mysql> SELECT * FROM test1;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  1 | ccy1 |   12 |
    |  2 | ccy2 |   13 |
    |  3 | ccy  |   16 |
    |  4 | ccy  |   16 |
    +----+------+------+
    4 rows in set (0.00 sec)
    # 查询所有记录并去除完全重复记录
    mysql> SELECT DISTINCT * FROM test1;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  1 | ccy1 |   12 |
    |  2 | ccy2 |   13 |
    |  3 | ccy  |   16 |
    |  4 | ccy  |   16 |
    +----+------+------+
    4 rows in set (0.00 sec)
    # 查询name字段数据并去重
    mysql> SELECT DISTINCT name FROM test1;
    +------+
    | name |
    +------+
    | ccy1 |
    | ccy2 |
    | ccy  |
    +------+
    3 rows in set (0.00 sec)
    
  3. Conditions inquiry

    SELECT * FROM table_name [WHERE CONDITION]

    Conditional operator Explanation
    = equal
    > more than the
    >= greater or equal to
    < Less than
    <= Less than or equal
    != (<>) not equal to
    mysql> SELECT * FROM test1 WHERE name='ccy';
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  3 | ccy  |   16 |
    |  4 | ccy  |   16 |
    +----+------+------+
    2 rows in set (0.00 sec)
    
  4. Sequence

    ORDER BY keywords achieved, and ASC DESC keyword is sorted, in descending order according represents DESC field, said ASC ascending order, if the default is not written this keyword in ascending order. ORDER BY can later with a plurality of different sort fields, and each field may have a different sort order sort, according to the sort order of the fields

    # 语法格式
    # SELECT * FROM table_name [WHERE CONFITION] [ORDER BY] col_name
    # [DESC|ASC],col_name2 [DESC|ASC],...,col_namen [DESC|ASC]
    mysql> SELECT * FROM test1 ORDER BY id DESC;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  4 | ccy  |   16 |
    |  3 | ccy  |   16 |
    |  2 | ccy2 |   13 |
    |  1 | ccy1 |   12 |
    +----+------+------+
    4 rows in set (0.00 sec)
    
  5. Limit the number of records

    SELECT * FROM table_name [LIMIT start, end]

    # 默认情况下,其实偏移量为0,数字为记录条数
    mysql> SELECT * FROM test1 LIMIT 2;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  1 | ccy1 |   12 |
    |  2 | ccy2 |   13 |
    +----+------+------+
    2 rows in set (0.00 sec)
    # 从第一条到第三条(从0开始计算)
    mysql> SELECT * FROM test1 LIMIT 1,3;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  2 | ccy2 |   13 |
    |  3 | ccy  |   16 |
    |  4 | ccy  |   16 |
    +----+------+------+
    3 rows in set (0.00 sec)
    
  6. Aggregate function

    # 语法
    # SELECT [col_1,col_2,...,col_n] fun_name FROM table_name
    # [WHERE CONDITION] [GROUP BY col_1,...,col_n [WITH ROLLUP]]
    # [HAVING where_condition]
    mysql> SELECT COUNT(*) FROM test1;
    +----------+
    | COUNT(*) |
    +----------+
    |        4 |
    +----------+
    1 row in set (0.03 sec)
    
More than 4.2-table query
  1. Table join

    Table connector into the connector and the outer connector , the main difference between them is, only the selected connection table record two match each other, the outer connecting other records will be selected that do not match. The common connection

  2. En

    SELECT * FROM t1, t2 WHERE t1.c1=t2.c1

    mysql> SELECT * FROM test1,test2 WHERE test1.id=test2.id;
    +----+------+------+----+-------+
    | id | name | age  | id | name  |
    +----+------+------+----+-------+
    |  1 | ccy1 |   12 |  1 | ccy   |
    |  2 | ccy2 |   13 |  2 |   ccy |
    +----+------+------+----+-------+
    2 rows in set (0.28 sec)
    
  3. Outer join

    • Left connection

      Record contains all that's left of the table and even the right of the table and it does not match the record

      mysql> SELECT * FROM test1 left join test2 on test1.id=test2.id;
      +----+------+------+------+-------+
      | id | name | age  | id   | name  |
      +----+------+------+------+-------+
      |  1 | ccy1 |   12 |    1 | ccy   |
      |  2 | ccy2 |   13 |    2 |   ccy |
      |  3 | ccy  |   16 | NULL | NULL  |
      |  4 | ccy  |   16 | NULL | NULL  |
      +----+------+------+------+-------+
      4 rows in set (0.00 sec)
      
    • The right connection

      Record contains all of the right side of the table and even left the table and it does not match the record

      mysql> SELECT * FROM test1 right join test2 on test1.id=test2.id;
      +------+------+------+----+-------+
      | id   | name | age  | id | name  |
      +------+------+------+----+-------+
      |    1 | ccy1 |   12 |  1 | ccy   |
      |    2 | ccy2 |   13 |  2 |   ccy |
      +------+------+------+----+-------+
      2 rows in set (0.00 sec)
      
4.3 subqueries

When queried, the conditions needed was another select statement results, this time, it is necessary to use sub-queries. Keywords for sub-queries include in, not in, =,! =, Exists, not exists and so on.

# 查询test1中与test2的id相同的记录 
mysql> SELECT * FROM test1 WHERE id in (SELECT id FROM test2);
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | ccy1 |   12 |
|  2 | ccy2 |   13 |
+----+------+------+
2 rows in set (0.03 sec)
4.4 Record union

After two tables of data query out according to certain search criteria, the results will merge together to show up this time, we need to use union and union all keywords to achieve this function

  1. UNION ALL

    mysql> SELECT name FROM test1 UNION ALL SELECT name FROM test2;
    +-------+
    | name  |
    +-------+
    | ccy1  |
    | ccy2  |
    | ccy   |
    | ccy   |
    | ccy   |
    |   ccy |
    +-------+
    6 rows in set (0.02 sec)
    
  2. UNION

    UNION ALL result after re-set to the data set

    mysql> SELECT name FROM test1 UNION SELECT name FROM test2;
    +-------+
    | name  |
    +-------+
    | ccy1  |
    | ccy2  |
    | ccy   |
    |   ccy |
    +-------+
    4 rows in set (0.05 sec)
    

Three, DCL

Language Control the Data (data control statements) for direct access and permission levels of different data segment control statements. These statements define the databases, tables, fields, user access rights and security levels . The main statement keywords include DRANT, REVOKE and so on.

1. Create a database users and assign permissions

# GRANT [SELECT|INSERT] ON database_name.table_name to user_name IDENTIFIED
# BY password
# 创建用户并赋予权限
mysql> GRANT SELECT ON learn_mysql.* TO 'ccy'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected, 1 warning (0.06 sec)
# 以该用户登陆后数据库状态
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| learn_mysql        |
+--------------------+
2 rows in set (0.00 sec)
# 插入数据是提示无权限
mysql> insert into test1 values(10,'cc',10);
ERROR 1142 (42000): INSERT command denied to user 'ccy'@'localhost' for table 'test1'
# ROOT用户下修改权限
mysql> GRANT INSERT,SELECT ON learn_mysql.* TO 'ccy'@'localhost';
Query OK, 0 rows affected (0.00 sec)
# 插入成功
mysql> insert into test1 values(10,'cc',10);
Query OK, 1 row affected (0.12 sec)
2. Recovery of rights
# REVOKE [INSERT|SELECT] ON database_name.table_name FROM user_name;
# ROOT用户权限下
mysql> REVOKE INSERT ON learn_mysql.* FROM 'ccy'@'localhost';
Query OK, 0 rows affected (0.00 sec)
Published 17 original articles · won praise 1 · views 650

Guess you like

Origin blog.csdn.net/c_c_y_CC/article/details/102976983