Database principle + openGauss

Article directory

0. Preparation

0.1 Experimental environment

openGauss experimental environment deployment mode: By creating a virtual machine and using the open source operating system openEuler to build it, there is no additional cost.

0.2 Main content

Insert image description here

0.3 Learning resources

  1. Huawei MOOC
    ilearningx
    Database Learning Route
    Huawei Cloud Online Course
  2. Textbook
    Insert image description here

1. Installation and deployment experiment (Experiment 1)

1.1 Download VirtualBox

VirtualBox official website
After downloading, as shown below:
Insert image description here

1.2 Install VirtualBox

  1. When double-clicking VirtualBox to install, an error message is reported:
    Error message: virtualbox installation "This application cannot be run on the device"
    Insert image description here
  2. Turn off memory integrity, after reboot. Double-click VirtualBox to install, the installation path isF:\APP\install\VirtualBox, as shown in the figure below.
    Insert image description here

1.3 Image file import

  1. Open VirtualBox Manager and click Import
    Insert image description here
  2. Select the path of the image file to be importedopenEuler_openGauss.ova, assuming it isF:\APP\DownLoad, and click Next
    Insert image description here
    Image file openEuler_openGauss.ovaAs follows.
    Insert image description here
  3. In the Virtual Computer Import Settings, modify the default virtual computer location, assuming it isF:\APP\data\VirtualBox, and then click Import
    Insert image description here
  4. Wait for the import to complete
    Insert image description here

1.4 Start the virtual machine

  1. After the import is completed, click Start to start the virtual machine
    Insert image description here
  2. Enter usernameroot and passwordopenGauss@123. After successful login, change the password to 123456. The specific steps are as follows:
    Insert image description here
  3. Enterifconfig to check whether the two network cards are normal, as shown below:
    Insert image description here

