当前位置:网站首页>Tutorial on principles and applications of database system (007) -- related concepts of database

Tutorial on principles and applications of database system (007) -- related concepts of database

2022-07-07 12:15:00 Rsda DBA_ WGX

Database system principle and Application Tutorial (007)—— Database related concepts

One 、 data (Data)

1、 The concept of data

data (Data) It's a symbolic record of things . The data has the following characteristics :

(1) Data is the reflection of the attributes of things . such as : Student Zhang San is male , be “ male ” It means the gender attribute of student Zhang San .

(2) Data is affected by data type ( Numerical type , String type , Date type and logical type ) And value range constraints . Below emp Table data e_id( Employee number ) The data type is integer , Only integers can be entered when entering data ,birth( Date of birth ) The data type of is date , Only one legal date can be entered .

mysql> desc emp;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| e_id   | int(11)      | NO   | PRI | NULL    |       |
| name   | char(20)     | YES  |     | NULL    |       |
| gender | char(2)      | YES  |     | NULL    |       |
| birth  | date         | YES  |     | NULL    |       |
| height | decimal(5,2) | YES  |     | NULL    |       |
| phone  | char(11)     | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

--  data  e_id  Type error 
mysql> insert into emp values('abc',' zhang ',' male ','8888-88-88',78.5,'13782594456');
ERROR 1366 (HY000): Incorrect integer value: 'abc' for column 'e_id' at row 1

--  data  birth  Type error 
mysql> insert into emp values(1111,' zhang ',' male ','8888-88-88',78.5,'13782594456');
ERROR 1292 (22007): Incorrect date value: '8888-88-88' for column 'birth' at row 1

(3) It has carrier and manifestation . Data must have a certain physical carrier , such as MySQL The data in the database needs to be saved on the hard disk of the computer .

2、 Data management

Data management includes data storage 、 Data maintenance 、 Data query and statistics .

(1) Organize and save data

Relational database saves data in the form of tables on the hard disk of the computer . The following command is in mydb Create a stu surface , And input data .

