TiDB database from entry to proficiency series 4: SQL basic operations

After successfully deploying the TiDB cluster, you can execute SQL statements in TiDB. Because TiDB is compatible with MySQL, you can use the MySQL client to connect to TiDB, and in most cases you can directly execute MySQL statements.

SQL is a declarative language, which is how database users interact with databases. It's more like a natural language, as if talking to the database in English.

1. SQL Language Classification

The SQL language is usually divided into the following 4 parts according to the function:

  • DDL (Data Definition Language): A data definition language used to define database objects, including libraries, tables, views, and indexes.
  • DML (Data Manipulation Language): Data manipulation language, used to manipulate records related to business.
  • DQL (Data Query Language): Data query language, used to query records filtered by conditions.
  • DCL (Data Control Language): Data Control Language, used to define access rights and security levels.

Commonly used DDL functions are object (such as table, index, etc.) creation, attribute modification and deletion, and the corresponding commands are CREATE, ALTER and DROP respectively.

2. View, create and delete databases

The Database or database in the context of TiDB can be considered as a collection of objects such as tables and indexes.

Use the SHOW DATABASES statement to view a list of databases in the system:

SHOW DATABASES;

Use a database called mysql:

USE mysql;

Use the SHOW TABLES statement to view all tables in the database. For example:

SHOW TABLES FROM mysql;

Create a database using the CREATE DATABASE statement. The syntax is as follows:

CREATE DATABASE db_name [options];

For example, to create a database named samp_db, use the following statement:

CREATE DATABASE IF NOT EXISTS samp_db;

Adding IF NOT EXISTS prevents the error from occurring.

Use the DROP DATABASE statement to drop a database. For example:

DROP DATABASE samp_db;

3. Create, view and delete tables

Tables are created using the CREATE TABLE statement. The syntax is as follows:

CREATE TABLE table_name column_name data_type constraint;

For example, to create a table named person with fields for number, name, birthday, etc., use the following statement:

CREATE TABLE person (
    id INT(11),
    name VARCHAR(255),
    birthday DATE
    );

Use the SHOW CREATE statement to view the table creation statement, that is, DDL. For example:

SHOW CREATE TABLE person;

Use the DROP TABLE statement to drop a table. For example:

DROP TABLE person;

Fourth, create, view and delete indexes

Indexes are often used to speed up queries on indexed columns. For columns with non-unique values, an ordinary index can be created using the CREATE INDEX or ALTER TABLE statement. For example:

CREATE INDEX person_id ON person (id);

or:

ALTER TABLE person ADD INDEX person_id (id);

For columns with unique values, unique indexes can be created. For example:

CREATE UNIQUE INDEX person_unique_id ON person (id);

or:

ALTER TABLE person ADD UNIQUE person_unique_id (id);

Use the SHOW INDEX statement to view all indexes in the table:

SHOW INDEX FROM person;

Use the ALTER TABLE or DROP INDEX statement to drop an index. Like the CREATE INDEX statement, DROP INDEX can also be embedded within an ALTER TABLE statement. For example:

DROP INDEX person_id ON person;
ALTER TABLE person DROP INDEX person_unique_id;

Note: DDL operations are not transactions, and there is no need to correspond to the COMMIT statement when executing DDL.

Commonly used DML functions are to add, modify and delete table records, and the corresponding commands are INSERT, UPDATE and DELETE respectively.

5. Addition, deletion and modification of records

Use the INSERT statement to insert table records into the table. For example:

INSERT INTO person VALUES(1,'tom','20170912');

Use the INSERT statement to insert table records containing partial field data into the table. For example:

INSERT INTO person(id,name) VALUES('2','bob');

Use the UPDATE statement to modify some field data of the table record in the table. For example:

UPDATE person SET birthday='20180808' WHERE id=2;

Use the DELETE statement to delete some table records from the table. For example:

DELETE FROM person WHERE id=2;

Note: UPDATE and DELETE operations operate on the entire table if there is no WHERE filter condition.

DQL data query language is to retrieve the desired data rows from one or more tables, which is usually the core content of business development.

6. Query data

Use the SELECT statement to retrieve data in the table. For example:

SELECT * FROM person;

Add the name of the column to be queried after the SELECT. For example:

SELECT name FROM person;
+------+
| name |
+------+
| tom  |
+------+
1 rows in set (0.00 sec)

Use the WHERE clause to filter whether all records meet the conditions before returning. For example:

SELECT * FROM person WHERE id<5;

Commonly used DCL functions are to create or delete users, and to manage user permissions.

7. Create, authorize and delete users

Use the CREATE USER statement to create a user tiuser with a password of 123456:

CREATE USER 'tiuser'@'localhost' IDENTIFIED BY '123456';

Authorized user tiuser can retrieve tables in the database samp_db:

GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost';

Query the permissions of user tiuser:

SHOW GRANTS for tiuser@localhost;

Delete user tiuser:

DROP USER 'tiuser'@'localhost';

Guess you like

Origin blog.csdn.net/zhengzaifeidelushang/article/details/132308330