Advanced operation of MySQL database (detailed picture and text)

Data table advanced operations

Preparation: Install MySQL database

Shell script one-click deployment-source code compilation and installation of MySQL

create database CLASS;
use CLASS;

create table TEST (id int not null,name char(20) 	not null,cardid varchar(18) not null unique 	key,primary key (id));

insert into TEST(id,name,cardid) values (1,'zhangsan','123123');

insert into TEST(id,name,cardid) values (2,'lisi','1231231');

insert into TEST(id,name,cardid) values (3,'wangwu','12312312');
select * from TEST;

Insert picture description here


One, clone table

Generate the data records of the data table into a new table

method one

例:create table TEST01 like TEST;
select * from TEST01;

desc TEST01;
insert into TEST01 select * from TEST;
select * from TEST01;

Insert picture description here

Method Two

例:create table TEST02 (select * from TEST);
select * from TEST02;

Insert picture description here

2. Empty the table and delete all data in the table

method one

delete from TEST02;

#DELETE After emptying the table, there are deleted record entries in the returned result; DELETE works by deleting record data line by line; if there are self-growth fields in the table, use DELETE FROM to delete all records, and the newly added records will be Continue to increment and write records after the original largest record ID

Insert picture description here

例:create table if not exists TEST03 (id int primary 	key auto_increment,name varchar(20) not null,cardid varchar(18) not null unique key);
show tables;

insert into TEST03 (name,cardid) values ('zhangsan','11111');		
select * from TEST03;
delete from TEST03;

insert into TEST03 (name,cardid) values ('lisi','22222');
select * from TEST03;

Insert picture description here

Method Two

例:select * from TEST03;
truncate table TEST03;
insert into TEST03 (name,cardid) values ('wangwu','33333');
select * from TEST03;

#TRUNCATE After clearing the table, no deleted entries are returned; when TRUNCATE works, the table structure is rebuilt as it is, so in terms of speed, TRUNCATE will be faster than DELETE to clear the table; after using TRUNCATE TABLE to clear the data in the table, the ID will change from 1. Start recording again.

Insert picture description here


Three, create a temporary table

After the temporary table is created successfully, the temporary table created cannot be seen using the SHOW TABLES command. The temporary table will be destroyed after the connection exits. Before exiting the connection, you can also perform operations such as adding, deleting, modifying, and checking, such as using the DROP TABLE statement to manually directly delete the temporary table.

CREATE TEMPORARY TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]);

例:create temporary table TEST04 (id int not null,name varchar(20) not null,cardid varchar(18) not null unique key,primary key (id));
show tables;

insert into TEST04 values (1,'haha','12345');	
select * from TEST04;

Insert picture description hereInsert picture description here

Four, create foreign key constraints

Ensure the integrity and consistency
of the data. The definition of a foreign key: If the same attribute field x is the primary key in Table 1, but is not the primary key in Table 2, then the field x is called the foreign key of Table 2.

Understanding of primary key tables and foreign key tables:
1. Tables with common keywords as primary keys are primary key tables (parent table, primary table)
2. Tables with common keywords as foreign keys are foreign key tables (from table, foreign table)

Note: The fields of the primary table associated with the foreign key must be set as the primary key, and the secondary table cannot be a temporary table. The fields of the primary and secondary tables have the same data type, character length and constraints

例:create table TEST04 (hobid int(4),hobname varchar(50));
create table TEST05 (id int(4) primary key auto_increment,name varchar(50),age int(4),hobid int(4));

alter table TEST04 add constraint PK_hobid primary key(hobid);
alter table TEST05 add constraint FK_hobid foreign key(hobid) references TEST04(hobid);

Insert picture description here

例:添加数据记录
insert into TEST05 values (1,'zhangsan','20',1);
insert into TEST04 values (1,'sleep');
insert into TEST05 values (1,'zhangsan',20,1);

Insert picture description here

例:drop table TEST04;
drop table TEST05;
drop table TEST04;

Insert picture description here
Note: If you want to delete a foreign key constraint field
, delete the foreign key constraint first, and then delete the foreign key name, which is not demonstrated here

show create table TEST05;
alter table TEST05 drop foreign key FK_hobid;
alter table TEST05 drop key FK_hobid;
desc TEST05;

