msql official document learning record

mysql learning record mysql.docx:
link: https: //pan.baidu.com/s/1bjAO8IZgxoyZDjvRwo-vJw
extraction code: bc76

Mysql
Txt import mysql
description of the mysql official documentation directly import data directly txt file to mysql database, the actual use of the process is experiencing some problems, such as the version did not specify win format (absolute address) on the template;
document:
the LOAD the DATA the LOCAL INFILE '/path/pet.txt' INTO TABLE PET LINES TERMINATED BY ' \ r \ n ';
practical application:
the LOAD the DATA INFILE the LOCAL' D: \ mysqltest \ named text.txt in 'the INTO TABLE PET LINES TERMINATED BY' \ R & lt \ n-'
Note: according to their need not always look for errors prompted experience to judge the blind Baidu will waste a lot of time.
Insert single data:
MySQL> INSERT INTO PET values ( 'Puffball', 'Diane', 'Hamster', 'F', '1993-03-30', NULL); Query the OK,. 1 Row affected (0.28 sec);
INSERT into pet values ( 'puffball', 'Aiane', 'hamster', 'f', '1996-03-30', NULL);
INTO PET values INSERT ( 'Puffball', 'Biane', 'Hamster', 'F', '1997-03-30', NULL);
INSERT INTO PET values ( 'Puffball', 'the Ciane', 'Hamster', ' F ',' 1996-07-30 ', NULL);
INSERT INTO PET values (' Puffball ',' Hiane ',' Hamster ',' F ',' 1991-03-30 ', NULL);
INSERT INTO PET values ( 'puffball', 'Giane' , 'hamster', 'f', '1997-03-25', NULL);
in order to verify your watch by the way you expect to create, use a DESCRIBE statement:
MySQL> DESCRIBE PET ;
MySQL eRROR 3948 (42000):
Lading local the data IS Disabled; the this the mUST bE enabled oN both-at the client and server Sides
MySQL error 3948 (42000): prohibition to load local data; must enable this feature in the client and server side
Cmd window tips clearly told not to use directly in the cmd window, you need to use the import feature in the client.
Query data in the air:
mysql> SELECT * FROM pet; ± --------- ± ------ ± -------- ± ----- ± ---------- - ± ------ + | name | owner | species | sex | birth | death | ± --------- ± ------ ± -------- ± - ---- ± ----------- ± ------ + | puffball | Diane | hamster | f | 1993-03-30 | NULL | ± -------- - ± ------ ± -------- ± ----- ± ----------- ± ------ + 1 row in set (0.00 sec )
query specifies a column and the column specified by the descending order of {} mysql> SELECT name, birth FROM pet oRDER bY birth DESC; ± --------- ± ----------- + | name | birth | ± --------- ± ----------- + | puffball | 1997-03-30 | | puffball | 1997-03-25 | | puffball | 1996-07-30 | | puffball | 1996-03-30 | | puffball | 1993-03-30 | | puffball | 1991-03-30 | ± --------- ± ------ ----- + 6 rows in set (0.00 sec)

Date function:
just know that the problem of animal birth time calculated for each animal age
mysql> select name, birth, curdate (), (year (curdate ()) - year (birth)) - (right (curdate (), 5) <right (Birth,. 5)) AS Age from PET;
± ± ----------- ----------- --------- ± ± - --- + | name | birth | curdate () | age | ± --------- ± ----------- ± ----------- ± ----- + | puffball | 1993-03-30 | 2020-02-22 | 26 | | puffball | 1996-03-30 | 2020-02-22 | 23 | | puffball | 1997-03-30 | 2020 -02-22 | 22 | | puffball | 1996-07-30 | 2020-02-22 | 23 | | puffball | 1991-03-30 | 2020-02-22 | 28 | | puffball | 1997-03-25 | 2020-02-22 | 22 | ± --------- ± ----------- ± ----------- ± ----- + 6 rows in set (0.03 sec)
for the right function documentation date function is also explained in no need to Baidu Baidu keyword underperforms. . Right (particular field needs to be removed by the digits to the right) of the rear portion is subtracted because 0 or 1;