1.5 Database usage

  1. Use putty to connect to the virtual machine, and configure the IP address of the enp0s3 network card from the local computer (from ifconfig, it is 192.168.56.101) to connect to the virtual machine.
    Insert image description here

  2. Useroot to log in, and the password is the password changed in 5.2.2 above123456
    Insert image description here

  3. Log in to the main database node as the operating system user omm and entersu - omm
    Insert image description here

  4. Start the service, entergs_om -t start, and the following result is displayed, which means the startup is successful.
    Insert image description here

  5. To connect to the database, entergsql -d postgres -p 26000 -r. If the following result is displayed, the connection is successful.
    [postgres is the database generated by default after the openGauss installation is completed. You can initially connect to this database to create a new database. 】
    [26000 is the port number of the database master node. It needs to be replaced according to the actual situation of openGauss. Please confirm to obtain the connection information. 】
    [Before using the database, you need to use a client program or tool to connect to the database. Then you can use the database by executing SQL through the client program or tool. gsql is a command line database connection tool provided by the openGauss database. 】
    Insert image description here

  6. When connecting to the database,omm the user password is: openGauss@123, enteralter role omm identified by 'openGauss@1234' replace 'openGauss@123'; to change the password, the new password isopenGauss@1234
    Insert image description here

  7. Create database user
    By default, only the administrator user created during openGauss installation can access the initial database. EnterCREATE USER ljh WITH PASSWORD "openGauss@1234"; to create other database user accounts. [Other database usersCREATE USER lumi WITH PASSWORD "openGauss@1234"; ]
    Among them, the account number is ljh and the password is openGauss@1234
    Insert image description here

  8. Create database, enterCREATE DATABASE db_tpcc OWNER ljh;, createdb_tpcc database [Other databases:CREATE DATABASE db_lumi OWNER lumi;]
    Insert image description here

  9. After creatingdb_tpcc the database, you can exit the database according to the \q method and enter< a i=4>Use a new user to connect to the database to perform the following operations such as creating tables. [Other users log in to the database:]postgresgsql -d db_tpcc -p 26000 -U ljh -W openGauss@1234 -rljhdb_tpccgsql -d db_lumi -p 26000 -U lumi -W openGauss@1234 -r

  10. When the result is displayed as the following information, it means the connection is successful.
    Insert image description here

1.6 Basic database operations

1. Huawei openGauss (GaussDB) 1.0 User Manual
2. openGauss Database Development Guide (Part 1)
2. openGauss Database Development Guide (Part 2)
4. openGauss Database Developer Guide
5. Huawei Cloud
Error: permission denied for schema public a>

2 Tables & Patterns (Experiment 2)

  1. Create databases, schemas, tables and indexes.
  2. Modify the structure of the basic table.
    Use SQL statements (according to the attributes shown in the textbook) to create basic tables in the OpenGauss database system, such as the STUDENT table, COURSE table, SC table, etc., and fill the tables with data. Be familiar with the code input method of SQL statements in OpenGauss using a virtual machine.
  3. Create and delete indexes
    Use SQL statements to create indexes in the above tables. If you are familiar with the corresponding SQL statements, please refer to the SQL statements on pages P73-79 of the textbook. Please pay attention to standard SQL and The similarities and differences between the corresponding SQL statements in openGauss.

2.1 Creating a pattern

CREATE SCHEMA ljh AUTHORIZATION ljh;
Insert image description here

2.2 Create table

  1. Create students table
    CREATE TABLE students(sno CHAR(9) PRIMARY KEY,sname CHAR(10) NOT NULL,sex CHAR(2) CHECK(sex='男'OR sex='女'), birthday DATE, enrollyear CHAR(4), speciality CHAR(20), dno CHAR(4));
  2. Create courses table
    CREATE TABLE courses(cno CHAR(5) PRIMARY KEY,cname CHAR(20) NOT NULL,period SMALLINT, credit SMALLINT);
  3. Create sc table
    CREATE TABLE sc(sno CHAR(9), cno CHAR(5), grade SMALLINT CHECK(grade>=0 AND grade<=100), PRIMARY KEY (sno,cno), FOREIGN KEY (sno) REFERENCES students (sno), FOREIGN KEY (cno) REFERENCES courses (cno));
    Insert image description here

question:permission denied for schema public
Insert image description here

2.3 Index

  1. Create students_dept index for dno on students table
    db_tpcc=> CREATE INDEX students_dept ON students(dno);
    Insert image description here
  2. Delete the index (\diView the index, DBMS automatically creates an index for the main code)
    DROP INDEX students_dept
    Insert image description here

2.4 Modify table

  1. Modify the data type of the sex column of the students table
    ALTER TEBLE students ALTER sex TYPE char(4);
    Insert image description here

2.5 Insert table data

  1. Insert students table data
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (1,'卢一','女','1993-01-01','2018','人工智能','001');
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (2,'卢二','男','1993-01-02','2019','软件','001');
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (3,'卢三','女','1993-01-03','2020','计算机技术','001');
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (4,'卢四','男','1993-01-04','2018','儿科','002');
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (5,'卢五','女','1993-01-05','2019','内科','002');
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (6,'卢六','男','1993-01-06','2020','外科','002');
    Insert image description here
  2. Insert courses table data
    INSERT INTO courses(cno,cname,period, credit) VALUES (001,'计算机系','4','3');
    INSERT INTO courses(cno,cname,period, credit) VALUES (002,'医学系','5','6');
    Insert image description here
  3. Insert sc table data
    INSERT INTO sc(sno, cno, grade) VALUES (1, 001, 90);
    INSERT INTO sc(sno, cno, grade) VALUES (2, 001, 80);
    INSERT INTO sc(sno, cno, grade) VALUES (3, 001, 100);
    INSERT INTO sc(sno, cno, grade) VALUES (4, 002, 90);
    INSERT INTO sc(sno, cno, grade) VALUES (5, 002, 95);
    INSERT INTO sc(sno, cno, grade) VALUES (6, 002, 89);
    Insert image description here

3 Query & Update (Experiment 3)

  1. Query operation
    Complete various query operations based on all query examples on pages P79-94 of the textbook.
  2. Update operation
    Complete various update operations (insert, modify and delete data, etc.) according to all update examples in the textbook P94-96. Please pay attention to the corresponding SQL in standard SQL and openGauss Similarities and differences between statements.

3.1 Query

3.1.1 General form of SELECT statement

select 
  [all|distinct]
  <目标列的表达式1> [别名],
  <目标列的表达式2> [别名]...
  from <表名或视图名> [别名],<表名或视图名> [别名]...
  [where<条件表达式>]
  [group by <列名> [having <条件表达式>]]
  [order by <列名> [asc|desc]];

Prerequisite: Create a table and insert data.
coursesTable:
Insert image description here
studentsTable:
Insert image description here
scTable:
Insert image description here

3.1.2 Simple query without WHERE

  1. Query all course information
    Insert image description here
  2. Query the age of students in 2023 (using functiondate_part('year',birthday))
    Insert image description here

3.1.3 Query with WHERE clause

  1. Comparison expression
    (1) The number of students whose scores are lower than 90 points
    Insert image description here
  2. BETWEEN expression
    (1) Number and major of students whose birth year is between 1997~2005
    (2) birth year is not 1997 Student number and major between ~2005
    Insert image description here
  3. IN expression
    (1) Number and name of a student majoring in artificial intelligence
    (1) Not a student majoring in artificial intelligence or pediatrics Number and Name
    Insert image description here
  4. LIKE expression
    (1) Course names starting with
    Insert image description here
  5. NULL expression
    (1) The number of the student whose course grade is empty and the number of the course
    Insert image description here

3.1.4 Sorting and grouping

  1. Sort the query results
    (1) Query the scores of each student's 001 course and sort them in descending order
    Insert image description here
    (2) Sort all scores in descending order< /span>
    Insert image description here
  2. Aggregation function
    (1) Query the number of tuples of course 001, that is, the number of people taking course 001
    Insert image description here
    (2) The minimum score, average, maximum
    Insert image description here
  3. Group
    (1) Query the average score of each student, output the student number and average score
    Insert image description here
    (2) Query the average score of each student Grades, output the student number and average grade of students whose average grade is >90
    Insert image description here

3.1.5 Connection query

  1. Course name and grades with course number 001
    Insert image description here
  2. The grades of each course (course name) taken by each student (number, name)
    Insert image description here
  3. Query the average score of each student and output the number, name, and average score of the students whose average score is greater than 90
    Insert image description here
  4. The names of other students with the same birth year as Lu Yi
    Insert image description here

3.1.6 Nested query

  1. Subquery induced by IN
    (1) Query the student number and name of the male classmate with the same dno as Lu Yi
    Insert image description here

  2. Subquery induced by comparison of sets
    (1) Student numbers, names, majors, and dates of birth in other majors that are smaller than all students whose dno is 001
    Insert image description here
    (2) (Course) The course number and average grade with the highest average grade
    Insert image description here

  3. There is a subquery induced by quantifiers
    (1) All student numbers and names who have taken course 001
    Insert image description here
    (2) Query all courses taken Students
    Insert image description here
    (3) Students who have taken at least all the courses chosen by Student 1
    Insert image description here

  4. Check for duplicate elements in subquery results

3.1.7 Set operations

(1) Students who have taken courses 001 or 002
Insert image description here
(2) Students who have taken courses 001 and 002
Insert image description here
(3) Students who have taken courses 001 Students who have not taken course 002
Insert image description here

3.2 Update

3.2.1 Insertion

  1. Query existing data to avoid insertion conflicts
    Insert image description here
  2. Insert data and query results
    Insert image description here

3.2.2 Update

  1. Update enrollment year for students 7 and 1
    Insert image description here

3.2.3 Delete

  1. Delete student 7’s information
    Insert image description here

4 views (Experiment 4)

  1. Create views using SQL statements.
  2. Perform operations such as creating, searching, and updating views, and compare the search and update operations with basic tables to see if there are any differences.

4.1 Define views

  1. Create a view
    (1) Create a student view se_students with dno 001, including all attributes in Students with dno 001 (except Speciality)
    Insert image description here
    (2) Create a view se_sc with dno 001, including all SC attributes of students with dno 001 in Students
    Insert image description here
    (3) Create a student grade view students_grades, including student number, student name, and course name and achievements. Insert image description here

  2. View specific information of the view
    (1) se_students view
    Insert image description here

4.2 View-based query

  1. Query the boys whose dno is 001 (the student view SE_Students with dno 001 has been defined and can be used directly.
    Insert image description here
  2. Query the scores of each subject of the student with student number 1, and it is required to display the student's name, course name and scores. (The query involves three tables Students, SC and Courses. It can be queried using the defined student grade view Students_Grades)
    Insert image description here

4.3 View-based update

openGauss does not support view updates

  1. ALTER viewname RENAME TO newname, change the view name se_students to ve_students
    Insert image description here

4.4 Delete a view

  1. See all views first
    Insert image description here

  2. Delete view se_sc and view all views again
    Insert image description here

5 Integrity Control (Experiment 5)

  1. Practice creating the following constraints:
      Primary key (PRIMARY KEY) constraint;
      Check (CHECK) constraint;
      Foreign key (FOREIGN KEY) constraints: Foreign key constraints are used to enforce referential integrity between tables. Cascading referential integrity constraints are designed to ensure the relevance of foreign key data.
      Uniqueness (UNIQUE) constraint: Set the SNAME column in the STUDENTS table as a unique key constraint, and set the CNAME column in the COURSES table as a unique key constraint;
  2. Create a trigger
     A trigger is a special type of stored procedure, usually used to enforce business rules and data integrity. Create an UPDATE trigger to check the business rules of the modification operation. For example, create a trigger on the STUDENTS table and give a prompt message when the user modifies a student's student number.

5.1 Create tables and primary key constraints, check constraints, and foreign key constraints

  1. First create the database and user, grant permissions to the user, and then create the table. The detailed process is as follows:
    Insert image description here
  2. The process of creating the table in the above figure also adds PRIMARY KEY constraints, CHECK constraints, and FOREIGN KEY constraints. View details through\d+ 表名, as shown in the figure below.
    Insert image description here
    Insert image description here

5.2 Add unique constraints

Set the SNAME column in the STUDENTS table as a unique key constraint and the CNAME column in the COURSES table as a unique key constraint;

  1. Create a unique constraint usingALTER TABLE students ADD UNIQUE(sname); and ALTER TABLE courses ADD UNIQUE(cname);.
  2. The changes after adding unique constraints to forstudents and courses are as shown below:
    Insert image description here

5.3 Create triggers

The general syntax is as follows:

CREATE [ CONSTRAINT ] TRIGGER trigger_name {
    
     BEFORE | AFTER | INSTEAD OF } {
    
     event [ OR ... ] }
    ON table_name
    [ FROM referenced_table_name ]
    {
    
     NOT DEFERRABLE | [ DEFERRABLE ] {
    
     INITIALLY IMMEDIATE | INITIALLY DEFERRED } }
    [ FOR [ EACH ] {
    
     ROW | STATEMENT } ]
    [ WHEN ( condition ) ]
    EXECUTE PROCEDURE function_name ( arguments );
# event语句
    INSERT
    UPDATE [ OF column_name [, ... ] ]
    DELETE
    TRUNCATE

5.3.1 Insertion

Create a trigger on the STUDENTS table and give a prompt when data is inserted.

  1. Create function
CREATE OR REPLACE FUNCTION insert_stu() RETURNS TRIGGER AS
$$
DECLARE
BEGIN
RAISE NOTICE'插入成功!';
RETURN NEW;
END
$$ LANGUAGE PLPGSQL;
  1. Create trigger
CREATE TRIGGER insert_trigger
AFTER INSERT ON students
FOR EACH ROW
EXECUTE PROCEDURE insert_stu();
  1. Test
    Since students has the foreign key departments(dno), you need to insert data for departments first, otherwise an error will be reported:
    INSERT INTO departments(dno,dname,dheadno) VALUES ('CS','信息系','05001'), ('MA','数学系','06001'), ('PH','物理系','07001'), ('CH','化学系','08001');
    After that, send data to students Insert data into:
INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (1,'卢一','女','1993-01-01','2018','人工智能','CS');

The specific process is as follows:
Insert image description here

5.3.2 Update

Create a trigger on the STUDENTS table to give a prompt message when the user modifies a student's student number.

  1. Create function
CREATE OR REPLACE FUNCTION update_stu() RETURNS TRIGGER AS
$$
DECLARE
BEGIN
RAISE NOTICE'更新成功!';
RETURN NEW;
END
$$ LANGUAGE PLPGSQL;
  1. Create trigger
CREATE TRIGGER update_trigger
AFTER UPDATE ON students
FOR EACH ROW
EXECUTE PROCEDURE update_stu();
  1. test
UPDATE students SET sno='01' WHERE sname='卢一';

The specific process is as follows:
Insert image description here

5.4 Others

5.4.1 Insert table data

  1. departments table
    INSERT INTO departments(dno,dname,dheadno) VALUES ('CS','信息系','05001'),('MA','数学系','06001'),('PH','物理系','07001'),('CH','化学系','08001');

  2. students table
    INSERT INTO students(sno,sname,sex, birthday, enrollyear, speciality, dno) VALUES (01,'卢一','女','1993-01-01','2018','人工智能','CS'),(02,'卢二','女','1993-01-01','2018','人工智能','CS'),(03,'卢三','男','1993-01-02','2019','软件','CS'),(04,'卢四','女','1993-01-03','2020','高数','MA'),(05,'卢五','男','1993-01-04','2018','数理统计','MA'),(06,'卢六','女','1993-01-05','2019','概率论','MA'),(07,'卢七','男','1993-01-06','2020','光学','PH'),(08,'卢八','男','1993-01-04','2018','电学','PH'),(09,'卢九','女','1993-01-05','2023','热学','PH'),(10,'卢十','男','1993-01-04','2018','燃料','CH'),(11,'十一','女','1993-01-05','2023','分子学','CH'),(12,'十二','男','1993-01-11','2018','气体学','PH');

  3. courses table
    INSERT INTO courses(cno,cname,period, credit) VALUES ('CS01','人工智能','4','3'),('CS02','软件','5','6'),('MA01','高数','4','6'),('MA02','数理统计','4','4'),('MA03','概率论','4','5'),('PH01','光学','4','4'),('PH02','电学','3','5'),('PH03','热学','3','6'),('CH01','燃料','4','4'),('CH02','分子学','4','6'),('CH03','气体学','3','5');

  4. sc table
    INSERT INTO sc(sno, cno, grade) VALUES (1, 'CS01', 90),(2, 'CS01', 80),(3, 'CS02', 56),(4, 'MA01', 76),(5, 'MA02', 95),(6, 'MA03', 89),(7, 'PH01', 79),(8, 'PH02', 53),(9, 'PH03', 100),(10, 'CH01', 65),(11, 'CH02', 95),(12, 'CH03', 74);

5.4.2 Modify table data

UPDATE students SET sno=1 WHERE sname='卢一';, because the foreign key sno of table sc refers to the primary key of table students, so the foreign key of SC needs to be deleted first.
Insert image description here

5.4.3 Delete foreign keys

Delete foreign keys throughALTER TABLE sc DROP CONSTRAINT sc_sno_fkey;. The detailed process is as follows:
Insert image description here

5.4.4 Adding foreign keys

  1. Before adding a foreign key to table sc, you should first change sno=01 of table sc to sno=1. Otherwise, the foreign key will fail to be added, so the foreign key sno of sc is the primary key sno of students, and the values ​​should remain consistent.
    Insert image description here

  2. EnterALTER TABLE sc ADD CONSTRAINT sc_sno_fkey FOREIGN KEY (sno) REFERENCES students(sno);Recreate the foreign key sno. The results are shown below:
    Insert image description here

6 Security Control (Experiment 6)

  1. Create some database users and understand the relationship between database users and roles.
  2. Use Query Analyzer to become familiar with security control of data through SQL (grant and revoke statements).

6.1 Database users & roles

6.1.1 User USER:

  • Users created through CREATE USER have LOGIN permissions by default;
  • When creating a user, a SCHEMA with the same name will be created for the user in the database where the command is executed;
  • In other databases, SCHEMA with the same name will not be automatically created; you can use the CREATE SCHEMA command to create SCHEMA with the same name in other databases for this user.
  1. Create userlll, the login password is ‘openGauss@1234, after the creation is completed, view the user list
    Insert image description here
  2. Grant userlllCreate role permissionCREATE ROLE
    Insert image description here

6.1.2 ROLE:

  • The entity that owns database objects and permissions. In different contexts a role can be considered a user, a group or both.
  • Add a new role in the database. The role does not have LOGIN permissions.
  • The user who creates the role must have CREATE ROLE permissions [ALTER USER 用户名 CREATEROLE;] or be a system administrator.
  1. Create a role with the namelllr and password openGauss@1234. After creation, view the role list.
    Insert image description here
  2. Modify the rolelllrBe the system administrator and view the role list again
    Insert image description here

6.2 GRANT&REVOKE

6.2.1 Grant the system permission SYSADMIN to USER/ROLE

  1. Authorize the system permission SYSADMIN to the userlll, and view the role list after authorization
    Insert image description here

6.2.2 Authorize database objects to USER/ROLE

  1. Create schemallls and create table under schemalllsstudents
    Insert image description here

  2. Grant the usage rights of schemallls and all rights of tablellls.students to the userlll
    Insert image description here

  3. Grant the query permission of column in tablellls.students and the update permission of to sno,sname,sex,dnosnolll
    Insert image description here

  4. Grant the connection permission of the databasepostgres to the userlll, and grant the userlll in< Create permissions in a i=4>, and allow users to grant this permission to other users () a>postgresschemalllWITH GRANT OPTION
    Insert image description here

  5. Grant access to schemallls to rolelllr and grant rolelllr on< Permission to create objects under a i=4> does not allow users in role to grant permissions to others. lllslllr
    Insert image description here

6.2.3 Grant USER/ROLE permissions to other USER/ROLEs

  1. Grant the permissions of the userlll to the rolelllr, and allow the rolelllr to grant permissions to others< /span>
    Insert image description here

  2. Create userhhh and grant rolelllr permissions to userhhh
    Insert image description here

6.2.4 Reclaim permissions and clean up users

  1. Recycle Rolelllr, Userlll, Userhhh, Modellls Permissions
    Insert image description here

  2. Delete rolelllr, Delete userlll, Delete userhhh, Delete modellls
    Insert image description here
    Check the modes and roles after deletion is complete:
    Insert image description here


Reference for subsequent experimentsCSDNBkbK-

7 Transaction Concurrency and Control (Experiment 7)

  1. Write a transaction instance, make it successfully submitted and rolled back, and observe the impact of the transaction on the database.
  2. Use the blocking mechanism provided by the database management system to resolve data inconsistencies caused by concurrent operations.

7.1 Transactions

7.1.1 ACID properties of transactions

  • Atomicity: An atomic transaction is a series of indivisible database operations. After the transaction completes, either all of these operations occur or none of them occur.
  • Consistency: After the transaction ends, the database is in a consistent state and data integrity is retained.
  • Isolation: Transactions cannot interfere with each other.
  • Durability: Even if crashes and failures occur, the effects of successfully completed (committed) transactions are persisted.

7.1.2 Transaction management

The transaction management commands supported by the openGauss database include start, set, commit, and rollback transactions.

  • Start transaction (START TRANSACTION | BEGIN)
START TRANSACTION
 [ 	{
   
   <!-- -->ISOLATION LEVEL 
	{
   
   <!-- -->READ COMMITTED | READ UNCOMMITTED  | SERIALIZABLE | REPEATABLE READ} | 
 	{
   
   <!-- -->READ WRITE | READ ONLY}} 
	[, ...] ];
  • SET TRANSACTION
{ SET [ LOCAL ] TRANSACTION | SET SESSION CHARACTERISTICS AS TRANSACTION }
{ ISOLATION LEVEL 
{
   
   <!-- -->READ COMMITTED | READ UNCOMMITTED  | SERIALIZABLE | REPEATABLE READ} | 
{
   
   <!-- -->READ WRITE | READ ONLY}} 
[, ...];
  • commit transaction
{ COMMIT | END } [ WORK | TRANSACTION ];
  • Rollback transaction
ROLLBACK [ WORK | TRANSACTION ];

7.1.3 Transaction instances

1. Uncommitted transactions
  • Start transaction
START TRANSACTION;
Insert image description here
  • Insert data
INSERT INTO Students 
VALUES ('201905001', 'BK','男', '2000-01-01','2019',' 计算机','CS');
Insert image description here
SELECT * FROM Students;
Insert image description here
  • Rollback transaction
ROLLBACK;
Insert image description here
SELECT * FROM Students;
Insert image description here
2. Submitted transactions
  • Start transaction
START TRANSACTION;
Insert image description here
  • Insert data
INSERT INTO Students 
VALUES ('201905001', 'BK','男', '2000-01-01','2019',' 计算机','CS');
Insert image description here
  • commit transaction
COMMIT TRANSACTION;
Insert image description here
SELECT * FROM Students;

Insert image description here

7.2 Concurrency control

7.2.1 Locking syntax

LOCK [ TABLE ] {
   
   <!-- -->[ ONLY ] name [, ...]| {name [ * ]} [, ...]}
    [ IN {ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE} MODE ]
    [ NOWAIT ];

7.2.2 Locking example

  • Create test table
CREATE TABLE TestLock(test CHAR(5));
Insert image description here
  • Open transaction
START TRANSACTION;
Insert image description here
  • Lock
LOCK TestLock;
Insert image description here
  • View all locks
SELECT  * FROM pg_locks ;
Insert image description here
  • commit transaction
COMMIT TRANSACTION;
Insert image description here
  • View all locks again
SELECT  * FROM pg_locks ;
Insert image description here

8 Database backup and recovery (Experiment 8)

1. Develop backup plans for specific failures and use these backup plans to restore the database.
2. Use various backup methods provided by openGauss for database backup.
3. Use the recovery mechanism provided by the specific database management system to restore the database using copies.

8.1 Physical backup and recovery

8.1.1 Experimental preparation

  1. Switch to the omm user and log in to the main database node as the operating system user omm.
su - omm
Insert image description here
  1. Create a folder to store backup files
mkdir -p /home/omm/physical/backup
Insert image description here

8.1.2 Physical backup

  1. If the database service is not started, start the database service (be sure to start the database service as the operating system user omm, if not, please switch the user).
gs_om -t start 
Insert image description here
  1. Make a physical backup of the database
gs_basebackup -D /home/omm/physical/backup -p 26000
Insert image description here
  1. Switch to the storage backup folder to view the backup files
cd /home/omm/physical/backup
ls
Insert image description here

8.1.3 Physical backup and recovery

  1. Stop openGauss ((Be sure to stop the database service as the operating system user omm, if not please switch users).
gs_om -t stop
Insert image description here
  1. Clean all or part of the files in the original database and destroy the database files.
cd /gaussdb/data/
cd db1
rm -rf *
Insert image description here
  1. Use the database system user rights to restore the required database files from the backup. In /gaussdb/data/db1, db1 is the name of the database node folder. Different databases may be different, please check and confirm.
cp -r /home/omm/physical/backup/.  /gaussdb/data/db1
  1. The backup takes about a few minutes. The file list after restoration is as follows:
cd /gaussdb/data/db1
ls
Insert image description here
  1. Restart the database server and check the database contents to ensure that the database has been restored to the required state
gs_om -t start
Insert image description here

8.2 Logical backup and recovery

8.2.1 Experimental preparation:

  • Switch to the omm user and log in to the main database node as the operating system user omm.
su - omm      
Insert image description here
  • Create a folder to store backup files
mkdir -p /home/omm/logical/backup
Insert image description here

8.2.2 gs_dump backup

1. gs_dump backup example 1
  • Execute gs_dump, and the exported MPPDB_backup.sql file format is plain text.
gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup.sql -p 26000 postgres -F p
Insert image description here
  • Switch to the backup folder and view the MPPDB_backup.sql file
ll /home/omm/logical/backup/
Insert image description here
cat /home/omm/logical/backup/MPPDB_backup.sql

Insert image description hereThe following content is displayed:

--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET xmloption = content;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: postgres; Type: COMMENT; Schema: -; Owner: omm
--

COMMENT ON DATABASE postgres IS 'default administrative connection database';


--
-- Name: tpcds; Type: SCHEMA; Schema: -; Owner: omm
--

CREATE SCHEMA tpcds;


ALTER SCHEMA tpcds OWNER TO omm;

SET search_path = tpcds;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: reason; Type: TABLE; Schema: tpcds; Owner: omm; Tablespace: 
--

CREATE TABLE reason (
    r_reason_sk integer NOT NULL,
    r_reason_id character(16) NOT NULL,
    r_reason_desc character varying(20)
)
WITH (orientation=row, compression=no);


ALTER TABLE tpcds.reason OWNER TO omm;

--
-- Data for Name: reason; Type: TABLE DATA; Schema: tpcds; Owner: omm
--

COPY reason (r_reason_sk, r_reason_id, r_reason_desc) FROM stdin;
\.
;

--
-- Name: public; Type: ACL; Schema: -; Owner: omm
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM omm;
GRANT CREATE,USAGE ON SCHEMA public TO omm;
GRANT USAGE ON SCHEMA public TO PUBLIC;


--
-- Name: tpcds; Type: ACL; Schema: -; Owner: omm
--

REVOKE ALL ON SCHEMA tpcds FROM PUBLIC;
REVOKE ALL ON SCHEMA tpcds FROM omm;
GRANT CREATE,USAGE ON SCHEMA tpcds TO omm;


--
-- Name: reason; Type: ACL; Schema: tpcds; Owner: omm
--

REVOKE ALL ON TABLE reason FROM PUBLIC;
REVOKE ALL ON TABLE reason FROM omm;
GRANT SELECT,INSERT,REFERENCES,DELETE,TRIGGER,TRUNCATE,UPDATE ON TABLE reason TO omm;


--
-- PostgreSQL database dump complete
--

2. gs_dump backup example 2
  • Execute gs_dump, and the format of the exported MPPDB_backup.tar file is tar format.
gs_dump -U omm -W Bigdata@123 -f  /home/omm/logical/backup/MPPDB_backup.tar -p 26000 postgres -F t	

Insert image description here
  • View generated file information
ll /home/omm/logical/backup/

Insert image description here
3. gs_dump backup example 3
  • Execute gs_dump, and the exported MPPDB_backup.dmp file format is a custom archive format.
gs_dump -U omm -W Bigdata@123 -f  /home/omm/logical/backup/MPPDB_backup.dmp -p 26000 postgres -F c
Insert image description here
  • View generated file information
ll /home/omm/logical/backup/
Insert image description here
4. gs_dump backup example 4
  • Execute gs_dump, and the exported MPPDB_backup file format is directory format.
gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/MPPDB_backup -p 26000  postgres -F d
Insert image description here
  • View generated file information
ll /home/omm/logical/backup/
Insert image description here
5.gs_dump backup example 5

Execute gs_dump to export the table (or view, or sequence, or appearance) object of the postgres database, such as table customer_t1

  • Execute gs_dump and export the table customer_t1
gs_dump -U omm -W Bigdata@123 -f /home/omm/logical/backup/bkp_shl2.sql -t public.customer_t1 -p 26000 postgres
Insert image description here
  • View generated file information
ll /home/omm/logical/backup/
Insert image description here
  • View the generated sql file
cat /home/omm/logical/backup/bkp_shl2.sql 

bkp_shl2.sqlis an empty file

8.2.3 gs_dumpall backup

  • Use gs_dumpall to export all databases of openGauss at once
gs_dumpall -f  /home/omm/logical/backup/bkp2.sql -p 26000
Insert image description here
  • View generated file information
ll /home/omm/logical/backup/
Insert image description here

8.2.4 gs_restore import

1. gs_restore import example 1
  • Execute gs_restore and import the exported MPPDB_backup.tar file (tar format) into the db_tpcc01 database
gs_restore /home/omm/logical/backup/MPPDB_backup.tar -p 26000 -d db_tpcc01
2.gs_restore import example 2
  • Execute gs_restore and import the exported MPPDB_backup.dmp file (custom archive format) into the db_tpcc02 database
gs_restore /home/omm/logical/backup/MPPDB_backup.dmp -p 26000 -d db_tpcc02
3.gs_restore import example 3
  • Execute gs_restore and import the exported MPPDB_backup file (directory format) into the db_tpcc03 database
gs_restore /home/omm/logical/backup/MPPDB_backup -p 26000 -d db_tpcc03
4.gs_restore import example 4

Execute gs_restore and use the MPPDB_backup.dmp file in the custom archive format to perform the following import operations. Only the definition of table customer_t1 in PUBLIC mode is imported.

  • Execute gs_restore and import only the definition of table customer_t1 in PUBLIC mode
gs_restore  /home/omm/logical/backup/MPPDB_backup.dmp -p 26000 -d db_tpcc04 -n public -t customer_t1

9 Use JDBC to connect to the database (Experiment 9)

  1. Create databases and tables in openGauss;
  2. Use jdbc to connect to the newly created database;
  3. Change the value in the database or output the value in the database in the java program;
  4. Please refer to the experiment manual provided by Huawei: openGauss scenario-based comprehensive application experiment.

9.1 Prepare the connection environment

9.1.1 Modify the pg_hba.conf file of the database

  • Look for the pg_hba.conf file in GS_HOME. In this experiment, the database GS_HOME is set to /gaussdb/data/db1. In actual operation, the GS_HOME address can be viewed in the installation configuration file:&lt;PARAM name="dataNode1" value="/gaussdb/data/db1"/&gt;.
cd /gaussdb/data/db1
vi pg_hba.conf
  • Enter ":90" to find the corresponding position, then enter "i" to switch to INSERT mode, add the following content to the pg_hba.conf file, press the "ECS" key after adding, exit INSERT mode, enter ":wq" and press Enter save.
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all    all    192.168.0.19/32    trust
host all all 0.0.0.0/0 sha256
# IPv6 local connections:
host    all             all             ::1/128                 trust
  • Log in as omm user and use gs_ctl to take effect of the policy.
su - omm
gs_ctl reload -D /gaussdb/data/db1/

9.1.2 Log in to the database and authorize exit

  • Use the omm user to log in to the database, authorize the dbuser user, and exit the database.
gsql -d postgres -p 26000 -r
alter role dbuser createrole createdb;
\q

9.1.3 Modify the database listening address

  • In GS_HOME, the database GS_HOME setting in this experiment is /gaussdb/data/db1
cd /gaussdb/data/db1
vi postgresql.conf
  • Enter ":60" to find the corresponding position, then enter "i" to switch to INSERT mode, change the value of listen_addresses to *, press the "ECS" key after modification, exit INSERT mode, enter ":wq" and press Enter to save.
#listen_addresses = '192.168.0.19'              # what IP address(es) to listen on;
listen_addresses = '*'
  • After the modification is completed, restart the database to take effect (the default path of the database after -D needs to be modified according to the actual situation)
gs_ctl restart -D /gaussdb/data/db1/

9.1.4 Download Java driver package import tool

  • Download the driver package for Java connection to openGauss and import it into the corresponding tool.

  • Download the driver package through the following link

  • Assume that the file is stored in the d:\Download directory and decompressed. The decompressed file is "postgresql.jar".

9.1.5 Create test database demo

  • Use the gsql tool to log in to the database and enter the dbuser password (eg: Gauss#3demo)
gsql -d postgres -p 26000 -U dbuser -r
  • Create database demo
create database demo ENCODING 'UTF8' template = template0;
  • Switch to the demo database and enter the dbuser password (eg: Gauss#3demo).
\connect demo;

9.1.6 Create schema

  • Create a schema named demo and set demo as the current schema.
CREATE SCHEMA demo;
  • Set the default search path to demo.
SET search_path TO demo;

9.1.7 Create test table websites

CREATE TABLE websites (
  id int NOT NULL,
  name char(20) NOT NULL DEFAULT '',
  url varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (id)
);
COMMENT ON COLUMN websites.name IS '站点名称';

9.1.8 Inserting data

INSERT INTO websites VALUES 
('1', 'openGauss', 'https://opengauss.org/zh/'), 
('2', '华为云', 'https://www.huaweicloud.com/'), 
('3', 'openEuler', 'https://openeuler.org/zh/'), 
('4', '华为support中心', 'https://support.huaweicloud.com/');

9.1.9 Exit the database

\q

9.2 Determine whether port 26000 is open

  • Open the Huawei Cloud homepage, log in and enter the "Console", click "Elastic Cloud Server ECS" to enter the ECS list
    Insert image description here
    Insert image description here

  • In the cloud server console, find the ECS where the database host is installed, click to view basic information, and find the security group
    Insert image description here
    Insert image description here

  • Click to enter the security group, select "Inbound Rules" and "Add Rule" to set port 26000Insert image description here
    Insert image description here

  • After confirmation, you can see that "TCP:26000" is added to the network access rules, as shown below:
    Insert image description here

9.3 Download and install JDK

  • Download JDK
    Insert image description here

  • Click jdk-8u261-windows-x64.exe to install. The default settings are enough, and the installation progress will appear.
    Insert image description here

  • The following display indicates successful installation:Insert image description here

  • View the installation directory
    Insert image description here

9.4 Configure JDK environment variables

  • Right-click "This PC", select "Properties", and click "Advanced system settings"
    Insert image description here
    Insert image description here

  • Click "Environment Variables", create a new system variable "JAVA_HOME", and enter the JDK installation directory
    Insert image description here
    Insert image description here
    C:\Program Files\Java\jdk1.8.0_261 is the JDK installation directory

  • Edit the system variable "path"
    Insert image description here
    Enter at the end of the variable value%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; (note whether there is a ; sign at the end of the original Path variable value, if not, Enter ; first and then enter the code above).

  • Create a new system variable "CLASSPATH" variable and enter "."
    Insert image description here

  • The system variables are configured. Query and verify whether the configuration is successful. Run cmd and enterjava -version (there is a space between java and -version). If the version information is displayed, the installation and configuration are successful

9.5 Connect to openGauss and execute java code

9.5.1 Use Java program to connect to the database and query

Step 1 Use a Java program to connect to the database and query (Note: Please replace the red text according to the actual situation and modify the elastic public IP information in jdbc:postgresql://Elastic Public IP:26000/demo, USER = "dbuser" The user and password to connect to the database PASS = "Gauss#3demo").

Create the openGaussDemo.java file in d:\Download\. The content of the file is as follows. Note that the red text should be replaced according to the actual situation:

import java.sql.*; 
public class openGaussDemo {
   
   <!-- -->
 
    static final String JDBC_DRIVER = "org.postgresql.Driver";  
    static final String DB_URL = "jdbc:postgresql://弹性公网IP:26000/demo?ApplicationName=app1";
      // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "dbuser";
    static final String PASS = "Gauss#3demo";
         public static void main(String[] args) {
   
   <!-- -->
        Connection conn = null;
        Statement stmt = null;
        try{
   
   <!-- -->
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
        
            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
        
            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM demo.websites";
            ResultSet rs = stmt.executeQuery(sql);
        
            // 展开结果集数据库
            while(rs.next()){
   
   <!-- -->
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
   
   <!-- -->
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
   
   <!-- -->
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
   
   <!-- -->
            // 关闭资源
            try{
   
   <!-- -->
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
   
   <!-- -->
                        }// 什么都不做
            try{
   
   <!-- -->
                if(conn!=null) conn.close();
            }catch(SQLException se){
   
   <!-- -->
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

9.5.2 Execution after compilation

On the machine where Java is installed, open cmd to compile the Java program and execute it. In cmd, enter the d:\Download\ directory and compile the Java program first (enter the directory of the Java program)

javac -encoding utf-8 -cp d:\Download\postgresql.jar openGaussDemo.java

java -cp .;D:/Download/postgresql.jar openGaussDemo

9.5.3 Execution results

连接数据库...
 实例化Statement对象...
ID: 1, 站点名称: openGauss, 站点 URL: https://opengauss.org/zh/
ID: 2, 站点名称: 华为云, 站点 URL: https://www.huaweicloud.com/
ID: 3, 站点名称: openEuler, 站点 URL: https://openeuler.org/zh/
ID: 4, 站点名称: 华为support中心, 站点 URL: https://support.huaweicloud.com/
Goodbye!

A Other operations

● View help information: postgres=# \?
● Switch database: postgres=# \c dbname
● List databases: postgres=# \l
● Exit database: < /span> a> a> ● Switch users: ● View all roles: ● View user attributes: ● View database user list: ● List indexes: ● List SCHEMA: ● Check the pg_tablespace system table, that is, all table spaces defined by the system and users: ● Query table space: ● View table structure: ● Query table attributes: ● List tables: ● List all tables, views and indexes: postgres=# \q
● Query the database list through the system table pg_database: postgres=# SELECT datname FROM pg_database;
postgres=# \d+
postgres=# \dt
postgres=# \d+ tablename
postgres=# \d tablename
postgres=# \db
postgres=# SELECT spcname FROM pg_tablespace;
postgres=# \dn
postgres=# \di
postgres=# SELECT * FROM pg_user;
postgres=# SELECT * FROM pg_authid;
postgres=# SELECT * FROM PG_ROLES;
postgres=# \c – username

● View all SQL statements supported by openGauss:postgres=#\h

Reference link

B. oracle、mysql、sql server、Navicat

  1. From the perspective of database type
    oracle, mysql and sql server are all relational databases.
    Navicat is a fast, reliable and affordable database management tool designed to simplify database management and reduce system management costs.

  2. From the perspective of application scenarios
    MySQL and SQL Server can be understood as small and medium-sized databases, while Oracle is a large database. (Relatively speaking)
    MySQL: basically supports all mainstream operating systems Windows, Linux, Unix
    SQL Server: Microsoft’s son, I’m sure It mainly supports Windows, but now it also supports Linux
    Oracle: Although it is supported by both Windows and Linux, it is better to use it on Linux in terms of performance.
    Navicat: It is a powerful MySQL database management and development tool. Navicat for MySQL is often used together.
    Due to its visual interface and numerous management tools, it greatly improves the efficiency of database development and operation and maintenance.

  3. In terms of cost
    mysql is open source, and everything else is free of charge.
    Because MySQL is open source software, the total cost of ownership can be greatly reduced.
    Linux is used as the operating system, Apache or Nginx is used as the web server, MySQL is used as the database, and PHP/Perl/Python is used as the server-side script interpreter.
    Since these four software are all free or open source software (FLOSS), you can build a stable and free website without spending a penny (except labor costs) using this method. The system is called "LAMP" or "LNMP" combination in the industry. Real fragrance combination

Guess you like

Origin blog.csdn.net/deer2019530/article/details/129260416