当前位置:网站首页>MySQL (II) - basic operation
MySQL (II) - basic operation
2022-06-28 06:10:00 【Mu Di%】
MySQL operation
- Database operation
- Database table operation
- - All the tables in the database
- - Create data table
- - Query table structure
- - Query the table creation statement of the specified table
- - Modify the name of the table
- - Delete form
- - Delete the table and recreate ( format )
- - Add table fields
- - Modify field type and name
- - Delete table fields
- - Query data from table
- - Insert data in the form
- where Clause
- AND and OR Operator
- LIKE Clause
- IN The operator
- BETWEEN The operator
- UNION The operator
- Sort
- grouping
- Update data
- Delete data
- JOIN Clause
- SELECT DISTINCT sentence
Database operation
- Create database
CREATE DATABASE
# impact Name the database
# if not exists Optional , prevent mysql Report errors
CREATE DATABASE (if not exists) impact;
- List MySQL Database list
SHOW DATABASES
show databases;
- Using a database
USE Database name
use impact;
Database changed
- Determine the current database
SELECT DATABASE()
SELECT DATABASE();
- Delete database
DROP DATABASE Database name
# runoob Name the database ,IF EXISTS Optional , prevent mysql Report errors
DROP DATABASE (IF EXISTS) runoob;
Database table operation
- All the tables in the database
SHOW TABLES
show tables;
- Create data table
CREATE TABLE Table name ()
# The first column is the table field name , The second column is the data type
# Table field ',' separate
# comment ' explain ' Optional , Properties of fields Optional
create table charact_tbl(
id int comment ' Number ',
name varchar(10) comment ' full name ',
country varchar(10) comment ' Country ',
sex varchar(1) comment ' Gender '
) comment ' Role table ';
- Query table structure
desc Table name
# Multiple tables can be queried , Separated by commas
desc charact_tb;
- Query the table creation statement of the specified table
SHOW CREATE TABLE Table name
show create table charact_tb;
- Modify the name of the table
ALTER TABLE Table name RENAME TO The new name of the table
ALTER TABLE charact_tb RENAME TO alter_tbl;
- Delete form
DROP TABLE (IF EXISTS) Table name
DROP TABLE IF EXISTS charact_tb;
- Delete the table and recreate ( format )
TRUNCATE TABLE Table name
TRUNCATE TABLE charact_tbl;
- Add table fields
ALTER TABLE Table name ADD Field name type Will be added to the end of the form
alter table charact_tb add age int;
- Modify field type and name
ALTER TABLE Table name MODIFY Field name type ;Only modify the data type
alter table charact_tb modify age tinyint;
ALTER TABLE Table name MODIFY Field name new field name type
alter table charact_tb change age school varchar(10);
- Delete table fields
ALTER TABLE Table name DROP Table field name ; If there is only one field left in the data table, it cannot be used DROP To delete fields
alter table charact_tb drop school;
- Query data from table
SELECT Column name FROM The name of the table
# Query multiple columns can be separated by commas
select name from alter_tbl;
SELECT * FROM The name of the table'*' Represents all columns in the table
select * from alter_tbl;
- Insert data in the form
INSERT INTO Table name ( Table field )VALUES( Inserted data );
insert into user(name,age,school,grade,gender,date)
-> VALUES
-> (" Xiao He ",17,"xx university "," Freshman "," Woman ",now());
where Clause
Used to specify the criteria for selection , It can be used to add, delete, query and modify
- Find data
SELECT Table field FROM The name of the table WHERE Table field Operator value
select * from user where age>17;
AND and OR Operator
Further filtering of information
- AND Operator
# Use AND Displays age greater than 17 And the grade is freshman
select * from user where age>17 AND grade=" Freshman ";
- OR Operator
# Use OR Displays age greater than 17 Or the grade is freshman
select * from user where age>17 OR grade=" Freshman ";
- Use at the same time OR and AND Operator
# Use OR Displays age greater than 17 Or is a freshman and younger than 19
select * from user where(age>17 OR grade=" Freshman ") AND age<19;
LIKE Clause
Fuzzy matching
- % wildcard
- After the character means that the matching starts from these characters
# Inquire about id With 1 At the beginning
select * from user where id LIKE "1%" ;
- Before the character generation, it means that the match ends with these characters
# Inquire about id With 1 At the end of the
select * from user where id LIKE "%1" ;
- There are... On both sides of the character % Means to contain
# The query date contains 16 Of
select * from user where date LIKE "%16%" ;
- _ Operator
- Equivalent to replacing any 1 Characters
# _ Instead of the second place of age
select * from user where age LIKE "1_" ;
IN The operator
stay WHERE Multiple values specified in clause
SELECT Table field FROM Table name WHERE Table field IN ( Table field value )
SELECT * FROM user WHERE age IN (16,17);
BETWEEN The operator
Select a data range between two values , and AND Continuous use
SELECT Table field FROM Table name WHERE Table field BETWEEN Table field value 1 AND Table field value 2
Include table field values 1, Include table field values 2
select * from user where id BETWEEN 1 AND 3;
stay BETWEEN Before to add NOT, Out of selection
select * from user where id NOT BETWEEN 4 AND 11;
UNION The operator
Merge two or more SELECT Result of statement
Duplicate values are not displayed
UNION ALL You can display duplicate values
Sort
ORDER BY Used to define how to sort
# Default ascending order
select * from employees order by last;
desc Descending
select * from employees order by last desc;
asc Ascending
select * from employees order by last asc;
grouping
group by Group the data
# Count count()
select age ,count(*) from user group by age;
Update data
UPDATE Table name SET Field name = The new value WHERE Field name The operator value
update user SET school="xxx university " where age=16;
Delete data
DELETE FROM Table name WHERE Field name The operator value
delete from user where age<17;
JOIN Clause
- INNER JOIN
Get the records of field matching relationship in two tables
The two tables must match
select * from user inner join employees on employees.id=user.id;
This is equivalent to
select * from user ,employees where employees.id =user.id;
- LEFT JOIN
Get all the records in the left table , Even if the right table does not have a matching record
select * from employees left join user on employees.id=user.id order by employees.id ;
- RIGHT JOIN
Get all the records in the right table , Even if the left table does not have a matching record
select * from user right join employees on employees.id=user.id order by employees.id ;
SELECT DISTINCT sentence
List different values
边栏推荐
- death_ satan/hyperf-validate
- bash install. SH ******** error
- windows上安装redis并永久修改密码,及ssm框架集成redis
- AutoCAD C# 多段线小锐角检测
- API learning of OpenGL (2006) glclientactivetexture
- High quality domestic stereo codec cjc8988, pin to pin replaces wm8988
- YYGH-6-微信登录
- The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
- Object object to list collection
- Mosaic data enhanced mosaic
猜你喜欢

