MySQL basic operations
1. Start the service
1. Use ctrl+alt+delete to open the task manager, click on services, and find your MySQL service name. My MySQL service name is MySQL80.
2,
net start MySQL80
2. Turn off the service
net stop MySQL80
mysqladmin -u root -p shutdown
3. Modify user password
mysqladmin -u root -p password 12345
4. Log in
mysql -u root -p
5. Display version
show variables like 'version';
6. Create/Delete Database
Create database db1:
create database db1;
Show all databases:
show databases;
Delete the database db (the following operation is to avoid errors when performing a delete operation without db):
drop database if exists db;
7. Create/Delete Table
Operate under db1 database:
use db1;
Create a table user, which includes two attribute fields userID and userNname:
create table user (userID varchar(10),userNname varchar(10));
Delete table user1:
drop table if exists user1;
8. View all tables in the database
show tables;
9. View the attribute fields of the table
desc user;
10. Search for table data
1. Find all the data in the user table:
select * from user;
2. Find the data containing b in userName in the user table:
11. Add operation to table data
insert into user values('001','a');
12. Modify the table data
Change the userName of the data with userID 004 in the user table to d:
update user set userName='d' where userID='004';
13. Delete operation on table data
Delete the data with userID 004 in the user table:
delete from user where userID='004';
14. Add table attribute fields
Add the userTel attribute field to the table user (the default is to add it at the end):
alter table user
add userTel varchar(10);
Add the userSchool attribute field after the userName attribute field:
alter table user
add userSchool varchar(10) after userName;
Add the userAddr attribute field to the user table to make it at the forefront:
alter table user
add userAddr varchar(10) first;
15. Delete the attribute field of the table
Delete the userAddr attribute field in the user table:
alter table user
drop userAddr;
16. Modify the attribute fields of the table
Change the userSchool attribute field in the table user to userS:
alter table user
change userSchool userS varchar(10);
17. Rename the table
Rename the user table to users:
alter table user
rename users;
18. Backup
Use output redirection to store mydb data to the path after >:
mysqldump -u root -p mydb > C:\mydb_backup.sql
19. Recovery
If the mydb database is deleted, first create an empty mydb database, and then use the following command to use input redirection to write the original backup database to mydb:
mysql -u root -p mydb < C:\mydb_backup.sql