Relational database foundation and application (1)

Database Principles

Introduction to Databases

1. DBs and DBMSs

Database (DB) is a warehouse that organizes, stores and manages data according to the data structure ;

Database management system (Database Managerment System, DBMS for short): software for managing databases ;

2. Introduction to relational databases

Relationship: Describes the association or correspondence between two elements

Use the relational model to organize data into two-dimensional data tables (Table)

Productization: Oracle , DB2, Sybase, SQL Server, MySQL

3. The concept of table

A relational database consists of multiple data tables (tables), which are the basic storage structure of relational databases;

Tables are two - dimensional, consisting of rows and columns;

The row (Row) of the table is horizontal data, also known as the record (Record);

The column of the table is the column data, also known as the field (Field);

There is an association relationship between tables;

Mainstream relational database

1, Oracle database overview

Oracle is the database product of the famous Oracle (Oracle) company;

Oracle is the world 's first commercial relational database management system;

· Oracle adopts standard SQL (structured query statement), supports multiple data types, provides object-oriented data support, and has fourth-generation language development tools; supports UNIX, WINDOWS, OS/2 and other platforms;

2, DB2 database overview

DB2 is IBM's relational database management system ;

There are many different versions of DB2, which can run on different terminal machines from handheld products to mainframes;

DB2 is Oracle 's main competitor;

3, Sybase database overview

· Sybase is a relational database system of Sybase Corporation in the United States;

· Sybase is an early database vendor using C/S technology;

Large database systems in a client/server environment on a typical UNIX or Windows NT platform ;

4, SQL Server database overview

· Microsoft SQL Server is a product of Microsoft and runs on Windows NT servers;

5, MySQL database overview

MySQL is an open source small relational database management system, widely used in small and medium websites;

Low total cost of ownership and smaller scale than Oracle and DB2

· On January 16, 2008, Sun acquired MySQL; on April 20, 2009, Sun was acquired by Oracle, so now MySQL belongs to Oracle;

SQL overview

structured query

· SQL (Structured Query Language): Structured query statement

SQL is the standard language used to perform data manipulation, retrieval and maintenance on relational databases , and can be used to query data, manipulate data, define data, and control data;

All databases use the same or similar language

· SQL can be divided into:

 - Data Definition Language (DDL): Data Definition Language;

 - Data Manipulation Language (DML): Data Manipulation Language;

 - Transaction Control Language (TCL): Transaction Control Language;

 - Data Query Statement (DQL): Data Query Language;

 - Data Control Language (DCL): Data Control Language;

Data Definition Language (DDL): Data Definition Language

· Used to create, modify, delete data objects;

· Include:

  - CREATE: create a table or other object structure;

  - ALTER: modify the structure of a table or other object;

  - DROP: drop a table or other object structure;

  - TRUNCATE: delete table data, keep table structure;

Data Manipulation Language (DML): Data Manipulation Language
· Used to change the data in the data table;

· Related to the transaction, after the execution is completed, the transaction control statement is submitted before the changes are actually applied to the database;

· Include:

 - INSERT: insert data into the database;
 - UPDATE: update the existing data in the data table;

 - DELETE: delete the data in the data table;

Transaction Control Language (TCL): Transaction Control Language

· Statements used to maintain data consistency

· Include:

  - COMMIT: Commit to confirm the data changes that have been made;

  - ROLLBACK: rollback, cancel the data changes that have been made;

  - SAVEPOINT: save point, so that the current transaction can be rolled back to the specified save point, which is convenient to cancel some changes;

Data Query Statement (DQL): Data Query Language

· Used to query the required data;

SELECT statement ;

Data Control Language (DCL): Data Control Language

· Use for granting and reclaiming permissions for execution ;

· Include:

  - GRANT: Grant, used to grant permissions to users or roles;

  - REVOKE: used to reclaim the existing permissions of a user or role;

  - CREATE USER: create a user;

SQL(DDL、DML)

Oracle Numeric Type

NUMBER(number)

· NUMBER represents the numeric type;

· Often defined in the form NUMBER(P,S), where:

  - P represents the total number of digits;

  - S indicates the number of digits after the decimal point;

For example , the definition of the sal column in the table emp is as follows: sal NUMBER(6,2)

  - Represents the data in the sal column, the maximum number of integer digits is 4, and the maximum number of decimal digits is 2, that is, the maximum value: 9999.99;

CHAR(char)

Represents a fixed - length character type;

Often defined as CHAR(N), where N represents the number of bytes occupied ;

The maximum length is 2000 bytes

For example , the ename column in the emp table is defined as follows: ename CHAR(20)

  - Indicates that a string of up to 20 bytes can be stored in the ename column, and the space occupied is a fixed 20 bytes;

VARVHAR2 (varchar2): Oracle-specific

