您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

看看MySQL数据库高级操作

2025/7/11 12:17:27发布30次查看
免费学习推荐:mysql视频教程
文章目录
数据表高级操作准备工作:安装mysql数据库一、克隆表方法一方法二二、清空表,删除表内的所有数据方法一方法二三、创建临时表四、创建外键约束mysql中6种常见的约束五、数据库用户管理1、新建用户2、查看用户信息3、重命名用户4、删除用户5、修改当前登录用户密码6、修改其他用户密码7、忘记 root 密码的解决办法六、数据库用户授权1、授予权限2、查看权限3、撤销权限数据表高级操作
准备工作:安装mysql数据库
shell脚本一键部署——源码编译安装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;
一、克隆表
将数据表的数据记录生成到新的表中
方法一
例:create table test01 like test;select * from test01;desc test01;insert into test01 select * from test;select * from test01;
方法二
例:create table test02 (select * from test);select * from test02;
二、清空表,删除表内的所有数据
方法一
delete from test02;
#delete清空表后,返回的结果内有删除的记录条目;delete工作时是一行一行的删除记录数据的;如果表中有自增长字段,使用delete from 删除所有记录后,再次新添加的记录会从原来最大的记录 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;
方法二
例:select * from test03;truncate table test03;insert into test03 (name,cardid) values ('wangwu','33333');select * from test03;
#truncate 清空表后,没有返回被删除的条目;truncate 工作时是将表结构按原样重新建立,因此在速度上 truncate 会比 delete 清空表快;使用 truncate table 清空表内数据后,id 会从 1 开始重新记录。
三、创建临时表
临时表创建成功之后,使用show tables命令是看不到创建的临时表的,临时表会在连接退出后被销毁。 如果在退出连接之前,也可以可执行增删改查等操作,比如使用 drop 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;
四、创建外键约束
保证数据的完整性和一致性
外键的定义:如果同一个属性字段x在表一中是主键,而在表二中不是主键,则字段x称为表二的外键。
主键表和外键表的理解:
1、以公共关键字作为主键的表为主键表(父表、主表)
2、以公共关键字作为外键的表为外键表(从表、外表)
注意:与外键关联的主表的字段必须设置为主键,要求从表不能是临时表,主从表的字段具有相同的数据类型、字符长度和约束
例: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;
注:如果要删除外键约束字段
先删除外键约束,再删除外键名,此处不演示
show create table test05;alter table test05 drop foreign key fk_hobid;alter table test05 drop key fk_hobid;desc test05;
mysql中6种常见的约束
主键约束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');quitmysql -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 mysqldmysql #直接登录
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 -p112233
六、数据库用户授权
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 -p123456show databases;use information_schema;show tables;select * from innodb_sys_tablestats;
#允许用户wangwu在本地远程连接 mysql ,并拥有所有权限。
quit;mysql -u root -p112233grant all privileges on *.* to 'wangwu'@'localhost' identified by '123456';flush privileges;quitmysql -u wangwu -p123456create database school;
2、查看权限
show grants for 用户名@来源地址;例:show grants for 'wangwu'@'localhost';
3、撤销权限
revoke 权限列表 on 数据库名.表名 from 用户名@来源地址;例:quit;mysql -u root -p112233show grants for 'wangwu'@'localhost';revoke select on class.* from 'wangwu'@'localhost';show grants for 'wangwu'@'localhost';
#usage权限只能用于数据库登陆,不能执行任何操作;usage权限不能被回收,即 revoke 不能删除用户。
flush privileges;
更多相关免费学习推荐:mysql教程(视频)
以上就是看看mysql数据库高级操作的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product