ES9023音频解码芯片的工作原理

YYGH-BUG-02

YYGH-BUG-02

深度学习19种损失函数

death_ satan/hyperf-validate

How the third-party libraries in cocoapod reference local header files
![Taobao seo training video course [22 lectures]](/img/81/21e844542b35010760d061abe905e9.jpg)
Taobao seo training video course [22 lectures]

High quality domestic stereo codec cjc8988, pin to pin replaces wm8988

ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用

AutoCAD C# 多段线自相交检测
随机推荐
Camx架构开UMD、KMD log以及dump图的方式
pkg打包node工程(express)
MySQL common functions
cocoapod中的第三方库怎么引用本地头文件
death_ satan/hyperf-validate
Global country (and region) information JSON data
No one can only use foreach to traverse arrays, right?
慢内容广告:品牌增长的长线主义
Enum
YYGH-BUG-03
借助nz-pagination中的let-total解析ng-template
JDBC学习(一)——实现简单的CRUD操作
MR-WordCount
Main functions of 5ggnb and ng ENB
CAD secondary development +nettopologysuite+pgis reference multi version DLL
Academic search related papers
AttributeError: 'callable_iterator' object has no attribute 'next'
深度學習19種損失函數
API learning of OpenGL (2006) glclientactivetexture
Independent station sellers are using the five e-mail marketing skills, do you know?