Fuzzy query, packet statistics.
Wherein the front parts of the document are to be processed in a table
to create more than one table
creating table
; mysql> create table event (name VARCHAR (20), data DATE, type VARCHAR (15), remark VARCHAR (25))
after the Query structure of the table
Mysql> Event DESCRIBE;
± ± ------- ------------ ----- ± ± ± -------- ± ---- ------ + | Field | Type | Null | Key | Default | Extra | ± ------- ± ------------ ± ----- ± - - ± -------- ± ------ + | name | varchar (20) | YES | | NULL | | | data | date | YES | | NULL | | | type | varchar (15 ) | YES | | NULL | | | remark | varchar (25) | YES | | NULL | | ± ------- ± ------------ ± ----- ± ---- ± -------- ± ------ + 4 rows in set (0.00 sec)

In order to be able to contact two tables, establishment of the same pet table according to the document
name owner Species Sex Birth Death
Fluffy Harold CAT f 1993-02-04
Claws Gwen CAT m 1994-03-17
Buffy Harold Dog f 1989-05-13
Fang Dog m 1990-08-27 Benny
Bowser Dog Diane m 1979-08-31 1995-07-29
chirpy Gwen Bird F 1998-09-11
Whistler Gwen Bird 1997-12-09
Slim Benny Snake m 1996-04-29

insert into pet values(‘Fluffy’,‘Harold’,‘cat’,‘f’,‘1993-02-04’,NULL);
insert into pet values(‘Claws’,‘Gwen’,‘cat’,‘m’,‘1994-03-17’,NULL);
insert into pet values(“Buffy”,”Harold”,”dog”,”f”,”1989-05-13”,NULL);
insert into pet values(‘Fang’,‘Benny’,‘dog’,‘m’,‘1990-08-27’,NULL);
insert into pet values(‘Bowser’,‘Diane’,‘dog’,‘m’,‘1979-08-31’,1995-07-29);
insert into pet values(‘Chirpy’,‘Gwen’,‘bird’,‘f’,‘1998-09-11’,NULL);
insert into pet values(‘Whistler’,‘Gwen’,‘bird’,’\N’,‘1997-12-09’,NULL);
insert into pet values(‘Slim’,‘Benny’,‘snake’,‘m’,‘1996-04-29’,NULL);

name date type remark
Fluffy 1995-05-15 litter 4 kittens, 3 female, 1 male
Buffy 1993-06-23 litter 5 puppies, 2 female, 3 male
Buffy 1994-06-19 litter 3 puppies, 3 female
Chirpy 1999-03-21 vet needed beak straightened
Slim 1997-08-03 vet broken rib
Bowser 1991-10-12 kennel
Fang 1991-10-12 kennel
Fang 1998-08-28 birthday Gave him a new chew toy
Claws 1998-03-17 birthday Gave him a new flea collar
Whistler 1998-12-09 birthday First birthday

The data of the second table is a need to match two appearances association table queries
Insert into event values ( "Fluffy" , "1995-05-15", "litter", "4 kittens, 3 female, 1 male" );
the Insert INTO Event values ( "Buffy", "1993-06-23", "Litter", "Puppies. 5, 2 FEMALE, MALE. 3");
the Insert INTO Event values ( "Buffy", "1994-06-19 "," Litter "," Puppies. 3, FEMALE. 3 ");
the Insert INTO Event values (" chirpy "," 1999-03-21 "," VET "," needed Straightened beak ");
the Insert INTO Event values (" Slim "," 1997-08-03 "," VET "," Broken RIB ");
the Insert INTO Event values (" Bowser "," 1991-10-12 "," Kennel "," NULL ");
the Insert INTO Event values ( "Fang", "1991-10-12", "Kennel", "NULL");
the Insert INTO Event values ( "Fang", "1998-08-28", "Birthday", "A new new Chew Toy the Gave HIM ");
the Insert INTO Event values (" Claws "," 1998-03-17 "," Birthday "," A new new Flea HIM the Gave a collar ");
Insert into event values ( "Whistler" , "1998-12-09", "birthday", "First birthday");
two associated table query:
information required to be output, and where in between the two tables after the between contact:

mysql> select p1.name,p1.sex,p2.name,p2.sex,p1.species from pet as p1,pet as p2 where p1.species = p2.species and p1.sex = ‘f’ and p2.sex = ‘m’;