· Represents a variable-length string;

· Define the format VARCHAR(N), N represents the maximum number of bytes that can be occupied;

The maximum length is 4000 bytes;

· For example, the job column in table emp is defined as follows: job VARCHAR(100);

  - Indicates that a string with a maximum length of 100 bytes can be stored in the job column. According to the length of the data stored in it, the occupied space varies, and the maximum occupied space is 100 bytes;

·  VARCHAR is currently a synonym for VARCHAR2. The industry standard VARCHAR type can store empty strings, but oracle does not, although it reserves the right to do so in the future. Oracle has developed a data type VARCHAR2, which is not a standard VARCHAR. It changes the feature that varchar columns in the database can store empty strings to store NULL values. If you want backward compatibility, Oracle recommends using VARCHAR2 instead of VARCHAR.

DATE(date)

The most commonly used date type in ORACLE to save date and time;

The date range represented by DATE can be from January 1, 4712 BC to December 31, 9999 AD;

The storage of the DATE type in the database is fixed to 7 bytes, and the format is:

  - 1st byte: century + 100

  - 2nd byte: year

  - 3rd byte: month

  - 4th byte: day

  - 5th byte: hour +1

  - 6th byte: minutes + 1

  - 7th byte: seconds + 1

Database Definition Statement (DDL)

Create a table: CREATE statement
CREATE TABLE employee(
    id NUMBER(4),
    name VARCHAR2(50),
    birth DATE
);
DESC statement: view table structure

DEFAULT statement


NOT NULL statement

Not Null is a constraint that ensures that the field value is not null ;

· By default, any column is allowed to have null values;

· When a field is set with a non-null constraint, there must be a valid value in this field;

· When performing the operation of inserting data, the data of this column must be provided;

· When performing an update operation, the value of this column cannot be set to NULL;


Modify table name

· If you want to modify the table name after creating the table, you can use the RENAME statement to achieve it;

The syntax is as follows, which will change the table name old_name to new_name:

  - RENAME old_name TO new_name;

add column

Adding columns to a table can be achieved using the ADD clause of ALTER TABLE ;

· Example:

--Add a column hiredate to the table and set the default value to the current date
ALTER TABLE myemp ADD(hiredate DATE DEFAULT sysdate);

Columns can only be added to the end and cannot be inserted into an existing column

delete column

Use the DROP clause of ALTER TABLE to drop unneeded columns ;

· Example:

-- delete the column hiredate of the table myemp
ALTER TABLE myemp DROP hiredate;

Deleting a field needs to delete the length and data occupied by the field from each row, and release the space occupied in the data block. If the table record is relatively large, it may take a long time to delete the field ;

Modify column

After the table is created, the data type, length and default value of the columns in the table can be changed;

Modifications are only valid for data inserted later ;

· If the length is changed from large to small, it may be unsuccessful;

· Example:

--Modify the column obj of the table myemp, and increase the setting of the default value
ALTER TABLE myemp MODIFY(job VARCHAR2(40) DEFAULT 'CLERK');

Simple DML statement

INSERT (insert) statement

· Add records to the data table

· Example:

INSERT INTO emp(id,name,job,salary)VALUES(100,'rose','programmer',5500);

After the DML operation is performed, the commint statement needs to be executed before the operation is truly confirmed;

· If the inserted column has a date field, the format of the date needs to be considered; the default date format is 'DD-MON-RR';

· You can customize the date format and convert it to date type data with the TO_DATE function ;

--Insert records using default date format
INSERT INTO emp(id,birth)VALUES(1002,'01-SEP-03');
--Insert records with custom date format
INSERT INTO emp(id,birth)VALUES(1003,TO_DATE('1999-01-01','YYYY-MM-DD'));
UPDATE statement

· Update records in the table

· Example:

--Change the salary of staff ROSE to 8500
UPDATE emp SET salary = 8500 where name='ROSE';
· When updating or querying, it is best to query conditionally, otherwise the full table query will be very slow in the face of massive data;
DELETE statement

· delete records in the table;

· Example:

--Delete records of employees whose job title is empty
DELETE FROM emp WHERE job is null;

· If there is no where, the entire table data will be deleted;

· The TRUNCATE (truncate) statement in the DDL statement also has the effect of deleting table data;

· Difference between TRUNCATE and DELETE:

  - DELETE can delete conditionally, and TRUNCATE deletes all table data;

  - DELETE is a DML statement that can be rolled back, and TRUNCATE is a DDL statement that takes effect immediately and cannot be rolled back;

  - If all table records are deleted and the data is large, the efficiency of the DELETE statement is lower than that of the TRUNCATE statement;

· Example:

-- delete all records
DELETE FROM emp;
--or
TRUNCATE TABLE emp;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898095&siteId=291194637