Basic commands commonly used in mysql

Improve data processing and work efficiency by learning mysql commands

basic command

1. Log in to MySQL
mysql -u root -p
2. View all databases in the current system
show databases;
insert image description here

3. Switch database
use database name
insert image description here

4. View all tables under the database

show tables;
insert image description here

5. View table structure;
desc table name;
insert image description here
6. Create database
create database name;

7. Create a table:
create table table name (field name + space + data type);

8. Add value:
insert into table name (column name) values ​​(value);
9. View all data in the table:
select * from table name;
10. Query the structure when creating a table: (original ecological sql statement)
show create table Table Name;

insert image description here

Delete the value in the field:
delete from table name where condition;

Delete a field in a table:
delete from table name drop column field name; or alter table table name drop field name
Delete table:
drop table table name;
delete library:
drop database library name;
primary key constraint: primary key
unique constraint: unique
is not empty Constraints: not null
Default constraints: default
Foreign key constraints: foreign key (foreign key) references main table (primary key)

查看别的数据库的表格:show tables from 表名;

2. where conditional query

Exact query: =, !=, >, <, >=, <=
Fuzzy query: like (like), not like (not like)
Wildcard: %: any character, -: single character
Logical operator:
and: satisfy at the same time (Priority is greater than or)
or: Any condition can be satisfied
Interval operation: between a and b (closed interval)
Set operation: in, not in
Non-null operation: is null, is not null

3. Operations on the data in the table

Add: insert into table name (column name) values ​​(value);
delete: delete from table name where condition;
view: select * from table name where condition;
modify: update table name set field = new value where condition;

Sorting: order by field name; (asc ascending order, desc descending order)
example: select * from table name order by column name 1 asc, column name 2 desc;
modify table name: alter table old table name rename new table name;

Modify the bookid field in the table to id:
alter table table name change bookid id char;
insert image description here

Remove a column:
alter table table name drop column name;

Add a column:
alter table table name add column name char;

insert image description here

Modify the column as a character type
alter table table name modify column name char(20);
add multiple columns:
alter table table name add(xh int(4),zc char(8),ads char(50),);
delete multiple columns :
alter table table name drop xh, zc, ads;
add a field to set the primary key constraint:
alter table table name add id sm unsigned primary key auto_increment;

4. Advanced query (emphasis)

Association query - equivalent query: select * from table name where a.id=b.id and condition

Inner connection: select * from table name 1 inner join table name 2 on table name 1.xh=table name 2.xh where condition;

Left join: select * from table name 1 left join table name 2 on table name 1.xh=table name 2.xh where condition;

Right join: select * from table name 1 right join table name 2 on table name 1.xh=table name 2.xh where condition;

Guess you like

Origin blog.csdn.net/m0_67929156/article/details/130036785