MySql database operation
– Log in to MySql:——–
After entering the cmd in the Windows environment, enter mysql –h localhost –u root –p, and then enter the password to start mysql; where localhost is the ip where the mysql server is located, if it is a local machine, you can use localhost.
– Database related operations – –
create database db_name (database name): create a database named db_name
show databases: view an existing database
drop database db_name: delete a database named db_name
use db_name: operate a database named db_name
show tables : Display the table in the database
desc table_name: View the structure of the table named table_name
– Operation of the table (TABLE) in the database – –
Create table table_name(IDint primary key, NAME varchar(50), AGE int , SEX varchar( 10)): Create a table and set ID as the primary key
create tabletable_name(stu_id int, course_id int, name varchar(20), grade float, primarykey(stu_id, course_id)): Set both stu_id and course_id in the table as primary keys
Foreign key: If an attribute value of table A depends on the primary key of table B, B is called the parent table, A is the child table, and this field in A is the foreign key of A. If the information in the parent table changes, then The data of the corresponding sub-table will also change
Syntax : create tabletable_name01(id int primary key,stu_id int,course_id int ,score float,gradeint,constraint c_fk(foreign key alias) foreign key(stu_id,course_id)references table_name02(stu_id,course_id) );table_name02 is the parent table, table_name01 is the child table, and set two non-null constraints on the foreign key
table field for the child table: create tabletable_name(id int primary key not null,name varchar(50) not null,stu_id int); Setting not null means that the field cannot be null, that is, non-null
Unique constraint, which means that the value in the field cannot be repeated: create tabletable_name(id int primary key auto_increment, stu_id int unique, name varchar(20) not null); where id is set automatically Increase, and unique sets the value of stu_id must be unique, there cannot be the same value in
E, set the default value for the table, that is, when no data is inserted, the default value will be used instead;
Create table table_name(idint primary key auto_increment,stu_id int unique,name varchar(50) not nullEnglish varchar(20) default 'zero');that is, set the default value to zero for the Englist field;
show create table table_name;View the details of the table Structural statement
- table modification operation - -
modify table name: alter table old table name rename [to] new table name; change the table name
Modify field attributes: Alter tabletable_name modify attribute name data type (modified type)
modify field : alter table table_namechange old field name new field name new data type
Add field: alter table table_name add field 1 data type after field 2; add field 1 after field 2; if field 2 is changed to FIRST, it will be added to the front
Delete field: alter table table_namedrop field name
Modify the position of the field: alter tabletable_name modify field name First (the first position, after field, after the specified field)
Change the engine name of the table: alter tabletable_name engine=Mylsam;
delete the foreign key constraint of the table: alter tabletable_name drop foreign key foreign key alias
Drop table:
9.1: Ordinary unrelated table: drop table table_name;
9.2: Delete related table: first use show create table table_name; view the details of the table, see the other name of the foreign key, delete the foreign key first, and then delete the table That's it.
– database addition, deletion, modification and query operation – –
database addition (insert into), delete (delete), change (update), query (select) operation:
1. Add data insert into
A There are two types of data addition: 1. No specific fields are specified. Names such as: insert into table_name values (value 1, value 2...)
Specify the field name: insert intotable_name (field 1, field 2....) values (value 1, value 2....); if it is to add data to the specified field, You only need to write out the fields that need to add data.
Colleagues insert multiple pieces of data: insert intotable_name [field list] values (select list 1), (value list 2)...
Insert data from one table into another table:
Insert into table_name1 (field list) select (table 2 field) fromtable_name2 where conditional expression;
2. Update data (change) operation The
overall update operation is: update table_name set field1=value1, field2=value2...where conditional expression
You can change the data in a certain range, mainly judged from the conditions behind where
3. Delete data operation delete
delete from table_namewhere conditional expression
Delete from table_name; will delete all data;
4. Query data query
Select field name list from table_name [where conditional expression 1] [group by field name [having conditional expression 2]][order by Field name [ASC (ascending order)/DESC (descending order)]]
single-table query: select field name from table_name where condition Query
with in keyword:
determine whether the value of a field is in the specified set, and if so, check it out: select field name or * table_name where field name in (value 1, value 2…..)
query with between and keyword: select * or field name from table_name where field name between value 1 and value 2; looking for the range in Corresponding data between value 1 and value 2; the result is a match containing the values at both ends. A complete string is queried for a match
with like, which can be added with % or ;% means a string of any length, such as b%k means starting with b and ending with k is an arbitrary string of things, but only represents a single character, such as b_k represents a 3-character string starting with b and ending with k
Method : select * or field name from table_name where field name [not]like condition; not means no When matching
- null value query: select * or field name from table_name where field name is [not] null; that is, query [not] null data
Multi-condition query of and and or: select * or field name from table_name where condition 1 and condition 2; and from table_name where condition 1 and condition 2; and means that all conditions must be met, and or means that only one of the conditions needs to be met Just
F, the query results are not repeated: select distinct field name from table_name;
5. Group query If group by is used
alone , the result will only display one record in one group:
Select * or field name from table_name group by field name
group by and The group_concat() function is used: all fields of each group can display the
Select field name, group_coucat (field name) from table_name group by field name-group
by and the collection function use: select field name, count (field name) fromtable_name group by Field name having count (field name) Condition
Multi- field grouping: select * fromtable_name group by field 1, field 2...
E, group by and with rollup use
Select field name, count (field name) from table_name group by field name with rollup
6. Use limit to limit query data
Select * from table_namelimit a or (limit a,b), the former is to display the data from the first to a, the latter is to display the data from a to b
7. Use the aggregate function to query the data
count() statistics Number of records: Select count(*) from table_name
sum() Summation: Select field name, sum(field name) from table_name where condition
avg() Average number: Select avg(field name) from table_name group by field name
max and min maximum and minimum value: Select max(field name)/min(field name) from table_name;
8. Multi-table join query
- inner join query: When there are fields with the same meaning in two or more tables, you can use this field to join For example: select field 1, field 2, field 3...from table_name1, table_name2 wheretable_name1. field a=table_name2. field b
- outer join query: select field list from table_name01 left/right jointable_name02 on table_name01. field name=talbe_name02 .Field name. Letf means left link, right means right link
- compound conditional query: precise query using multiple conditions
Query records ending with a specific character: Select * fromtable_name where regexp 'xx$;
Use the symbol "." to replace any character in the string: Select * from table_name where name regexp '^l..y$';
– table or Field aliases --
table aliases: select * fromtable_name t where t. field=value; t is the table alias
Field alias: use the as keyword, such as: select t_id as field id from table_name wheret_id=value; t_id is The alias of the corresponding field, the alias can be used as the real name
– database backup – –
mysqldump command backup: mysqldump –u username –p db_nametable1,table2….>BackupName.sql;//Where db_name is the name of the database, table1.. It is the table name. If there is no table name, the entire database will be backed up. backupname.sql represents the name of the backup file, and an absolute path can be added in front.
Backup multiple databases: Mysqldump -uusername –p –databases db_name1 db_name2… > backupname.sql
Backup all databases: Mysqldump –u root –p –all-databases > C:\all.sql
Quick backup with Mysqlhotcopy tool
Database restore: Mysql –u root –p < backup.sql// where backup.sql is the saved database file
The above sharing is from the MySQL database training
of Brotherhood. Please indicate the source when reprinting.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326642848&siteId=291194637
Recommended
Ranking