当前位置:网站首页>Basic SQL operations in tidb
Basic SQL operations in tidb
2022-06-27 06:35:00 【Tianxiang shop】
Successful deployment TiDB After cluster , You can do it in TiDB In the implementation of SQL Statement . because TiDB compatible MySQL, You can use MySQL Client connection TiDB, also Most of the time Can be executed directly MySQL sentence .
SQL Is a declarative language , It is a way for database users to interact with the database . It is more like a natural language , It seems that we are talking with the database in English . This document describes the basic SQL operation . complete SQL Statement list , See TiDB SQL Grammar explanation .
classification
SQL Languages are usually divided into the following according to their functions 4 Parts of :
DDL (Data Definition Language): Data definition language , Used to define database objects , Including the library 、 surface 、 Views and indexes, etc .
DML (Data Manipulation Language): Data operation language , Used to operate business-related records .
DQL (Data Query Language): Data query language , Used to query records filtered by criteria .
DCL (Data Control Language): Data control language , Used to define access rights and security levels .
frequently-used DDL Function is object ( As shown in the table 、 Index, etc. ) The creation of 、 Property modification and deletion , The corresponding commands are CREATE、ALTER and DROP.
see 、 Create and delete databases
TiDB In context Database Or database , It can be considered as a collection of objects such as tables and indexes .
Use SHOW DATABASES Statement to view the list of databases in the system :
SHOW DATABASES;
Use the name mysql The database of :
USE mysql;
Use SHOW TABLES Statement to view all tables in the database . for example :
SHOW TABLES FROM mysql;
Use CREATE DATABASE Statement create database . The grammar is as follows :
CREATE DATABASE db_name [options];
for example , To create a samp_db The database of , You can use the following statements :
CREATE DATABASE IF NOT EXISTS samp_db;
add to IF NOT EXISTS Prevents errors .
Use DROP DATABASE Statement to delete the database . for example :
DROP DATABASE samp_db;
establish 、 View and delete tables
Use CREATE TABLE Statement create table . The grammar is as follows :
CREATE TABLE table_name column_name data_type constraint;
for example , To create a person Table of , Including numbering 、 name 、 Birthday and other fields , You can use the following statements :
CREATE TABLE person ( id INT(11), name VARCHAR(255), birthday DATE );
Use SHOW CREATE Statement view table creation statement , namely DDL. for example :
SHOW CREATE TABLE person;
Use DROP TABLE Statement delete table . for example :
DROP TABLE person;
establish 、 View and delete indexes
Indexes are often used to speed up queries on indexed columns . For columns with non unique values , You can use CREATE INDEX or ALTER TABLE Statement to create a normal index . for example :
CREATE INDEX person_id ON person (id);
perhaps :
ALTER TABLE person ADD INDEX person_id (id);
For columns with unique values , You can create a unique index . for example :
CREATE UNIQUE INDEX person_unique_id ON person (id);
perhaps :
ALTER TABLE person ADD UNIQUE person_unique_id (id);
Use SHOW INDEX Statement to view all indexes in the table :
SHOW INDEX FROM person;
Use ALTER TABLE or DROP INDEX Statement to delete index . And CREATE INDEX Statements like ,DROP INDEX You can also embed ALTER TABLE sentence . for example :
DROP INDEX person_id ON person;
ALTER TABLE person DROP INDEX person_unique_id;
Be careful :DDL Operations are not transactions , In execution DDL when , There is no need to correspond to COMMIT sentence .
frequently-used DML The function is to add table records 、 Modification and deletion , The corresponding commands are INSERT、UPDATE and DELETE.
Addition, deletion and modification of records
Use INSERT Statement to insert a table record into a table . for example :
INSERT INTO person VALUES(1,'tom','20170912');
Use INSERT Statement to insert a table record containing some field data into the table . for example :
INSERT INTO person(id,name) VALUES('2','bob');
Use UPDATE Statement to modify some field data of a table record into a table . for example :
UPDATE person SET birthday='20180808' WHERE id=2;
Use DELETE Statement to delete part of a table record . for example :
DELETE FROM person WHERE id=2;
Be careful :UPDATE and DELETE Operation without WHERE The filter condition is to operate the whole table .
DQL Data query language is to retrieve the desired data row from one or more tables , It is usually the core content of business development .
Query data
Use SELECT Statement to retrieve data in a table . for example :
SELECT * FROM person;
stay SELECT Add the column name to be queried . for example :
SELECT name FROM person;
+------+ | name | +------+ | tom | +------+ 1 rows in set (0.00 sec)
Use WHERE Clause , Filter all records to see if they meet the criteria before returning . for example :
SELECT * FROM person WHERE id<5;
frequently-used DCL The function is to create or delete users , And the management of user rights .
establish 、 Authorize and delete users
Use CREATE USER Statement to create a user tiuser, The password for 123456:
CREATE USER 'tiuser'@'localhost' IDENTIFIED BY '123456';
Authorized user tiuser Searchable database samp_db Table in :
GRANT SELECT ON samp_db.* TO 'tiuser'@'localhost';
Query the user tiuser Authority :
SHOW GRANTS for [email protected];
Delete user tiuser:
DROP USER 'tiuser'@'localhost';
边栏推荐
- Multithreading basic Part3
- Ora-00909: invalid number of parameters, caused by concat
- Program ape learning Tiktok short video production
- 网关状态检测 echo request/reply
- LeetCode 0086. Separate linked list
- Instance tunnel use
- EasyExcel:读取Excel数据到List集合中
- Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
- 机 器 学 习
- 记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
猜你喜欢

JVM常用指令

Fast realization of Bluetooth communication between MCU and mobile phone

JS to implement bidirectional data binding

Multithreading basic part2

Program ape learning Tiktok short video production

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

高斯分布Gaussian distribution、线性回归、逻辑回归logistics regression

Openresty usage document

JVM的垃圾回收机制

C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
随机推荐
分数阶PID控制
Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
Block level elements & inline elements
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
Fractional Order PID control
HTAP 快速上手指南
[QT] use structure data to generate read / write configuration file code
1317. convert an integer to the sum of two zero free integers
TiDB 数据库快速上手指南
[getting started] regular expression Basics
matlab GUI界面仿真直流电机和交流电机转速仿真
LeetCode 0086.分隔链表
Create a basic WDM driver and use MFC to call the driver
Thinking technology: how to solve the dilemma in work and life?
Assembly language - Wang Shuang Chapter 11 flag register - Notes
快速实现蓝牙iBeacn功能详解
Redis 缓存穿透、缓存击穿、缓存雪崩
tracepoint
Sqlsever 字段相乘后保留2位小数
JVM tuning ideas