GBase 8a MPP Cluster V95 SQL 综合练习
一、安全管理
使用数据库超户root连接数据库后,创建数据库,创建新用户组和用户,并给新用户组和用户分配相关权限
① 连接登录集群数据库(默认管理员为root,密码为空)
gccli -u用户名 -p密码
1 | gccli -uroot -p |
② 给root用户设置密码,保障安全
1 | set password for root =password('Peixun_2021'); |
③ 创建数据库
create database if not exists 数据库名;
检查数据库是否创建成功
1 2 | show databases; create database if not exists mydb; |
use 数据库名;
1 | use mydb; |
④ 创建用户组
1 | create role role1; |
⑤ 给用户组赋予数据库级权限,首先选择要赋权限的数据库
grant all on 数据库名.* to 用户组名;
1 | grant all on mydb.* to role1; |
⑥ 创建用户
create user 用户名 identified by ‘密码’;
1 | create user user1 identified by '123456'; |
⑦ 给用户授于用户组权限
grant 用户组名 to 用户;
1 | grant role1 to user1; |
⑧ 显示该用户和用户组的权限
1 2 | show grants for user1; show grants for role1; |
⑨ 在系统库中查询用户和用户组对应关系
1 2 | select user from gbase.user; select * from gbase.role_edges; |
⑩ 使用新用户登录8a系统
1 | gccli -uuser1 -p -D mydb |
11 进入有权限数据库,创建student表,操作如下:
– 切换数据库
1 2 | show databases; use mydb; |
12 查看当前用户和用户的权限信息
1 | show grants for current_user(); |
二、数据库对象基本操作
创建hash分布表、复制表、随机分布表、视图、索引等
继续使用user1账户进行操作即可
① 创建hash分布表
– 在库中创建hash分布表
1 2 3 4 5 6 7 | create table student( sno varchar(20) DEFAULT NULL COMMENT '学号', sname varchar(20) DEFAULT NULL COMMENT '姓名', ssex varchar(3) DEFAULT NULL COMMENT '性别(男、女)', sage int(4) DEFAULT NULL COMMENT '年龄', sdept varchar(20) DEFAULT NULL COMMENT '系') distributed by ('sno'); |
– 查看列名
1 2 | desc student; show create table student; |
② 创建复制表
– 在库中创建复制表
1 2 3 4 5 | create table course(cno int(4) DEFAULT NULL COMMENT '课程号', cname varchar(20) DEFAULT NULL COMMENT '课程名', cpno int(4) DEFAULT NULL COMMENT '选修系', Ccredit int(4) DEFAULT NULL COMMENT '学分') replicated ; |
③ 创建随机分布表
1 2 3 4 5 | drop table if exists sc; create table sc( sno varchar(20) DEFAULT NULL COMMENT '学号', cno int(4) DEFAULT NULL COMMENT '课程号', grade int(4) DEFAULT NULL COMMENT '成绩'); |
④ – 查询建表语句
1 | show create table course; |
⑤ – 插入数据
1 2 3 4 5 6 7 8 | insert into student values ('200215121','李勇','男',20,'CS'),('200215122','刘晨','女',19,'CS'),('200215123','王敏','女',18,'MA'), ('200215125','张立','男',19,'IS'); insert into course values (1,'数据库',5,4),(2,'数学',null,2),(3,'信息系统',1,4),(4,'操作系统',6,3),(5,'数据结构',7,4),(6,'数据处理',null,2),(7,'PASCAL语言',6,4); insert into sc values ('200215121',1,92),('200215121',2,85),('200215121',3,88),('200215122',2,90),('200215122',3,80),('200215123',3,56), ('200215123',5,80),('200215123',1,66), ('200215126',3,58),('200215126',6,54); |
⑥ – 查询数据
1 2 3 4 5 6 | select count(*) from student; desc student; select * from student; select * from course; select * from sc; |
⑦ – 新建一个视图,显示学生成绩表,字段包括学生姓名、课程、成绩’
1 2 3 | CREATE OR REPLACE VIEW v_sc AS SELECT sname, cno, grade FROM student s,sc where s.sno=sc.sno; |
⑧ – 显示表和视图名字
1 2 3 | show tables; show create view v_sc; |
⑨ – 修改表sc,在课程号后面增加一列课程名称
1 2 3 4 | alter table sc add cname varchar(20) DEFAULT NULL after cno; desc sc; show create table sc; |