/* create table stu( s_id char(11) primary key, name char(20), gender char(2), birth date, height decimal(5,2), phone char(11) ); */
mysql> create table stu(
    ->     s_id char(11) primary key,
    ->     name char(20),
    ->     gender char(2),
    ->     birth date,
    ->     height decimal(5,2),
    ->     phone char(11)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into stu values('20210224001',' Zhang Yun ',' Woman ','2002-2-23',56,'13603735899');
Query OK, 1 row affected (0.00 sec)

mysql> insert into stu values('20210224002',' Liu tao ',' male ','2003-1-3',60,'13737345866');
Query OK, 1 row affected (0.00 sec)

mysql> select * from stu;
+-------------+--------+--------+------------+--------+-------------+
| s_id        | name   | gender | birth      | height | phone       |
+-------------+--------+--------+------------+--------+-------------+
| 20210224001 |  Zhang Yun    |  Woman      | 2002-02-23 |  56.00 | 13603735899 |
| 20210224002 |  Liu tao    |  male      | 2003-01-03 |  60.00 | 13737345866 |
+-------------+--------+--------+------------+--------+-------------+
2 rows in set (0.00 sec)

(2) Data maintenance

Data maintenance mainly includes data addition 、 Delete 、 Change operation . Adding refers to adding rows to the table , Delete refers to deleting rows in the table , Change refers to modifying the value of data in the table .

--  Add a row of data , Use  insert  command 
mysql> insert into stu values('20210224003',' Wang Yangming ',' male ','2003-11-10',62,'13837345333');
Query OK, 1 row affected (0.01 sec)

--  Delete a row of data , Use  delete from  command 
mysql> delete from stu where s_id='20210224001';
Query OK, 1 row affected (0.02 sec)

--  Change Zhang Yun's weight to  50  kg 
mysql> update stu set height=50 where s_id='20210224002';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--  View results 
mysql> select * from stu;
+-------------+-----------+--------+------------+--------+-------------+
| s_id        | name      | gender | birth      | height | phone       |
+-------------+-----------+--------+------------+--------+-------------+
| 20210224002 |  Liu tao       |  male      | 2003-01-03 |  50.00 | 13737345866 |
| 20210224003 |  Wang Yangming     |  male      | 2003-11-10 |  62.00 | 13837345333 |
+-------------+-----------+--------+------------+--------+-------------+
2 rows in set (0.00 sec)

(3) Data query and statistics

--  Check the date of birth of Wang Yangming 
mysql> select birth from stu where s_id='20210224003';
+------------+
| birth      |
+------------+
| 2003-11-10 |
+------------+
1 row in set (0.00 sec)

--  Count the number of boys 
mysql> select count(*) from stu where gender=' male ';
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

Two 、 database (DataBase,DB)

Database is a long-term storage in the computer 、 Organized 、 Can share 、 Data collection under unified management . Database is a computer software system that stores and manages data according to data structure .

(1) A database is an entity , It is a warehouse that can keep data reasonably , The user stores the transaction data to be managed in the warehouse , The two concepts of data and database are combined to form a database .

(2) Database is a new method and technology of data management , He can organize data more reasonably 、 More convenient maintenance data 、 Tighter control of data and more efficient use of data .

for example , The personnel department of an enterprise or institution often needs to give the basic information of its employees ( Employee number 、 full name 、 Age 、 Gender 、 Native place 、 Wages 、 Resume, etc ) Store in a table , This table can be seen as a database . With this database, we can query the basic information of an employee at any time according to our needs , You can also query the number of employees whose wages are within a certain range, etc . Of course , A database can contain multiple tables .

--  Open database  mydb
mysql> use mydb
Database changed

--  View tables in the database 
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| emp            |
| stu            |
+----------------+
2 rows in set (0.00 sec)

3、 ... and 、 Database management system (DataBase Management System,DBMS)

Database management system is a layer of data management software between user application and operating system , Is the core component of the database system , Manage and maintain the database . Be able to provide the definition of data 、 maintain 、 Query, statistics and other operations, as well as data security 、 Integrity control function . Popular database management systems are Oracle、MySQL、DB2,SQL Server etc. .DBMS The working principle of is shown in the figure below .

 Insert picture description here

(1) Receive data requests from applications and process requests ;
(2) Send the user's data request ( High-level language / Instructions ) Into complex machine code ( The underlying instructions );
(3) Realize the operation of database ( The underlying instructions );
(4) Accept query results from operations on the database ;
(5) Process the query results ( format conversion );
(6) Return the processing results to the application .

DBMS The functions are as follows :

1、 Define database

All operations related to database results belong to the definition database . Including creating new databases and tables 、 modify the database 、 Modify the table structure and other operations .DBMS Provide corresponding data definition language (DDL) To define the database structure .

--  Create database 
mysql> create database employee;
Query OK, 1 row affected (0.00 sec)

--  Open database 
mysql> use employee;
Database changed

/* create table dept( d_id int primary key, name char(20) ); create table emp( e_id int primary key, name char(20), gender char(20), birth date, phone char(11) ); */

--  Create table :dept
mysql> create table dept(
    ->     d_id int primary key,
    ->     name char(20)
    -> );
Query OK, 0 rows affected (0.03 sec)

--  Create table :emp
mysql> create table emp(
    ->     e_id int primary key,
    ->     name char(20),
    ->     gender char(20),
    ->     birth date,
    ->     phone char(11)
    -> );
Query OK, 0 rows affected (0.01 sec)

2、 Manipulating databases

Manipulating the database refers to the operation of the data in the table , Including the increase of data 、 Delete 、 Change 、 check .DBMS Provide data manipulation language (DML), Realize the basic access operation of database data : Insert 、 modify 、 Delete and retrieve .

--  increase : add rows 
mysql> insert into dept values(11,' Finance Department '),(12,' The sales department '),(13,' Production department ');

mysql> insert into emp values(1101,' Zhang Yunfei ',' male ','1998-12-2','13782556699');
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(1102,' Liu Mingming ',' male ','1989-10-12','13682553322');
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(1201,' Wang Jingjing ',' Woman ','1990-11-8','13782554488');
Query OK, 1 row affected (0.01 sec)

--  View the data 
mysql> select * from dept;
+------+-----------+
| d_id | name      |
+------+-----------+
|   11 |  Finance Department     |
|   12 |  The sales department     |
|   13 |  Production department     |
+------+-----------+
3 rows in set (0.00 sec)

mysql> select * from emp;
+------+-----------+--------+------------+-------------+
| e_id | name      | gender | birth      | phone       |
+------+-----------+--------+------------+-------------+
| 1101 |  Zhang Yunfei     |  male      | 1998-12-02 | 13782556699 |
| 1102 |  Liu Mingming     |  male      | 1989-10-12 | 13682553322 |
| 1201 |  Wang Jingjing     |  Woman      | 1990-11-08 | 13782554488 |
+------+-----------+--------+------------+-------------+
3 rows in set (0.00 sec)

--  Delete row 
mysql> delete from dept where d_id=13;
Query OK, 1 row affected (0.00 sec)

--  View the data 
mysql> select * from dept;
+------+-----------+
| d_id | name      |
+------+-----------+
|   11 |  Finance Department     |
|   12 |  The sales department     |
+------+-----------+
2 rows in set (0.00 sec)

--  Modifying data 
mysql> update emp set phone='10086' where e_id=1201;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--  View the data 
mysql> select * from emp;
+------+-----------+--------+------------+-------------+
| e_id | name      | gender | birth      | phone       |
+------+-----------+--------+------------+-------------+
| 1101 |  Zhang Yunfei     |  male      | 1998-12-02 | 13782556699 |
| 1102 |  Liu Mingming     |  male      | 1989-10-12 | 13682553322 |
| 1201 |  Wang Jingjing     |  Woman      | 1990-11-08 | 10086       |
+------+-----------+--------+------------+-------------+
3 rows in set (0.00 sec)

3、 Control function

DBMS Provide data control function , That is, data security control 、 Integrity control and concurrency control effectively control and manage the database operation , To ensure that the data is correct and valid .

(1) Security control

Security control sets different access permissions for different users , To ensure that the data is not used by illegal users . The data security control is to ensure the data security and reliability of the database , Prevent data leakage and damage caused by illegal use , That is to avoid data being peeked 、 Tamper with or destroy .MySQL adopt grant( to grant authorization ) and revoke( Recycling permissions ) To realize the granting and recycling of user rights .

(2) Integrity control

Data integrity control is to ensure that the data in the database is correct 、 Effective and compatible , To prevent incorrect data that does not conform to semantics from being input or output . The integrity of relational database includes entity integrity 、 Referential integrity and user-defined integrity .

--  Entity integrity : The primary key cannot be null 
--  Definition  d_id  Primary key , The value of this column cannot be empty when inserting and modifying data , Nor can duplicate values be taken 
/* create table dept( d_id int primary key, name char(20) ); */
mysql> create table dept(
    ->     d_id int primary key,  
    ->     name char(20)
    -> );
Query OK, 0 rows affected (0.01 sec)

-- d_id  Can't be empty 
mysql> insert into dept values(null,' Management Department ');
ERROR 1048 (23000): Column 'd_id' cannot be null

mysql> insert into dept values(1,' Management Department ');
Query OK, 1 row affected (0.00 sec)

-- d_id  Can't repeat 
mysql> insert into dept values(1,' Department of Economics ');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

--  Referential integrity : The value of the foreign key must be taken from the primary key of the corresponding parent table 
--  Definition  d_id  For foreign key , And watch  dept  Column in  d_id  Corresponding 
/* create table stu( s_id char(11) primary key, s_name char(20), gender char(2), d_id int, foreign key(d_id) references dept(d_id) ); */

mysql> create table stu(
    ->     s_id char(11) primary key,
    ->     s_name char(20),
    ->     gender char(2),
    ->     d_id int,
    ->     foreign key(d_id) references dept(d_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

--  Insert data into the sub table 
mysql> select * from dept;
+------+-----------+
| d_id | name      |
+------+-----------+
|    1 |  Management Department     |
+------+-----------+
1 row in set (0.00 sec)

--  Foreign keys  d_id  You can take a null value  
mysql> insert into stu values('20210224001',' Wang Peng ',' male ',null);
Query OK, 1 row affected (0.01 sec)

--  Foreign keys  d_id  by  1, Insert the success ( Exists in parent table  d_id  by  1  The record of )
mysql> insert into stu values('20210224002',' Liu Ming ',' male ',1);
Query OK, 1 row affected (0.01 sec)

--  Foreign keys  d_id  by  2, Insert the failure ( The parent table does not exist  d_id  by  2  The record of )
mysql> insert into stu values('20210224003',' Zhang Jing ',' Woman ',2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constra
int fails (`aaa`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`d_id`) REFERENCES `dept` (`d_id`))

--  User-defined integrity : Constraints for a specific relational database , User defined rules .
--  for example : Students' grades range from  0~100  Between .

(3) concurrency control

Concurrency control is a mechanism to ensure timely correction of errors caused by concurrent operations . Concurrency control refers to when multiple users update data at the same time , Various technologies for protecting database integrity . The purpose of concurrency control is to ensure that the work of one user will not have an unreasonable impact on the work of another user . The basic unit of concurrency control is transaction .

4、 Fault recovery —— Data backup and recovery

Backup is crucial for databases . When the data file is damaged 、MySQL Service error 、 System kernel crash 、 When the computer hardware is damaged or the data is deleted by mistake , Use an effective data backup scheme , You can quickly solve all the above problems .

MySQL Provides a variety of backup solutions , Include : Logical backup 、 The physical backup 、 Full backup and incremental backup .

(1) Physical backup is realized by directly copying directories and files containing database contents , This backup method is suitable for the backup of important large-scale data , And it requires a production environment with fast restore . A typical physical backup is replication MySQL Some or all directories of the database , Physical backup can also back up related configuration files . Adopting physical backup requires MySQL In the closed state or lock the database , Prevent sending data from changing during backup . Physical backup can be used mysqlbackup Yes InnoDB Data backup , Use mysqlhotcopy Yes MyISAM Data backup .

(2) Logical backup is realized by storing the description information representing the database structure and data content , For example, save and create data structures and add data content SQL sentence , This backup method is applicable to the backup and restore of a small amount of data . The advantage of logical backup lies in the service level 、 Database level or data table level backup can be realized , This backup and system 、 Hardware has nothing to do with it .

(3) A full backup will back up all the data at a certain time , Incremental backup only backs up data that has changed in a certain period of time . A full backup can be done with a physical or logical backup tool , Incremental backup needs to be enabled MySQL Binary log , Log changes in data , To achieve incremental differential backup .

5、 The data transfer

DBMS Provide the transmission of processing data , Implement user program and DBMS Communication between .

Four 、 Database system (DataBase System,DBS)

A database system consists of a database 、 Database management system 、 Database application system 、 Database administrators and users constitute . As shown in the figure below :

 Insert picture description here

5、 ... and 、 Three level schema structure of database

The three-level schema structure of the database includes external schema 、 Conceptual patterns and internal patterns . The three-level schema structure of the database is effectively organized 、 Management data , It improves the logical independence and physical independence of the database .

The user level corresponds to the external mode , The concept level corresponds to the concept pattern , The physical level corresponds to the internal mode . Users at different levels form different views of the database . So called view , It means observing 、 Recognize and understand the scope of data 、 Angle and method , It is the reflection of the database in the eyes of users . Different levels ( Level ) The database that users see is different .

1、 External mode

External mode (External Schema) It's also called sub pattern (Subschema) Or user mode (User Schema), Corresponding to user level . It is the data view of the database seen by one or more users , Is a logical representation of data related to an application . An external schema is a subset derived from the schema , Contains data that a particular user is allowed to use in the schema . Users can manipulate language through data (Data Manipulation Language,DML) Operate on these data records . The external schema reflects the user view of the database system .

The sub mode is completely in accordance with the user's own needs for data 、 Design from a local perspective . Because a database system has multiple users , So there may be multiple sub patterns . Because the sub pattern is user oriented or programming oriented , So it is called user data view .

Logically , A sub pattern is a subset of patterns , Many different sub patterns can be derived from one pattern . The database based on the sub schema is the user database . Sub patterns have the following advantages :

(1) Use sub mode , Users do not have to consider data that is not related to subsets , There is no need to understand the storage structure of data , The work of user using data and program design have been simplified .

(2) Users can only operate on the data required by the subset , Other data in the database is isolated from users , It is conducive to the security and confidentiality of data .

2、 Conceptual model

Conceptual pattern is also called pattern (Schema) Or logical mode (Logical Schema), Corresponding to concept level . It is the database designer who synthesizes the data of all users , A global logical structure constructed from a unified point of view , It is a general description of the logical structure and characteristics of all data in the database , Is a common data view for all users ( Global view ). It is a data schema description language provided by the database management system (Data Description Language,DDL) To describe 、 Defined . Conceptual schema reflects the holistic view of database system .

Logical mode is the system to reduce data redundancy 、 Achieve the goal of data sharing and comprehensively abstract the data of all users to obtain a unified global logical view . A database can only have one logical schema , The database based on logical schema is the conceptual database .

3、 Internal mode

Internal mode (Internal Schema) Also called storage mode (Access Schema) Or physical mode (Physical Schema), Corresponding to physical level . It is the internal representation or underlying description of all data in the database , Is the lowest level logical description of the database , It describes the storage mode and physical structure of data on the storage medium , Corresponding to the database actually stored on the external storage medium . Internal schema reflects the storage view of database system .

The design goal of physical mode is to integrate the mode of the system ( Logical mode ) Organize into the best physical mode , To improve the efficiency of data access , Improve the performance index of the system . The database based on physical mode is physical database . In the database system , Only physical models really exist , It is the actual data file really stored in external storage . The concept database and user database do not exist in the external storage of the computer .

User database 、 The relationship between conceptual database and physical database is :
(1) Conceptual database is the logical abstract form of physical database ;
(2) Physical database is the concrete implementation of conceptual database ;
(3) User database is a subset of conceptual database , It is also a logical description of a subset of physical databases .

6、 ... and 、 Secondary image technology of database

In order to achieve transparency between users and data , The database management system provides a secondary image :(1) External mode / Pattern image ;(2) Pattern / Internal mode image . The secondary image ensures that the data in the database system can have high logical independence and physical independence .

The tertiary mode and secondary image of the database are shown in the following figure .

 Insert picture description here

1、 External mode / Pattern image

Every external schema of the database has an external schema / Pattern image , It defines the corresponding relationship between external patterns and patterns , External mode / Schema images are generally described in external schemas .

When the mode changes ,DBA You can change the external mode by modifying the image . Because the application is designed according to the external pattern , As long as the external mode does not change , The application does not need to be modified . External mode / Schema mapping technology not only establishes the corresponding relationship between user database and logical database , At the same time, it also ensures the logical independence of data .

2、 Pattern / Internal mode image

Pattern / Internal mode image is unique , Because the database always has only one schema and one internal schema , It exists between patterns and inner patterns . Pattern / The inner schema image defines the corresponding relationship between logical schema and inner schema .

When the storage structure of the database changes ,DBA You can modify the mode / The mapping between internal schemas keeps the data schema unchanged . Because users or programs use data according to the logical mode of data , So as long as the data pattern remains the same , Users can still use the data the same way , The program does not need to be modified .

Pattern / Internal mode mapping technology not only enables users or programs to use data according to the logical structure of data , It also provides a method to change the internal mode and keep the program unchanged , This ensures the physical independence of the data .

原网站

版权声明
本文为[Rsda DBA_ WGX]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071009028359.html

随机推荐