Databases (concepts, syntax, DBMS, SQL language: create databases, tables, add, modify, delete data records)
Relational database: A data structure with tables as entities and primary key and foreign key relationships as connections.
Primary key: In a relational database, each row is marked with a unique identifier, which is the primary key. The primary key has two characteristics: non-null and non-repeatable.
Foreign keys: In relational databases, foreign keys are used to express the relationship and connection between tables, and establish the connection between entities through primary and foreign key relationships.
Three basic relationship models between tables:
① One-to-many relationship: One master table record corresponds to multiple slave table records, and one slave table record corresponds to one master table record.
② One-to-one relationship: A master table record corresponds to a slave table record, and a slave table record corresponds to a master table record.
③ Many-to-many relationship: One master table record corresponds to multiple slave table records, and one slave table record corresponds to multiple master table records.
Relational Database Management System (DBMS):
A relational database is just a container that holds data, and most databases rely on a software called a database management system ( DBMS ) to manage the data in the database.
The current popular relational database server management systems are:
① Microsoft MS SQL Server
② Oracle's Oracle
③ IBM's DB2
④ MySQL, PostgreSQL for open source databases
SQL:
SQL is Structured Query Language, a computer language used to manage relational databases and communicate with the data in the database.
SQL statement:
① Data Definition Language (DDL): used to create, modify and delete data structures within the database.
--create database db CREATE DATABASE db; --delete database db DROP DATABASE db; --Create table t_student CREATE TABLE t_student( -- PRIMARY KEY marks the column as the primary key column -- AUTO_INCREMENT sets the automatic growth column, the value of this column is allocated by the DBMS and does not need to be maintained by the developerid INT PRIMARY KEY AUTO_INCREMENT, --Number studentName VARCHAR( 20),-- name, 20 means name 20 characters sex ENUM( 'male','female'),-- sex, enum means enumerationbirthday DATE, --birthday tel VARCHAR( 11) -- tel );
② Data Query Language (DQL): used for querying data in the database
--Query all genders DISTINCT means remove duplicate columns SELECT DISTINCT sex FROM t_student; -- Query all the data in the table, * means all the columns in the query table SELECT * FROM t_student; --Query the data starting from the first record and display 3 records SELECT * FROM t_student LIMIT 0,3;
③ Data Manipulation Language (DML): used to modify data in the database, including inserting, updating and deleting data
-- add record INSERT INTO t_student(studentName,sex,birthday,tel) VALUES( 'Zhang San','Male','1983-09-30','13987879898' ); -- delete the record with id 17 DELETE FROM t_student WHERE id=17; --Modify Li Si's phone number UPDATE t_student SET tel='13966666666' WHERE studentName='李四';
④ Data Control Language (DCL): Control database access rights
Foreign key constraints:
Foreign key: A column in the slave table is dependent on a column in the primary key.
Foreign key constraint: refers to the mandatory addition of a constraint on the primary key associated with the foreign key. If the constraint is violated, the modification of the piece of data is not allowed.
Note: No foreign key constraints are not established without foreign keys.
Primary key constraints:
The primary key is one or more columns in the table. The primary key column cannot be empty or repeated. There can only be one primary key in a table.
Aggregate function:
Statistical analysis of a set of data is achieved using aggregate functions. Common aggregation functions are as follows:
COUNT: Count the number of rows.
SUM: Get the sum measure of a single column.
AVG: Calculates the average of a column.
MAX: Calculates the maximum value of a column.
MIN: Calculates the minimum value of a column.
Execution order of SQL statements:
Step 1: Execute FROM
Step 2: where condition filter
The third step: GROUP BY grouping
Step 4: Execute select projection column
Step 5: HAVING condition filtering
Step 6: Execute the ORDER BY sorting statement, the default is asc ascending, and DESC is descending.
Join:
The difference between inner join and outer join:
Inner join: Only records with related data in both tables can be queried;
Outer join: You can query all records in a table, regardless of whether the record has associated data.
CREATE TABLE t_man( id INT PRIMARY KEY AUTO_INCREMENT, manName VARCHAR(20), birthday DATE ); INSERT INTO t_man(manName,birthday) VALUES( 'Zhang San','1980-02-03'),('Li Si','1994-01-05' ), ( 'Wang Wu','1991-07-30'),('Zhao Liu','1995-11-18' ); SELECT * FROM t_man; CREATE TABLE t_bike( id INT PRIMARY KEY AUTO_INCREMENT, bikeType VARCHAR(20), money INT, manId INT ); --Add foreign key constraints, so that the primary key and the foreign key correspond one by one to prevent the generation of garbage data ALTER TABLE t_bike ADD CONSTRAINT fk_mb FOREIGN KEY (manId) REFERENCES t_man(id); INSERT INTO t_bike(bikeType,money,manId) VALUES( 'Phoenix',400,1),('Permanent',500,1),('Fire Unicorn',250,1 ), ( 'Universiade',1000,2),('Xia Li',600,2),('Giant',1200,3 ), ( 'Mobike',200,3),('BMW',2000,3),('Mercedes',600,3 ); --Query all bicycles and display the name of the owner of the bicycle SELECT b.*,m.manName FROM t_bike b JOIN t_man m ON m.id=b.manId; --Query all bicycles of Li Si (inner join: display the data related to the two tables) SELECT b.* ,m.manName FROM t_bike b JOIN t_man m ON m.id=b.manId WHERE m.manName='李四'; SELECT b.* ,m.manName FROM t_man m JOIN t_bike b ON m.id=b.manId WHERE m.manName='李四'; SELECT b.* FROM t_bike b,t_man m WHERE b.manId=m.id AND m.manName='李四'; --Display the bicycle information of all users, the outer join cannot be used, add the form of where to write -- outer join: RIGHT right outer join, including all the information in the right table, left left outer join, including all the information in the left table . SELECT m.*,b.bikeType,b.money FROM t_bike b RIGHT JOIN t_man m ON b.manId=m.id; --Display the number of all user bicycles --Left outer join SELECT m.*,COUNT(bikeType) num FROM t_man m LEFT JOIN t_bike b ON m.id=b.manId GROUP BY m.id; -- 右外连接 SELECT m.*, COUNT(bikeType) number FROM t_bike b RIGHT JOIN t_man m ON b.manId=m.id GROUP BY m.id;
Relational database: A data structure with tables as entities and primary key and foreign key relationships as connections.
Primary key: In a relational database, each row is marked with a unique identifier, which is the primary key. The primary key has two characteristics: non-null and non-repeatable.
Foreign keys: In relational databases, foreign keys are used to express the relationship and connection between tables, and establish the connection between entities through primary and foreign key relationships.
Three basic relationship models between tables:
① One-to-many relationship: One master table record corresponds to multiple slave table records, and one slave table record corresponds to one master table record.
② One-to-one relationship: A master table record corresponds to a slave table record, and a slave table record corresponds to a master table record.
③ Many-to-many relationship: One master table record corresponds to multiple slave table records, and one slave table record corresponds to multiple master table records.
Relational Database Management System (DBMS):
A relational database is just a container that holds data, and most databases rely on a software called a database management system ( DBMS ) to manage the data in the database.
The current popular relational database server management systems are:
① Microsoft MS SQL Server
② Oracle's Oracle
③ IBM's DB2
④ MySQL, PostgreSQL for open source databases
SQL:
SQL is Structured Query Language, a computer language used to manage relational databases and communicate with the data in the database.
SQL statement:
① Data Definition Language (DDL): used to create, modify and delete data structures within the database.
--create database db CREATE DATABASE db; --delete database db DROP DATABASE db; --Create table t_student CREATE TABLE t_student( -- PRIMARY KEY marks the column as the primary key column -- AUTO_INCREMENT sets the automatic growth column, the value of this column is allocated by the DBMS and does not need to be maintained by the developerid INT PRIMARY KEY AUTO_INCREMENT, --Number studentName VARCHAR( 20),-- name, 20 means name 20 characters sex ENUM( 'male','female'),-- sex, enum means enumerationbirthday DATE, --birthday tel VARCHAR( 11) -- tel );
② Data Query Language (DQL): used for querying data in the database
--Query all genders DISTINCT means remove duplicate columns SELECT DISTINCT sex FROM t_student; -- Query all the data in the table, * means all the columns in the query table SELECT * FROM t_student; --Query the data starting from the first record and display 3 records SELECT * FROM t_student LIMIT 0,3;
③ Data Manipulation Language (DML): used to modify data in the database, including inserting, updating and deleting data
-- add record INSERT INTO t_student(studentName,sex,birthday,tel) VALUES( 'Zhang San','Male','1983-09-30','13987879898' ); -- delete the record with id 17 DELETE FROM t_student WHERE id=17; --Modify Li Si's phone number UPDATE t_student SET tel='13966666666' WHERE studentName='李四';
④ Data Control Language (DCL): Control database access rights
Foreign key constraints:
Foreign key: A column in the slave table is dependent on a column in the primary key.
Foreign key constraint: refers to the mandatory addition of a constraint on the primary key associated with the foreign key. If the constraint is violated, the modification of the piece of data is not allowed.
Note: No foreign key constraints are not established without foreign keys.
Primary key constraints:
The primary key is one or more columns in the table. The primary key column cannot be empty or repeated. There can only be one primary key in a table.
Aggregate function:
Statistical analysis of a set of data is achieved using aggregate functions. Common aggregation functions are as follows:
COUNT: Count the number of rows.
SUM: Get the sum measure of a single column.
AVG: Calculates the average of a column.
MAX: Calculates the maximum value of a column.
MIN: Calculates the minimum value of a column.
Execution order of SQL statements:
Step 1: Execute FROM
Step 2: where condition filter
The third step: GROUP BY grouping
Step 4: Execute select projection column
Step 5: HAVING condition filtering
Step 6: Execute the ORDER BY sorting statement, the default is asc ascending, and DESC is descending.
Join:
The difference between inner join and outer join:
Inner join: Only records with related data in both tables can be queried;
Outer join: You can query all records in a table, regardless of whether the record has associated data.
CREATE TABLE t_man( id INT PRIMARY KEY AUTO_INCREMENT, manName VARCHAR(20), birthday DATE ); INSERT INTO t_man(manName,birthday) VALUES( 'Zhang San','1980-02-03'),('Li Si','1994-01-05' ), ( 'Wang Wu','1991-07-30'),('Zhao Liu','1995-11-18' ); SELECT * FROM t_man; CREATE TABLE t_bike( id INT PRIMARY KEY AUTO_INCREMENT, bikeType VARCHAR(20), money INT, manId INT ); --Add foreign key constraints, so that the primary key and the foreign key correspond one by one to prevent the generation of garbage data ALTER TABLE t_bike ADD CONSTRAINT fk_mb FOREIGN KEY (manId) REFERENCES t_man(id); INSERT INTO t_bike(bikeType,money,manId) VALUES( 'Phoenix',400,1),('Permanent',500,1),('Fire Unicorn',250,1 ), ( 'Universiade',1000,2),('Xia Li',600,2),('Giant',1200,3 ), ( 'Mobike',200,3),('BMW',2000,3),('Mercedes',600,3 ); --Query all bicycles and display the name of the owner of the bicycle SELECT b.*,m.manName FROM t_bike b JOIN t_man m ON m.id=b.manId; --Query all bicycles of Li Si (inner join: display the data related to the two tables) SELECT b.* ,m.manName FROM t_bike b JOIN t_man m ON m.id=b.manId WHERE m.manName='李四'; SELECT b.* ,m.manName FROM t_man m JOIN t_bike b ON m.id=b.manId WHERE m.manName='李四'; SELECT b.* FROM t_bike b,t_man m WHERE b.manId=m.id AND m.manName='李四'; --Display the bicycle information of all users, the outer join cannot be used, add the form of where to write -- outer join: RIGHT right outer join, including all the information in the right table, left left outer join, including all the information in the left table . SELECT m.*,b.bikeType,b.money FROM t_bike b RIGHT JOIN t_man m ON b.manId=m.id; --Display the number of all user bicycles --Left outer join SELECT m.*,COUNT(bikeType) num FROM t_man m LEFT JOIN t_bike b ON m.id=b.manId GROUP BY m.id; -- 右外连接 SELECT m.*, COUNT(bikeType) number FROM t_bike b RIGHT JOIN t_man m ON b.manId=m.id GROUP BY m.id;