mysql use three

ER model relationship: It is
not recommended to establish a closed relationship between the table and the table

  1. If there is a delete and modify operation in the relationship generated by the multi-table constraint, the corresponding associated table data will be reported as an error
    .
  2. First build the table in the constraint:
    alter table scores add constraint stu foreign key (stuid) referrnces stu (id) on delete cascade
    cascade: cascade relationship
    restrict: restrict relationship
    set null: foreign key blank
    no action

Premise: Create a table
Create two foreign keys in the score table, and associate it with the student table and the
subject table create table subjects (
id int auto_increment primary key not null,
title varchar (10) not null
);
insert into subjects values ​​(0, ' Language '), (0,' Math '), (0,' English '), (0,' Science ');
create table stu (
id int auto_increment primary key,
name varchar (10) not null,
birthday datetime,
gender bit default 0,
isdelete bit default 0,
adress varchar (100),
score int
);

(1, "小 明", "2018-01-01", 0,0, "Beijing", 90),
(2, "Little Red", "2007-01-01", 1,0, "Shanghai" 80),
(3, "Xiaolan", "2006-01-01", 1,0, "Guangzhou", 100),
(4, "Xiaowang", "2005-01-01", 0,0, "Shenzhen", 20),
(5, "Lao Wang", "2009-01-01", 0, 0, null, 30),
(6, "Lao Liu", "2004-01-01", 0, 0, null, 40),
(7, "Xiao Li", "2003-01-01", 1,0, "Dongguan", 50),
(8, "Xiao Fang", "2002-01-01", 1,0, "Fujian", 60),
(9, "Little Li", "2001-01-01", 0,0, "Fuzhou", 70);

create table scores (
id int auto_increment primary key,
stuid int,
subid int,
score decimal (5,2),
foreign key (stuid) references stu (id),
foreign key (subid) references subjects (id)
);
// decimal (5,2): used to store accurate values, you can store 5-digit
insert into scores values ​​(0,1,1,80), (0,2,2,60), (0,2 , 3,70), (0,3,1,90), (0,4,4,60), (0,5,2,75);

Link query:

  1. Inner join
    query the student's name, the corresponding score of the subject
  2. left join: A left link B table: query results are based on A
  3. right join: A right link B table: The query results are based on B.
    Select *
    from scores
    right join stu on stu.id = scores.subid;

    find the average score of

    everyone : find the average score of each subject:

    students highest score with id <
    5select stu.name, subjects.title, Max (scores.score)
    from scores
    inner join stu on scores.stuid = stu.id
    inner join subjects on scores.subid = subjects.id
    where stu.id < 5
    group by stu.name;


View view: Encapsulation of complex SQL statements

  1. Create: create view view name as sql query statement
  2. View the result of the view: select * from view name
  3. Delete view: drop view view name

Affairs:

  1. The four characteristics of transactions: ACID
    Atomic atomicity : Treat transactions as a program, either succeed or fail together. The operation of a database is inseparable. Only if all operations are successful, the entire transaction is submitted to
    consistency consistency: The state of the database is consistent with his business rules, that is, the data will not be destroyed. For example, if the A account transfers 100 yuan to the B account, regardless of whether the operation is successful or not, the total deposit in the A and B accounts is unchanged.
    Isolation isolation: transaction and transaction are isolated
    durability durability: Once the transaction is successfully submitted, all data operations in the transaction must be persisted to the database. Even after the transaction is committed, the database crashes immediately. When the database is restarted, it must be guaranteed that the data can be recovered through some mechanism.

  2. Table requirement type: innodb / ddb
    view table creation statement: show create table table name

  3. Start transaction: begin

  4. Submit a transaction: commit

  5. Transaction rollback: rollback

  6. Opening a transaction is equivalent to performing a series of operations in the buffer, which will be completed after submission

Open another terminal, before the transaction is not committed, the added data cannot be queried

Query efficiency:

  1. Index query: the most efficient
  2. Improve efficiency: the smaller the data type, the better.
    It is
    best to use integer types. It is best not to use NULL, use 0, or special symbols instead
  3. Create index: create index indexname on tablename (key (length))
  4. View all indexes: show index from tablename
  5. Delete index: drop index indexname on tablename
  6. Delete when the index is used up
  7. Verification time: set profiling = 1;
  8. Display time after query: show profiles;

Backup and recovery of mysql
Backup: mysqldump -u root -p db_name table_name> absolute path of backup file
Recovery: mysql -u root -p db_name> absolute path of backup file

Recovery: You must first create an empty table in the database, and the table name is the same as the table name in the backup file

Guess you like

Origin www.cnblogs.com/wuweixiong/p/12747664.html