数据库database
#创建数据库,指定字符集UTF8,缺省情况下新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。
CREATE DATABASE devdb ENCODING 'UTF8' template = template0;
CREATE DATABASE testdb;
标识符的命名需要遵守如下规范:
* 标识符需要为字母(a-z)、下划线(_)、数字(0-9)或美元符号($)。
* 标识符必须以字母(a-z)或下划线(_)开头。
ENCODING [ = ] 'encoding' 可选。指定数据库使用的字符编码
TEMPLATE [ = ] template可选。模板名。即从哪一个模板创建新数据库。GaussDB采用从模板数据库复制的方式来创建新的数据库。
#修改数据库搜索路径
ALTER DATABASE testdb SET search_path TO pa_catalog,public;
#查询搜索路径
show search_path;
#查询数据库
SELECT datname FROM pg_database;
\l
#切回切换回默认数据库postgres
\c postgres
#重命名数据库
ALTER DATABASE testdb RENAME TO testdb1
表table
#行存表
CREATE TABLE PART ( P_PARTKEY BIGINT NOT NULL, P_NAME VARCHAR(55) NOT NULL, P_MFGR CHAR(25) NOT NULL, P_BRAND CHAR(10) NOT NULL, P_TYPE VARCHAR(25) NOT NULL, P_SIZE BIGINT NOT NULL, P_CONTAINER CHAR(10) NOT NULL, P_RETAILPRICE DECIMAL(15,2) NOT NULL, P_COMMENT VARCHAR(23) NOT NULL ) WITH (ORIENTATION = ROW);
#列存表
CREATE TABLE PART1 ( P_PARTKEY BIGINT NOT NULL, P_NAME VARCHAR(55) NOT NULL, P_MFGR CHAR(25) NOT NULL, P_BRAND CHAR(10) NOT NULL, P_TYPE VARCHAR(25) NOT NULL, P_SIZE BIGINT NOT NULL, P_CONTAINER CHAR(10) NOT NULL, P_RETAILPRICE DECIMAL(15,2) NOT NULL, P_COMMENT VARCHAR(23) NOT NULL ) WITH (ORIENTATION = COLUMN);
#查看表信息
\d
#增加列
ALTER TABLE part ADD COLUMN p_col1 bigint;
#增加列默认值
ALTER TABLE part ALTER COLUMN p_col1 SET DEFAULT 1;
#删除列默认值
ALTER TABLE part ALTER COLUMN p_col1 drop DEFAULT ;
#修改列数据类型
ALTER TABLE part MODIFY p_col1 INT;
#修改列名称
ALTER TABLE part RENAME p_col1 to p_col;
#查看表结构
\d part
#添加约束
CREATE UNIQUE INDEX bmsql_oorder_idx1 ON bmsql_oorder USING btree (o_w_id, o_d_id, o_carrier_id, o_id);
#添加索引
create index bmsql_customer_idx1 on bmsql_customer (c_w_id, c_d_id, c_last, c_first);
#查看索引
\di bmsql_customer_idx1
用户user
#创建用户
CREATE USER hccdp SYSADMIN IDENTIFIED BY "NHY^7ujm";
CREATE USER jim PASSWORD 'Gaussdb@123';
CREATE USER dim CREATEDB PASSWORD 'Gaussdb@123';
#修改密码
ALTER USER jim IDENTIFIED BY 'Abcd@123' REPLACE 'Gaussdb@123';
#添加权限
ALTER USER jim CREATEROLE;
#锁定用户 解锁用户
ALTER USER jim ACCOUNT LOCK;
ALTER USER jim ACCOUNT UNLOCK;
#删除用户
DROP USER jim? CASCADE;
#查看角色
\du jim
#查看用户列表
SELECT * FROM pg_user;
模式Schema
#创建模式 在数据库创建用户时,系统会自动帮助用户创建一个同名Schema
CREATE SCHEMA ds;
#更改模式名
ALTER SCHEMA ds RENAME TO ds_new;
#修改模式的所有者
ALTER SCHEMA ds_new OWNER TO jack;
#查看模式的所有者
SELECT s.nspname,u.usename AS nspowner FROM pg_namespace s, pg_user u WHERE s.nspowner = u.usesysid;
#查看模式
\dn
权限
#sysadmin权限授权给他
CREATE USER joe PASSWORD 'Gaussdb@123';
ALTER USER joe with sysadmin;
#查看用户权限变化
\du
#撤回权限
ALTER USER joe with nosysadmin;
#使用schema权限
GRANT USAGE ON SCHEMA tpcds TO joe;
#表所有权限
GRANT ALL PRIVILEGES ON tpcds.reason TO joe;
#列的权限
GRANT select (r_reason_sk,r_reason_id,r_reason_desc),update (r_reason_desc) ON tpcds.reason TO joe;
#数据库连接权限 创建schema 且允许joe将此权限授权给其他用户
GRANT create,connect on database postgres TO joe WITH GRANT OPTION;
#创建角色
CREATE ROLE tpcds_manager PASSWORD 'Gaussdb@123';
#赋予权限给角色
GRANT USAGE,CREATE ON SCHEMA tpcds TO tpcds_manager;
存储过程示例
标签:Insert,NULL,gaussdb,ALTER,values,into,权限,数据库,CREATE From: https://blog.csdn.net/Story_begins/article/details/142887115#创建表 插入数据
CREATE TABLE employee (id varchar(100),name varchar(100),salary int);
CREATE TABLE ep_grade (id varchar(100),grade varchar(100),flag int);
Insert into employee values(1,'张三',5000);
Insert into employee values(2,'李四',5000);
Insert into employee values(3,'王五',5000);
Insert into employee values(4,'赵六',5000);
Insert into employee values(5,'太一',5000);
Insert into employee values(6,'太二',5000);
Insert into employee values(7,'太三',5000);
Insert into employee values(8,'太四',5000);
Insert into ep_grade values(1,'A',2022);
Insert into ep_grade values(2,'S',2021);
Insert into ep_grade values(3,'B',2021);
Insert into ep_grade values(4,'B',2021);
Insert into ep_grade values(5,'A',2021);
Insert into ep_grade values(6,'C',2022);
Insert into ep_grade values(7,'C',2021);
Insert into ep_grade values(8,'B',2022);
#创建存储过程
CREATE OR REPLACE PROCEDURE proc_emp()
AS
DECLARE
EP_ID VARCHAR(100);
GRADE VARCHAR(10);
SALARY INT;
CURSOR C1 IS select distinct id,grade FROM ep_grade where Flag = 2021;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO EP_ID,GRADE;
EXIT WHEN C1%NOTFOUND;
Case GRADE
when 'S'
then update employee set salary = salary+1000 where id=EP_ID ;
when 'A'
then update employee set salary = salary+500 where id=EP_ID ;
when 'B'
then update employee set salary = salary+100 where id=EP_ID;
when 'C'
then update employee set salary = salary-200 where id=EP_ID;
END Case;
DBE_OUTPUT.PRINT_LINE('ID: '||EP_ID||', Grade: '||GRADE||', updated.');
END LOOP;
CLOSE C1;
END;
/
#调用存储过程
call proc_emp();