当前位置:网站首页>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';
边栏推荐
猜你喜欢

Caldera安装及简单使用

JVM对象组成和存储

The risk of multithreading -- thread safety

2018年数学建模竞赛-高温作业专用服装设计

机 器 学 习

JS to implement bidirectional data binding

matlab GUI界面仿真直流电机和交流电机转速仿真

426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)

Convolution neural network -- Application of CNN model (ore prospecting prediction)

古典密码体制--代换和置换
随机推荐
Us camera cloud service scheme: designed for lightweight video production scenes
Unrecognized VM option ‘‘
JVM common instructions
写一个 goroutine 实例, 同时练习一下 chan
The form verifies the variables bound to the V-model, and the solution to invalid verification
块级元素&行内元素
Redis 缓存穿透、缓存击穿、缓存雪崩
技术人员创业一年心得
Fast realization of Bluetooth communication between MCU and mobile phone
网关状态检测 echo request/reply
观测电机转速转矩
Proxy reflect usage details
Matlab quickly converts two-dimensional coordinates of images into longitude and latitude coordinates
浅谈GPU:历史发展,架构
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
JVM常用指令
Multithreading basic part part 1
Spark SQL common time functions
310. minimum height tree
Maxcompute SQL 的查询结果条数受限1W