Use command to operate mysql database

Use the command to operate the mysql database:
(1) Use the command to connect the
command operation mode, which is used more in the work, so to achieve proficiency, open the terminal and run the command:

mysql -uroot -p

Enter the password after pressing Enter. The currently set password is mysql. After the connection is successful, the following appears:
Insert picture description here
indicating successful login, logout, quit or exit.
(2) Remote connection (connecting to a database in a remote server) is
generally under company development. will build a unified database on a server, a public database of all staff development
- run the command: mysql -h ip address -uroot -p
host ip address to connect to the back -h write
-u user name later wrote connection
- p Enter the password after entering

(3) Database operation: Command line operation
Create database: create database database name charset = utf8
delete database: drop database database name
Switch database: use database name;
view the currently selected database: select database ();

(4) Table operation: command line operation
View all tables in the current database: show tables;
create tables: auto_increment means automatic growth
create table table name (column and type);
create table students (
id int auto_increment primary key,
sname varchar 10) not null
);

Modify the table:
alter table table name add | change | drop column name type;
such as: alter table students add birthday datetime;

Delete table:
drop table table name;

View the structure of the table: desc table name;

Change the name of the table: rename table original table name to new table name;

View the table creation statement: you can see how the table was created
show create table 'table name'

(6) Data operation
Query: select * from table name, the most common query
Increase:
full column insertion: insert into table name values ​​(...)
Default insert: insert into table name (column 1, column 2 ...) values (Value 1, ...)
Insert multiple pieces of data at the same time: insert into table name values ​​(...), (...) or insert into table name (column 1 ....) values ​​(value 1 .... ), (Value 1...) The
primary key column is automatically increased, but requires a placeholder when inserting the entire column, usually using 0, after the successful insertion is based on the actual

Modification:
update table name set column 1 = value 1, . . where condition

Delete:
delete from table name where condition
Logical deletion, the essence is to modify the operation update
alter table students add isdelete bit default 0;
if you need to delete: update students isdelete = 1 where…;

(7) Backup and recovery
Data backup: enter the super administrator sudo -s
into the mysql library directory
cd / var / lib / mysql and
run the mysqldump command: mysqldump -uroot -p database name> xxx.sql
first convert the database to xxx.sql file

Data recovery:
(1) Connect to mysql on the command line, create a new database to restore xxx.sql software
(2) Exit the connection, execute the following command
mysql -uroot -p new database name <xxxx.sql
Enter sql according to the prompt Password, and then the data is restored to the new database, then you can operate on the new database

Underlying operating Summary:
question (1) database can be resolved, some of the standard database design, ER model, the three paradigms
(2) graphical interface to do gymnastics databases, tables, data
(3) command line databases, tables, data
which are It is a basic operation, the most important thing is to query the database, and then introduce the learned query

Published 129 original articles · Like 43 · Visits 100,000+

Guess you like

Origin blog.csdn.net/nbxuwentao/article/details/104418419