mysql full solution


Introduction https://www.cnblogs.com/snake23/p/9635822.html engine

primarykey primary key KEY PERIMARY
Foreign Key foreign key KEY a FOREIGN
References connecting the REFERENCES
Auto INCREMENT automatically increase AUTO_INCREMENTY
uniqueKey unique constraint UNIQUE KEY

No negative unsigned (unsigned) twice int UNSIGNED
general -127- {int> 128} becomes plus UNSIGNED INT [0-255]

default default value DEFAULT


create table abc(
id int(10) NOT NULL AOTO_INCREMENT PRIMARY KEY,
username VARCHAR(10) NOT NULL UNIQUE KEY,
password VARCHAR(10) NOT NULL UNIQUE KEY,
sex TINYINT(5) NOT NULL DEFAULT '0',
);

After a successful -------- "

CREATE TABLE `abc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
`sex` tinyint(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `password` (`password`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 

INSERT INTO abc(id,username,password,sex) values(1,'aaa','123',0);

SELECT * FROM abc ;
SELECT id,username,password,sex FORM abc;

UPDATE abc set username='ccc' WHERE id=1;

DELETE FROM abc WHERE id=1;

DROP TABLE abc;


// add a single
ALTER TABLE ABC ADD age int (10 ) NOT NULL // add columns
// add multiple columns
ALTER TABLE ABC ADD (hobby varchar ( 10) NOT NULL, kp varchar (10) NOT NULL) // add columns


// add the default constraint
ALTER TABLE ABC ADD age TINYINT DEFAULT 0 ;

// Default setting modification constraint
ALTER TABLE ABC alter age set default 15 ;

// delete the default constraint
ALTER TABLE ABC alter age DROP default;

// Delete the primary key constraint
the ALTER TABLE DROP PRIMARY KEY ABC;
// delete the only constraint
the ALTER TABLE DROP UNIQUE ABC;
// delete the foreign key constraint
ALTER TABLE ABC DROP FOREIGN KEY;

 


// modify the definition of the field
ALTER TABLE ABC MODIFY id int (5 ) NOT NULL SET DEFAULT 1;

// change the name of the field
ALTER TABLE ABC CHANGE id pid int ( 5);

 

The NOT EXISTS IF
- query did 102 elective courses students and students name
the SELECT SNO, SNAME from Stu not the WHERE EXISTS (
the SELECT * from the WHERE SCORE (Stu.SNO = SNO and CNO = '102')
)

- Elective a query students of all courses student ID and name of
the SELECT SNO, SNAME from Stu not the WHERE EXISTS (
the SELECT * from C not the WHERE EXISTS (
the SELECT * from the WHERE SNO SCORE = Stu.SNO and C.CNO = CNO)
)

 


mysql database, if and only if the engine is InnoDB, only supported [affairs];
// set the mysql isolation level: in the form of: set session transaction isolation level transaction isolation level set

Check the level statements show variables like 'transaction_isolation'; repeatable-read (default)


// read uncommitted level setting:
SET Transaction Isolation Level read uncommitted the session;
data not yet read resolution (dirty reads, phantom read, non-repeatability)

// set the read committed level:
the SET the session Transaction Isolation Level read committed;
read data has been submitted solutions (dirty read)

// Set repeatable read Level:
SET Transaction Isolation Level repeatable read the session;
repeatable read solution (dirty read unrepeatable degrees)

// Set Level serializable:
SET the session Isolation Level Transaction serializable;
serialized data solution of (dirty reads, phantom read, non-repeatability)

 

 


MySQL can be broadly divided into the following three kinds [lock]:

Table-level lock: Small overhead, lock fast; not deadlock; lock large size, the probability of lock conflicts of the highest and lowest degree of concurrency.

Row-level locking: large overhead, locking slow; there will be a deadlock; locking the smallest size, lowest probability of lock conflicts, have the highest degree of concurrency.

Page lock: cost and locked in a time bound between the table and row locks; there will be a deadlock; locking granularity boundary between the table and row locks, concurrency general
[lock]
MyISAM executing query (SELECT) before, will be automatically added to all table read locks involved in the update operation (uPDATE, DELETE, INSERT, etc.) before,
will be automatically added to the table write locks involved, this process does not require user intervention, so that users generally do not need lOCK tABLE command directly to the explicit lock MyISAM table
[lock]

MyISAM table to explicit lock, typically to a certain extent simulate transaction operation, reading of a consistency point in time a plurality of tables.

[For example], there is an order table orders, which recorded total order amount total, as well as an order list order_detail, which recorded the amount of the order subtotal subtotal of each product, if we need to check these two tables total amount for equality, you may need to perform the following two SQL:

1
SELECT SUM(total) FROM orders;
2
SELECT SUM(subtotal) FROM order_detail;

At this time, if you do not give these two tables locked, it may produce incorrect results, because the process of execution of the first statement, order_detail table may already have changed. Therefore, the correct approach should be:

1
LOCK tables orders read local,order_detail read local;
2
SELECT SUM(total) FROM orders;
3
SELECT SUM(subtotal) FROM order_detail;
4
Unlock tables;

Guess you like

Origin www.cnblogs.com/KFKang/p/12037581.html