± ------- ± ----- ± ------ ± ----- ± -------- + | name | sex | name | sex | species | ± - ------ ± ----- ± ------ ± ----- ± -------- + | Fluffy | f | Claws | m | cat | | Buffy | f | Fang | m | Dog | ± ± ------- ----- ----- ± ± ± ------ -------- +
2 in rows SET (0.00 sec)
database default key:
create a table structure to do when there is no explicit comment on the key, the default database of their own data.
mysql> describe pet; ±--------±------------±-----±----±--------±------+ | Field | Type | Null | Key | Default | Extra | ±--------±------------±-----±----±--------±------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | ±--------±------------±-----±----±--------±------+ 6 rows in set (0.00 sec) mysql> describe event; ±-------±------------±-----±----±--------±------+ | Field | Type | Null | Key | Default | Extra | ±-------±------------±-----±----±--------±------+ | name | varchar(20) | YES | | NULL | | | data | date | YES | | NULL | | | type | varchar(15) | YES | | NULL | | | remark | varchar(25) | YES | | NULL | | ±-------±------------±-----±----±--------±------+ 4 rows in set (0.00 sec)

About unsigned zerofill
Reference: HTTPS: //www.cnblogs.com/Latiny/p/8058209.html
ZEROFILL: int (10) ZEROFILL because an int is four bytes need to be changed, or a custom field is longer as required zerofill character
Unsight: unsigned field
primary: primary key is set
Create Table Shop (
Article This article was int (. 4) unsigned zerofill default "0000" not NULL,
Dealer char (20 is) default "" not NULL,
. price Double (16,2) default " 0.00 "not NULL,
Primary Key (Article This article was, Dealer)
);
mysql>describe shop; ±--------±-------------------------±-----±----±--------±------+ | Field | Type | Null | Key | Default | Extra | ±--------±-------------------------±-----±----±--------±------+ | article | int(4) unsigned zerofill | NO | PRI | 0000 | | | dealer | char(20) | NO | PRI | | | | price | double(16,2) | NO | | 0.00 | | ±--------±-------------------------±-----±----±--------±------+ 3 rows in set (0.00 sec)

A column information query corresponding to the maximum line
method: using a nested - embodiment subqueries.
Article This article was the SELECT, Dealer,. Price
the FROM Shop
the WHERE. Price = (the SELECT MAX (. Price) the FROM Shop);

limit 1 means that only returns one row.

SELECT article, dealer, price
FROM shop
ORDER BY price DESC
LIMIT 1;

mysql> select article, max (price ) from shop group by article; ± -------- ± ----------- + | article | max (price) | ± ---- ---- ± ----------- + | 0001 | 3.99 | | 0002 | 10.99 | | 0003 | 1.69 | | 0004 | 19.95 | ± -------- ± --- -------- + 4 rows in set (0.00 sec)
queries the maximum between each group and the print
can using a subquery
idea subqueries:
the SELECT MAX (s2.price) the FROM Shop S2, S1 Shop WHERE s1.article = s2.article;
on portions not appreciated subqueries (nested one data output part only)
MySQL> the SELECT MAX (s2.price) the FROM Shop S2, the WHERE s1.article Shop S1 = s2.article; ± -------------- + | MAX ( s2.price) | ± -------------- + | 19.95 | ± - ------------ + 1 row in set (0.00 sec)

Create table person(id smallint unsigned not NULL auto_increment ,name char(60) not NULL, primary key (id));

Mysql query syntax issue official document: <> means that =!.
mysql> select * from person p, shirt s where p.name like 'Lilliana%' and s.owner = p.id and s.color <> 'white';

±—±--------------------±—±------±-------±------+ | id | name | id | style | color | owner | ±—±--------------------±—±------±-------±------+ | 2 | Lilliana Angelovska | 4 | dress | orange | 2 | | 2 | Lilliana Angelovska | 5 | polo | red | 2 | | 2 | Lilliana Angelovska | 6 | dress | blue | 2 | ±—±--------------------±—±------±-------±------+ 3 rows in set (0.00 sec)
mysql> select *from person p,shirt s where p.name like ‘Lilliana%’ and s.owner = p.id and s.color != ‘white’;

