MySQL-add, delete, modify, and query commands
1. Basic commands
【note:The content in the following [] can be written or not, depending on actual needs】
Second, the database
1. View the database
show databases;
2.
Use the database name of the database use;
3. Create database
create database [if not exits] database name;
4. Delete the database
drop database [if exits] database name;
Three, table
1. View the table
show tables;
2. Create a table
create table table name (
field name 1 data type [integrity constraint][index][comment],
field name 2 data type [integrity constraint][index][comment],
…
)[table type ][Table Character Set][Comment];
3. Delete table
drop table table name;
4. View the table structure
desc table name;
Mention from the table creation:
[Note: the integrity constraints can be: not null, primary key, auto_increment, etc.
Table type: ENGINE=MYISAM/InnoDB...;
table character set: charset=utf8;
comment: comment'comment content']
An extension of the
data type . The data type can be as follows:
1. Numerical type
2. String type
3. Date type
4. NULL type
Extend the table type and the difference between MYISAM and InnoDB
Fourth, modify the table structure
1. Modify the table name
alter table old table name rename new table name;
2. Add field
alter table table name add field name column type [attribute];
3. Modify the field
alter table table name modify field name column type [attribute];
alter table table name change old field name, new field name column type [attribute];
4. Delete the field
alter table table name drop field name;
5. Foreign key When
creating a table, add a foreign key
create table table name (field list..., [constraint index name] foreign key (field name of this table) references foreign table name (field name of foreign table));
When the table already exists, modify the foreign key
alter table table name add [constraint index name] foreign key (field name of this table) references foreign table name (field name of foreign table);
Delete foreign key
alter table table name drop foreign key index name;
Fifth, the addition, deletion, modification and investigation of the contents of the table
1. Insert
insert into table name (field name 1, field name 2...) values (field name 1, field name 2...);
2. Modify the
update table name set column name = value where condition;
[condition: filter condition]
3. Delete
delete from table name where condition;
truncate table name; the
difference between the two:
truncate only deletes the contents of the table, does not delete the table structure, indexes, constraints, etc., faster, recovery is difficult,
and delete deletes all and deletes the content Empty all
4. Query
【as】:起别名
SELECT StudentNo AS “学号” FROM student;
SELECT a.StudentNo FROM student AS a;
SELECT Phone+1 AS Tel FROM student;
例如:select stu_id id,stu_name 名字 from student [where condition];
[Distinct]: de-
duplication select distinct field name 1, field name 2... from table name [where condition];
[Select syntax]:
Logical operator
[Between…and…]: range
select field 1, field 2…from table name where field between value 1 and value 2;
[note: left and right are closed intervals]
[Like]: fuzzy query
like "regular expression"
[in]: in the middle, replace or
select field 1, field 2...from table name where field in (value 1, value 2, ...);
[Group by]: group
group by field [having condition]: according to which field to group, if you want to add conditions, please write it after the having statement, not arbitrarily in the where statement
[Order by]: sort
Strictly follow the writing order of the query
Default ascending order: asc, descending order: desc
[Limit]: limit the number of
displayed data limit n: display the first n data
limit (m,n): display the n data from the mth data to the next
limit (m,n) is equivalent to limit n offset m
Associate
Internal association
join or inner join: show the data that can be connected to multiple tables (common)
External association
Left join: left join: will display all the data in the left table, and the right table can be linked to the data.
Right join: right join: will display all the data in the right table, and the left table can be linked.
Full join: full join (only after mysql 5.8 Yes), which is equivalent to left connection union all right connection
The difference between union and union all:
Using union all will splice the data vertically,
union is to remove the duplication on the basis of union all
Self-connection
Typical example:
There is a category table, pay attention to this is [Zipper table: pid points to categoryId]
In order to make them automatically classified, use the following query statement:
select a.categoryName 分类,b.categoryName 专业 from category a join category b on a.categoryId = b.pid;