Advanced operations of mysql database (query)

(1) There are 3 correspondences between entities, and these relationships also need to be stored. Relational databases mainly store relationships.
(2) During development, some stored data needs to be processed, and some built-in functions are used
( 3) The view is used to complete the encapsulation of the query statement
(4) The transaction can ensure that the complex add, delete, modify and check operations are effective ---- in the operation to make the mysql data effective
Relationship introduction:
relationships are very important in the database, multiple data there is a relationship between tables, table relationships can correspond in some properties, so you can use this relationship to the data query
attribute student table: id name
attribute score table: id score
chart properties : Id title
score table has id and score, each score will correspond to multiple names, each score will also correspond to multiple subjects, so it is a one-to-many relationship, so you need to establish a foreign key to connect to other tables, score table Need to establish a foreign key to connect the student table and grade table

Create a transcript of the statement is as follows:
the Create the Table Scores (
the above mentioned id int Primary Key AUTO_INCREMENT,
stuid int,
subid int,
Score decimal (5, 2)
)
foreign key
how relational data to ensure the validity of it: for data by foreign key constraints The validity of the verification, add foreign key constraints for stuid, subid.
create table scores (
id int primary key auto_increment,
stuid int,
subid int,
score decimal (5, 2)
foreign key (stuid) references students (id)
foreign key (subid) references subjects (id)
);

The cascading operation of foreign keys
(1) When deleting the data of the students table, if this id value already exists in scores, an exception will be thrown
(2) You can specify the cascading operation when creating the table, or you can create the table after The cascading operation to modify foreign keys
Syntax: alter table scores add constraint stu_sco foreign key (stuid) The
foreign key operation is mainly to associate multiple data tables.

Put all the tables together to query and display the results in the same result set :
When the query results come from multiple tables, you need to use join query, the key: to find the relationship between the tables, the current relationship is:
students table id ------ stuid of the scores table id of the
subject table ---- subid of the scores table

eg: select  students.name,subjects.title,scores.score  from  scores  inner join  students  on scores.stuid=students.id  inner  join  subjects on scores.subid=subjects.id;

Conclusion: When you need to query multiple related tables, you need to use join

Join query:
join query classified as follows:
Table A inner join Table B: Table A and Table B matching behavior can occur in the results
in Table A left join Table B: Table A and Table B matches the behavior appear in the results, plus Table A The unique data in the data, the uncorresponding data is filled with null
Table A right join Table B: The behavior of matching table A and table B will appear in the result, plus the unique data in table B, the uncorresponding data is filled with null
It is recommended to use the "table name. Column name" syntax in the query or condition.
If the column names in multiple tables are not repeated, the "table name" part can be omitted.
If the table name is too long, you can use the abbreviation of "as" after the table name Name, a temporary short name for the table

Built-in functions :
(1) String functions: operate on strings in data
(2) Mathematical functions:
– absolute value abs (n): select abs (-32)
– find the remainder of m divided by n mod (m, n), the same operator%: select mod (10, 3); select 10% 3–
– address floor (n), representing the largest integer not greater than n
– ceiling ceiling (n), representing the largest integer not less than n
– Find the rounded value round (n, d), n represents the original, d represents the decimal position, the default is 0
-find x to the power of y pow (x, y)-get the
PI (select PI), select rand ()

(3) Date function
Used to query the date
select data_format ('2016-12-21', '% Y% m% d')
current date: current_date () select current_date ();
current time: current_time () select current_time () ;
Current date and time: select now ();

Views: A encapsulation of the SQL statement, the result is to present
the meaning of the view in the form of a table : for complex queries, after multiple uses, maintenance is a very troublesome thing, defining the essence of the view is a encapsulation of the query- --- The results of the query are presented in a new table
Define the view: create view view name as (query code)
eg: create view view name as select students. *, Scores.score from scores inner join students on scores.stuid = students.id
------ After the view is created, you need to query-the query method is the same as the ordinary table:
select * from view name

Transactions in mysql : When a business logic requires multiple sql to complete, if one of the sql statements is wrong, you want to withdraw the entire operation, use the transaction to complete the function of returning, to ensure the correctness of the business logic, you
need to remember: Four characteristics (ACID)
atomicity (Atomicity): all operations in the transaction are inseparable in the database, either all or none of the
consistency (Consistency): several transactions executed in parallel, the execution result must be Consistent with the results of serial execution in a certain order
Isolation: Transaction execution is not interfered by other transactions, and the intermediate results of transaction execution must be transparent to other transactions
. Durability: For any committed transaction, The system must ensure that the transaction does not lose the changes to the database, even if the database fails.
Transaction support: the type of the table is innodb or bdb

Transaction statement:
open: begin is enabled by default.
Submit: commit requires manual operation.
Withdraw: rollback can be withdrawn when no sql statement is required.
This is often used in python interaction.

Query summary:
Relational storage: Use foreign key-> establish a connection (multiple tables), you can use a connection query when querying
Connection query: used for queries between multiple sheets
Built-in function
view: used for SQL statements Encapsulate operations
Transaction: Four characteristics, keep the correctness of business logic

Published 129 original articles · Like 43 · Visits 100,000+

Guess you like

Origin blog.csdn.net/nbxuwentao/article/details/104435498