±—±--------------------±—±------±-------±------+ | id | name | id | style | color | owner | ±—±--------------------±—±------±-------±------+ | 2 | Lilliana Angelovska | 4 | dress | orange | 2 | | 2 | Lilliana Angelovska | 5 | polo | red | 2 | | 2 | Lilliana Angelovska | 6 | dress | blue | 2 | ±—±--------------------±—±------±-------±------+ 3 rows in set (0.00 sec)

@ && :=
mysql> select @min := MIN(price),@max :=max(price) from shop; ±-------------------±------------------+ | @min := MIN(price) | @max :=max(price) | ±-------------------±------------------+ | 1.25 | 19.95 | ±-------------------±------------------+ 1 row in set, 2 warnings (0.00 sec)

mysql variables not declared in advance, directly with the "@ variable name" used when using it.
The first usage: set @ num = 1; or set @num: = 1; // here to use variables to hold the data, variables directly @num
second Usage: select @num: = 1; or select @num : = field names from table where ......
Note that the above two assignment symbol can be used when using the set "=" or ": =", but must "use select: = assignment

select year,month,count(0) from (
select year,month,count(day) from t1 group by year,month,day
) as tmp group by year,month;

mysql> select year,month,count(0) from ( -> select year,month,count(day) from t1 group by year,month,day -> ) as tmp group by year,month;

±-----±------±---------+ | year | month | count(0) | ±-----±------±---------+ | 2000 | 01 | 3 | | 2000 | 02 | 2 | ±-----±------±---------+ 2 rows in set (0.00 sec)
mysql> SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1 -> GROUP BY year,month; ±-----±------±-----+ | year | month | days | ±-----±------±-----+ | 2000 | 01 | 3 | | 2000 | 02 | 2 | ±-----±------±-----+ 2 rows in set (0.00 sec)
mysql> select year,month ,count(day) from(select year ,mo:(day) from t1 group by year,month,day);

select year,month ,count(day) from(select year ,month,count(day) from t1 group by year,month,day) as group by year,month,day;
子查询:统计每个月的访问次数:
mysql> select year,month ,count(0) from(select year ,month,count(1) from t1 group by year,month,day) as tmp1 group by year,month; ±-----±------±---------+ | year | month | count(0) | ±-----±------±---------+ | 2000 | 01 | 3 | | 2000 | 02 | 2 | ±-----±------±---------+ 2 rows in set (0.00 sec)

CREATE TABLE animalstwo (
grp ENUM(‘fish’,‘mammal’,‘bird’) NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
);

mysql> CREATE TABLE Tanimals (-> grp ENUM ( 'fish', 'mammal', 'bird') NOT NULL, -> id MEDIUMINT NOT NULL AUTO_INCREMENT, -> name CHAR (30) NOT NULL, -> PRIMARY KEY (grp , id) / Why use joint gradually will prompt an error / ->);
eRROR 1075 (42000): in here Incorrect the Table Definition; there CAN bE only One Auto column and IT the MUST bE defined AS a key
to check for a long time to see whether a composite primary key (composite primary key) problems, there is no problem composite primary key on grammar,
in fact design complex mysql prompt syntax, only one increment and auto-incrementing field as the primary key.
According to the document description of the other two databases:
in MyISAM and BDB tables you can specify AUTO_INCREMENT and multi-column index in the second column. In this case, the calculated value is generated AUTO_INCREMENT column: MAX (auto_increment_column) + 1 WHERE prefix = given-prefix. If you want to put the data into an ordered group you can use this method.

Mysql designs require the auto_increment placed between the first column, a composite primary key location is declared the position corresponding to:
MySQL> Tanimals the CREATE TABLE (-> the AUTO_INCREMENT ID MEDIUMINT the NOT NULL, -> the ENUM GRP ( 'FISH', ' mammal ',' bird ') NOT NULL, -> name CHAR (30) NOT NULL, PRIMARY KEY (id, grp) / why the combined use of an error will gradually / ->); Query OK, 0 rows affected (0.87 sec ) mysql> mysql>

Always a problem that can be solved. The problem is the syntax requirements.

Published 212 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_42664961/article/details/104496564