Free learning recommendations:mysql video tutorial
Article Directory
##Advanced data table operations
Preparation: Install MySQL database
Shell script one-click deployment - source code compilation and installation of MySQLcreate 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;
1. Clone the table
Generate the data records of the data table into a new table
Method 1
例:create table TEST01 like TEST; select * from TEST01; desc TEST01; insert into TEST01 select * from TEST; select * from TEST01;
Method 2
例:create table TEST02 (select * from TEST); select * from TEST02;
2. Clear the table and delete all entries in the table Data
Method 1
delete from TEST02;
#After DELETE clears the table, the returned result contains deleted record entries; DELETE works row by row Deleting record data; if there are self-increasing fields in the table, after using DELETE FROM to delete all records, the newly added records will continue to increment and write records from the original largest record ID
例: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;
Method 2
例: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 re-established as it is, so TRUNCATE will clear the table faster than DELETE in terms of speed; after using TRUNCATE TABLE to clear the data in the table, the ID will be recorded again from 1.
3. Create a temporary table
After the temporary table is successfully created, use SHOW TABLES The command cannot see the temporary table created, and the temporary table will be destroyed after the connection exits. If before exiting the connection, you can also perform operations such as addition, deletion, modification, and query, such as using the DROP TABLE statement to manually delete the temporary table directly.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;
4. Create foreign key constraints
Ensure data integrity and consistencyDefinition of foreign key: If the same attribute field x is the primary key in table one but not the primary key in table two, then field x is called the foreign key of table two.
1. Tables with public keywords as primary keys are primary key tables (parent tables, primary tables)
2. Use public keywords as foreign keys The table is a foreign key table (slave table, external table)
例: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 into TEST05 values (1,'zhangsan','20',1); insert into TEST04 values (1,'sleep'); insert into TEST05 values (1,'zhangsan',20,1);
例:drop table TEST04; drop table TEST05; drop table TEST04;
Note: If you want to delete the foreign key Constraint field
Delete the foreign key constraint first, and then delete the foreign key name. This 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 |
---|---|
外键约束 | foreign key |
非空约束 | not null |
唯一约束 | unique [key |
默认值约束 | default |
自增约束 | auto_increment |
五、数据库用户管理
1、新建用户
CREATE USER '用户名'@'来源地址' [IDENTIFIED BY [PASSWORD] '密码'];
‘用户名’:指定将创建的用户名
‘来源地址’:指定新创建的用户可在哪些主机上登录,可使用IP地址、网段、主机名的形式,本地用户可用localhost,允许任意主机登录可用通配符%
‘密码’:若使用明文密码,直接输入’密码’,插入到数据库时由Mysql自动加密;
------若使用加密密码,需要先使用SELECT PASSWORD(‘密码’); 获取密文,再在语句中添加 PASSWORD ‘密文’;
------若省略“IDENTIFIED BY”部分,则用户的密码将为空(不建议使用)
例:create user 'zhangsan'@'localhost' identified by '123123'; select password('123123'); create user 'lisi'@'localhost' identified by password '*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1';
2、查看用户信息
创建后的用户保存在 mysql 数据库的 user 表里
USE mysql; SELECT User,authentication_string,Host from user;
3、重命名用户
RENAME USER 'zhangsan'@'localhost' TO 'wangwu'@'localhost'; SELECT User,authentication_string,Host from user;
4、删除用户
DROP USER 'lisi'@'localhost'; SELECT User,authentication_string,Host from user;
5、修改当前登录用户密码
SET PASSWORD = PASSWORD('abc123'); quit mysql -u root -p
6、修改其他用户密码
SET PASSWORD FOR 'wangwu'@'localhost' = PASSWORD('abc123'); use mysql; SELECT User,authentication_string,Host from user;
7、忘记 root 密码的解决办法
1、修改 /etc/my.cnf 配置文件,不使用密码直接登录到 mysql
vim /etc/my.cnf [mysqld] skip-grant-tables #添加,使登录mysql不使用授权表 systemctl restart mysqld mysql #直接登录
2、使用 update 修改 root 密码,刷新数据库
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
六、数据库用户授权
1、授予权限
GRANT语句:专门用来设置数据库用户的访问权限。当指定的用户名不存在时,GRANT语句将会创建新的用户;当指定的用户名存在时,GRANT 语句用于修改用户信息。 GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'来源地址' [IDENTIFIED BY '密码'];
#权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。 #数据库名.表名:用于指定授权操作的数据库和表的名称,其中可以使用通配符“*”。*例如,使用“kgc.*”表示授权操作的对象为 kgc数据库中的所有表。 #'用户名@来源地址':用于指定用户名称和允许访问的客户机地址,即谁能连接、能从哪里连接。来源地址可以是域名、IP 地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.lic.com”、“192.168.184.%”等。 #IDENTIFIED BY:用于设置用户连接数据库时所使用的密码字符串。在新建用户时,若省略“IDENTIFIED BY”部分, 则用户的密码将为空。
#允许用户wangwu在本地查询 CLASS 数据库中所有表的数据记录,但禁止查询其他数据库中的表的记录。
例: 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;
#允许用户wangwu在本地远程连接 mysql ,并拥有所有权限。
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、查看权限
SHOW GRANTS FOR 用户名@来源地址; 例: SHOW GRANTS FOR 'wangwu'@'localhost';
3、撤销权限
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权限只能用于数据库登陆,不能执行任何操作;USAGE权限不能被回收,即 REVOKE 不能删除用户。
flush privileges;
更多相关免费学习推荐:mysql教程(视频)
The above is the detailed content of Take a look at MySQL database advanced operations. For more information, please follow other related articles on the PHP Chinese website!