MySQL ---- enhance knowledge points in java programming

Source: Internet
Author: User

MySQL ---- enhance knowledge points in java programming

Table creation is a common and practical method in data.

The relationship between a table and a table is.

1: N

N: N

Three methods.

1-to-1 Mode

 

Example: A pair of wife thinks that everyone's information is stored in a table. If there are male and female, query all couples, that is, one male and one female. Note the following: Wang Wu is not supposed to be able to query the bachelors.
Create table person (id INT, name varchar (10), sex CHAR (1), wife INT, husband INT); insert into person VALUES (1, 'flate ', '0',); insert into person VALUES (2, 'yufen', '0',); insert into person VALUES (3, 'zhang san', '1 ', 1, 0); insert into person VALUES (4, 'Li si', '1',); insert into person VALUES (5, 'wang wu', '1 ); // one-to-one operation: find out the name of each couple. create view w as select * FROM person WHERE sex = '0 '; create view m as select * FROM person WHERE sex = '1'; // do not use the relationship between the table and SELECT w. name as wife, m. name as husband FROM w, m WHERE w. husband = m. id AND m. wife = w. id; // the more advanced method: SELECT w using the relationship between tables. name as wife, m. name as husband FROM w inner join m ON w. husband = m. id AND m. wife = w. id;


 

1Many-to-many Method

 

1-to-many, multiple-to-1. A person can own multiple cars and asks to query all the cars owned by him. According to the requirements of the paradigm, two tables should be designed to represent human information and vehicle information respectively.
// Step 1: Draw a E-R graph // Step 2: CREATE a solid surface and add the foreign KEY constraint create table person2 (id VARCHAR (32) primary key, pname VARCHAR (30), sex CHAR (1); create table car (id VARCHAR (32) primary key, cname VARCHAR (30), price NUMERIC (10, 2 ), pid VARCHAR (32), CONSTRAINT car_fk foreign key (pid) REFERENCES person2 (id); drop table car; // Step 3: add test data to two tables // insert into person2 (id, pname, sex) VALUES ('p001', 'jack', '1') for entity table 1 '); insert into person2 (id, pname, sex) VALUES ('p002 ', 'Tom', '1'); insert into person2 (id, pname, sex) VALUES ('p003 ', 'Rose', '0'); insert into person2 (id, pname, sex) VALUES ('p004 ', 'Mary', '0 '); insert into person2 (id, pname, sex) VALUES ('p005 ', 'Mike', '1'); SELECT * FROM person2; /// entity Table 2 insert into car (id, cname, price, pid) VALUES ('c001', 'bmw ', 123.5, 'p001 '); insert into car (id, cname, price, pid) VALUES ('c002', 'benz', 123.5, 'p001'); insert into car (id, cname, price, pid) VALUES ('c003 ', 'bmw', 223.5, 'p001'); insert into car (id, cname, price, pid) VALUES ('c011 ', 'bmw ', 83.5, 'p003'); insert into car (id, cname, price, pid) VALUES ('c012', 'benz', 100, 'p003 '); insert into car (id, cname, price, pid) VALUES ('c013', 'audi ', 223.5, 'p003'); insert into car (id, cname, price, pid) VALUES ('c021', 'bmw ', 88.5, 'p004'); insert into car (id, cname, price, pid) VALUES ('c022 ', 'qq', 10, 'p004 '); insert into car (id, cname, price, pid) VALUES ('c023', 'audi', 73, 'p005 '); insert into car (id, cname, price) VALUES ('c033', 'audi ', 1000 );
/Query: who has what kind of car (with "table name. if the column name is not repeated, You can omit the table name) // associate SELECT person2.pname, car by using the primary key of one party with the foreign key of multiple parties. cname FROM person2, car WHERE person2.id = car. pid; // query Jack's car SELECT person2.pname, car. cname FROM person2, car WHERE person2.id = car. pid AND person2.pname = 'jack'; // query which vehicles have more than two vehicles. SELECT person2.pname, COUNT (pname) AS number of vehicles FROM person2, car WHERE person2.id = car. pid group by pname having count (pname)> = 2 order by vehicle quantity; SELECT * FROM person2 WHERE id IN (SELECT pid FROM car group by pid having count (pid)> = 2); // query who has no car SELECT * FROM person2 WHERE id not in (SELECT pid FROM car );
Many-to-many

 

 

Many-to-many: a person can have multiple roles, such as a father, a son, and a husband. At the same time, these three roles can be assigned to all others. It is required to find out who is Father, Son, and husband. Query all roles owned by all users.
There are many-to-many student course selection systems.
The specific implementation method is posted in the last comparison note

 

 

 

Database constraint primary key
Specify a primary key when creating a table. When declaring a primary key, use the not null keyword at the same time.
Create table stud (id int primary key ,.....)
After creation, specify the primary key:
Alter table stud add constraint stud_pk primary key (id );
Automatic growth of primary keys:
Auto_increment is applicable to fields of the int type.
Foreign key-indicates a reference relationship
The association relationship with another table is called a foreign key. When a foreign key acts as a foreign key, when there is no record in the primary table, no reference record can be written to the subtable:
Alter table stud add constraint stud_fk foreign key (stud_clsid) references cls (id );
A primary-foreign key relationship is a one-to-many relationship. If no record exists in the primary table, the sub-table cannot be added.
Create a primary-foreign key Association. The data types and sizes of the two tables must be consistent. Otherwise, the creation will not succeed.
You can operate on external and external associations on the visual interface. Unique-different from primary keyThe primary key cannot be null, while the unique column can be null. This is the limitation of SqlServer, but Mysql can write the null value of multiple columns. Therefore, when unique is used in mysql, it is generally restricted by not null.
Alter table cls add constraint cls_uk unique (name);/* The specified name cannot be repeated */
Default Value: default
When creating a table, you can specify the default value, for example:
Create table stud (sex char (1) default '1 ',.....);

Use Association to query dataWhen the data in a table cannot meet our needs, we need to query data from multiple tables. In this case, you must use the join query:
Inner join-Internal join, both parties must exist.
Left join-left join. Take the data on the left as the standard.
Right join-right join. The relationship between the above table and the table is used

Summary:
Stored Procedure

 

Three methods:

1. the simplest and most basic method.

 

"DELIMITER $" is used to make the parser treat "$" as the end sign (otherwise, ";" is used as the end mark by default ), in this way, the statement Terminator ";" in the stored procedure is not used as the end mark of the process.
DELIMITER $ create procedure p1 () begin select * FROM stud; insert into stud (id, sname, age, score, dept) VALUES (1014, 'Liu Sanfeng, 'School of communications '); END $ DELIMITER; // restores the END mark to CALL p1 (); // calls the Stored Procedure p1

 

2. Parameters
// DELIMITER $ create procedure p2 (IN id INT, IN nm VARCHAR (30) begin insert into stud (id, sname) VALUES (id, nm); END $ DELIMITER; drop procedure p2; CALL p2 (1015, 'logging ');

3.

 

 

// Stored procedure with returned values ---- parameter and variable problems (@ variable name, one @ is the user variable, and two is the global system variable) DELIMITER $ create procedure p3 (IN id INT, IN nm VARCHAR (30), OUT num INT) begin insert into stud (id, sname) VALUES (id, nm ); select count (*) INTO num FROM stud; END $ DELIMITER; CALL p3 (1016, 'nameless ', @ aa); SELECT @ aa; // output the value of the variable aa

 


MySql case-sensitive Query
Mysql queries are case-insensitive by default, for example: select * from table_name where a like 'a % 'select * from table_name where A like 'A % 'has the same effect. To make mysql queries case sensitive, you can: select * from table_name where binary a like 'a % 'select * from table_name where binary a like 'a % 'can also be used when creating A table, to identify create table table_name (a varchar (20) binary)

Transaction: atomicity: the statement that makes up transaction processing forms a logical unit and cannot only execute a part of it.
Consistency: the database is consistent before and after transaction processing (database data integrity constraints ).
Isolation: the impact of one transaction processing on the processing of another transaction.
Durability: the effect of transaction processing can be permanently saved.
A transaction has only one result: either successful or failed.
Start transaction; Start a transaction. Commit; Submit the modifications. Rollback. If an error occurs during the operation, you should start a new transaction.



Set the transaction isolation level in the mySql command line.
Set the isolation level in the MySql command line, which is only valid for the opened command line window. The first command line window is an independent client.
Select @ tx_isolation; you can view the current isolation level.
Set transaction isolation level <level Name> to Set the isolation level.
Level name: {read uncommitted | read committed | repeatable read | SERIALIZABLE}
It should be noted that the isolation level must be used in the transaction. There is no transaction, and the isolation level is meaningless.
※Start transaction delete from stud WHERE id = 1015; delete from stud WHERE id = 1014; SELECT * FROM stud; ROLLBACK/COMMIT;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.