6 common constraints in MySQL

Primary key constraint primary key
Foreign key constraint foreign key
Non-empty constraint not null
Unique constraint unique [key
Default value constraints default
Self-increase constraint auto_increment

Five, database user management

1. Create a new user

CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];

'Username': Specify the username that will be created

'Source address': Specify the host on which the newly created user can log in. You can use the form of IP address, network segment, and host name. Local users can use localhost, and any host can log in with wildcard%

'Password': If you use a plain text password, directly enter the'password', it will be automatically encrypted by Mysql when inserted into the database;
------If you use an encrypted password, you need to use SELECT PASSWORD('password'); to get the cipher text, Then add PASSWORD'ciphertext' in the statement;
------If the "IDENTIFIED BY" part is omitted, the user's password will be empty (not recommended)

例:create user 'zhangsan'@'localhost' identified by '123123';
select password('123123');
create user 'lisi'@'localhost' identified by password '*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1';

Insert picture description here


2. View user information

The created user is saved in the user table of the mysql database

USE mysql;
SELECT User,authentication_string,Host from user;

Insert picture description here

3. Rename the user

RENAME USER 'zhangsan'@'localhost' TO 'wangwu'@'localhost';
SELECT User,authentication_string,Host from user;

Insert picture description here

4. Delete users

DROP USER 'lisi'@'localhost';
SELECT User,authentication_string,Host from user;

Insert picture description here

5. Modify the password of the currently logged in user

SET PASSWORD = PASSWORD('abc123');
quit
mysql -u root -p

Insert picture description here

6. Modify other user passwords

SET PASSWORD FOR 'wangwu'@'localhost' = PASSWORD('abc123');
use mysql;
SELECT User,authentication_string,Host from user;

7. The solution to forget the root password

1, Modify the /etc/my.cnf configuration file, log in to mysql directly without using a password

vim /etc/my.cnf
[mysqld]
skip-grant-tables					#添加,使登录mysql不使用授权表
systemctl restart mysqld
mysql								#直接登录

Insert picture description here
Insert picture description here

2, Use update to modify the root password and refresh the database

UPDATE mysql.user SET AUTHENTICATION_STRING = PASSWORD('112233') where user='root';
FLUSH PRIVILEGES;
quit

再把 /etc/my.cnf 配置文件里的 skip-grant-tables 删除,并重启 mysql 服务。
mysql -u root -p
112233

Insert picture description here

Six, database user authorization

1. Grant permissions

GRANT语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,GRANT语句将会创建新的用户;当指定的用户名存在时,GRANT 语句用于修改用户信息。

GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];

#权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。

#数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符“*”。*例如,使用“kgc.*”表示授权操作的对象为 kgc数据库中的所有表。

#'用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP 地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.lic.com”、“192.168.184.%”等。

#IDENTIFIED BY:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略“IDENTIFIED BY”部分, 则用户的密码将为空。

#Allow user wangwu to query the data records of all tables in the CLASS database locally, but prohibit querying the records of tables in other databases.

例:
GRANT select ON CLASS.* TO 'wangwu'@'localhost' IDENTIFIED BY '123456';
quit;
mysql -u wangwu -p
123456
show databases;
use information_schema;
show tables;
select * from INNODB_SYS_TABLESTATS;

#Allow user wangwu to connect to mysql locally and remotely and have all permissions.

quit;
mysql -u root -p112233
GRANT ALL PRIVILEGES ON *.* TO 'wangwu'@'localhost' IDENTIFIED BY '123456';

flush privileges;
quit

mysql -u wangwu -p123456
create database SCHOOL;

2. View permissions

SHOW GRANTS FOR 用户名@来源地址;

例:
SHOW GRANTS FOR 'wangwu'@'localhost';

3. Revocation of authority

REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址;

例:quit;
mysql -u root -p112233
SHOW GRANTS FOR 'wangwu'@'localhost';
REVOKE SELECT ON "CLASS".* FROM 'wangwu'@'localhost';

SHOW GRANTS FOR 'wangwu'@'localhost';

#USAGE permission can only be used for database login and cannot perform any operations; USAGE permission cannot be recycled, that is, REVOKE cannot delete users.

flush privileges;

Guess you like

Origin blog.csdn.net/weixin_51432770/article/details/113154390