当前位置:网站首页>Advanced operation of MySQL database basic SQL statement tutorial

Advanced operation of MySQL database basic SQL statement tutorial

2022-06-26 15:08:00 1024 Q

Catalog

Preface :

One . Clone table

1.1 Cloning method 1 ( Clone tables and contents separately )

1.2 Cloning method 2 ( Copy the table with the contents )

  Two . Clear the table , Delete all data in the table  

2.1 Method 1

2.2 Method 2

2.3 Little knot drop,truncate,eleted Comparison of

3、 ... and . Create a temporary table

Four . User management

4.1 A new user

4.2 Create users with clear text passwords

4.3 Create a database using ciphertext

5、 ... and . View user information  

6、 ... and . Rename user

  7、 ... and . Delete user

  8、 ... and . Password management

8.1 Change the current user password

8.2 Change the password of other users

8.3 forget root password

8.3.1 to root Set the password

Nine . Database authorization  

9.1 About Authorization

9.2 to grant authorization

9.2.1 Authorization list

9.3 Database authorization

9.4 Remote login authorization ( Use navicat Remote login )

9.5 Revoke authority

Ten . summary

Preface :

Got it MySQL Basic statement of database , In this chapter, learn about its advanced operation , Including adding, deleting and giving corresponding permissions to users

One . Clone table 1.1 Cloning method 1 ( Clone tables and contents separately )#​create table The new name of the table like Replicated table name ; ​​ Copy the format , Can copy the format of a table to a new table , But the contents cannot be copied ​​insert into The new name of the table select * from Replicated table name ; ​​ Copy the contents of the original table to the new table

1.2 Cloning method 2 ( Copy the table with the contents )create table The new name of the table (select * from Replicated table name )​ Data structure and data can be copied together

  Two . Clear the table , Delete all data in the table  2.1 Method 1 delete from naixu1;#DELETE After emptying the table , There are deleted record entries in the returned result ;delete When working, delete record data line by line ; If there is a self growing field in the table , Use DELETE FROM After deleting all records , The record added here will be changed from the original largest record id Continue to write data automatically later

2.2 Method 2 truncate table naixu1;#TRUNCATE After emptying the table , No deleted entries are returned :TRUNCATE The work is to rebuild the table structure as it is, so in terms of speed TRUNCATE than DELETE Empty table quick use TRUNCATE TABLE After clearing the data in the table ,id From 1 Start re recording

2.3 Little knot drop,truncate,eleted Comparison of
droptruncatedelete
Belong to DDL Belong to DDL Belong to DML
Cannot roll back Cannot roll back Roll back
Don't bring where Don't bring where Can take where
Table content and structure delete Table contents delete The table structure is in , The content of the table depends on where Implementation
Delete fast Delete fast Slow to delete , Need to delete line by line

summary :

Use when you no longer need a table drop When you want to delete some data rows, use delete, And bring them up. where Clause to keep the table and delete all data truncate Delete speed :drop>truncate> delete Security delete best
3、 ... and . Create a temporary table ## Add temporary table niaxu3create temporary table naixu3 (id int(4) zerofill primary key auto_increment,name varchar(10) not null,cardid int(18) not null unique key,hobby varchar(50));## View all tables in the current library show tables; ## Add data to temporary table insert into test03 values(1,'hehe',12345,' Look at the beauty '); ## View all data in the current table select * from naixu3;## Exit database quit ## Log in again and view mysql -u root -p## View all data in the temporary table created before , Found to have been automatically destroyed select * from naixu3;

Four . User management 4.1 A new user CREATE USER ' user name '@' Source address ' [IDENTIFIED BY [PASSWORD] ' password '];#‘ user name ': Specify the user name that will be created #‘ Source address ': Specify which hosts the newly created user can log on to , You can use IP Address 、 Network segment 、 The form of host name , Available to local users localhost, Allow any host to log in. Wildcards are available %#‘ password ': If plaintext password is used , Direct input ' password ', Inserted into the database by Mysql Automatic encryption ;####### If you use an encrypted password , You need to use it first SELECT PASSWORD(‘ password '); Get ciphertext , Then add... To the statement PASSWORD ‘ Ciphertext ';# If you omit “IDENTIFIED BY” part , Then the user's password will be empty ( Not recommended )4.2 Create users with clear text passwords create user 'nannan'@'localhost' identified by '123455';

4.3 Create a database using ciphertext

5、 ... and . View user information   The created user is saved in mysql Database user table use mysql; # Use mysql library select User from user;

6、 ... and . Rename user rename user 'nannan'@'localhost' to 'lnhs'@'localhost';# Will the user nannan Renamed as lnhs

  7、 ... and . Delete user drop user 'chenchen'@'localhost';# Delete user chenchen

  8、 ... and . Password management 8.1 Change the current user password set password = password('123456');

8.2 Change the password of other users set password for 'naixu'@'localhost' = password('123456');

8.3 forget root password Modify the configuration file , Add the configuration , Password free login MySQLvim /etc/my.cnfskip-grant-tables # add to , Log in mysql Not applicable to authorization form

8.3.1 to root Set the password update mysql.user set authentication_string = password('123456') where user='root';flush privileges; # Refresh Log in to the database and modify it again my.conf The configuration file , Comment out the previously added configuration command , And restart the service again to log in with a new password

Nine . Database authorization  9.1 About Authorization

  GRANT sentence : It is specially used to set the access rights of database users . When the specified user name does not exist ,GRANT Statement will     New users will be created ; When the specified user name exists ,GRANT Statement is used to modify user information .

9.2 to grant authorization GRANT Permission list ON Database name / Table name TO ' user name '@' Source address ' [IDENTIFIED BY ' password '];
Permission list Used to list various database operations authorized for use , Separated by commas , Such as “select,insert,update”. Use “all” Indicates all permissions , You can authorize any operation .
Database name . Table name The name of the database and table used to specify the authorization operation , You can use wildcards
user name @ Source address Used to specify the user name and the client address to which access is allowed , Who can connect 、 Where can I connect . The source address can be a domain name 、IP Address , You can also use “%” wildcard , Represents all addresses in an area or network segment , Such as “%.accp.com”、“192.168.80.%” etc. .
IDENTIFIED BY Used to set the password string used by users when connecting to the database . When creating a new user , If you omit “IDENTIFIED BY” part , Then the user's password will be empty .
9.2.1 Authorization list
jurisdiction function
select Query data
insert insert data
update Update data
delete Delete data
create Create a library 、 surface
drop Delete Library 、 surface
index Index
alter Change table properties
event event
trigger on Create trigger
9.3 Database authorization show grants for [email protected];# View user permissions

Specify which database or table users can view , Others cannot access

grant select on hehe.* to [email protected];# user nannan Only hehe Query permission of all tables under the library

Switch users for authentication

9.4 Remote login authorization ( Use navicat Remote login )grant all on *.* to 'nannan'@'%' identified by '123456';

9.5 Revoke authority revoke select on hehe.* from [email protected];

  Switch access again , You have no authority

Ten . summary

In this chapter, I will explain MySQL High level statement for , Includes how to clone tables , How to add, delete, and modify users and how to set user permissions , In general, just remember 3 Just click add, delete and modify

This is about MySQL The database is basically SQL This is the end of the advanced operation article of the statement tutorial , More about MySQL SQL Statement advanced operation content please search the previous articles of the software development network or continue to browse the following related articles. I hope you can support the software development network in the future !


原网站

版权声明
本文为[1024 Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261453106160.html