0, a unique index
The only limit to be unique num, num represents is unique, uql is the only index name
The above is the joint index: num and can not be exactly the same xx
1, the foreign key variants
a user table and the department table.
Users:
1 alex 1
2 root 1
3 2 Egon
4 laoyao 3
divisions:
1 Service
2 Security
3 PR
=== "one to many
b blog user tables and table
user table:
. 1 Alex
2 the root
. 3 Egon
. 4 laoyao
blog table:
FK () + unique
. 1 / yuanchenqi /. 4
2 / alex3714 /. 1
. 3 / asdfasdf /. 3
. 4 / FFFFFFFF / 2
===> one to one
code:
create table userinfo1( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8;
create table admin( id int not null auto_increment primary key, username varchar(64) not null, password VARCHAR(64) not null, user_id int not null, unique uq_u1 (user_id), CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id) )engine=innodb default charset=utf8;
c. many to many
create table userinfo2( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table host( id int auto_increment primary key, hostname char(64) )engine=innodb default charset=utf8; create table user2host( id int auto_increment primary key, userid int not null, hostid int not null, unique uq_user_host (userid,hostid), CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id), CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id) )engine=innodb default charset=utf8;
3, SQL statements to add rows of data operations
3.1 increase
INSERT INTO Tb11 (name, Age) values ( ' Alex ' , 12 is); # inserts a row into Tb11 the INSERT INTO Tb11 (name, Age) values ( ' Alex ' , 12 is), ( ' the root ' , 18 is); # Tb11 inserted into the plurality of data iNSERT iNTO TB12 (name, Age) SELECT name, Age from Tb11; # copying the data in the TB12 in Tb11
3.2 deleted
delete from tb12; delete from tb12 where id !=2 delete from tb12 where id =2 delete from tb12 where id > 2 delete from tb12 where id >=2 delete from tb12 where id >=2 or name='alex'
Change 3.3
update tb12 set name='alex' where id>12 and name='xx' update tb12 set name='alex',age=19 where id>12 and name='xx'
3.4 check
select * from tb12; select id,name from tb12; select id,name from tb12 where id > 10 or name ='xxx'; CNAME name AS SELECT, Age from TB12; # search data and the header of the name to CNAME SELECT ID, name AS CNAME from TB12 WHERE ID> 10 or name = ' XXX ' ; name SELECT, Age, 11 from TB12; # extra one, all the data for the 11
other:
* SELECT from ! TB12. 1 WHERE id = SELECT * from TB12 WHERE id in (1,5,12); # fetch the data id 1,5,12 SELECT * from TB12 WHERE id Not in (1,5,12) ; # taking the id no data 1,5,12 SELECT * from TB12 WHERE id in (SELECT id from Tb11) # first taken out of Tb11 id, investigation as to TB12 id SELECT * from TB12 BETWEEN WHERE id. 5 and 12; # fetch data id (closed interval) of 5 to 12
Tsuhaifu:
SELECT * from TB12 WHERE name like " a% " # check the name to a beginning with data SELECT * from TB12 WHERE name like " % a% " # survey data name in the band a is SELECT * from TB12 WHERE name like " A_ " # check the name starts with a, back with only one bit of data, such as ab, ag
Paging:
* SELECT from TB12 limit 10; # Check before 10 SELECT * from TB12 limit 0,10; # start reading from the first row 0, row 10 is read; SELECT * from TB12 limit 10,10; # beginning on line 10 reading, the reading line 10; SELECT * from TB12 limit 20,10; # start reading from the line 20, the read line 10; SELECT * from TB12 limit 10 offset 20; # start reading from the line 20, the read take 10 rows; # Combine Python Page: Page = the INPUT ( ' Please enter the page number you want to view ' ) page = int(page) (Page -1) * 10 SELECT * from TB12 limit 0,10; # and page 1 data SELECT * from TB12 limit 10,10; 2 # view the data page 2
Sort by:
* the SELECT from TB12 the Order by the above mentioned id desc; # the above mentioned id descending rows of the SELECT * from TB12 the Order by the above mentioned id asc; # the above mentioned id from small to large row of the SELECT * from TB12 Age desc by the Order, the above mentioned id desc; # Age descending row , id descending row (if the number of the same age, according to the descending row id) SELECT * from TB12 Order by id desc limit 10; # taken after 10 data
Creating departments and employees table:
Create Table department5 (
ID int AUTO_INCREMENT Primary Key,
title VARCHAR (32)
) Engine = InnoDB default charset = UTF8;
INSERT INTO department5 (title) values ( ' manager '), ( ' sales '), ( ' management '), ( ' financial ');
create table userinfo5( id int auto_increment primary key, name varchar(32), part_id int, CONSTRAINT fk_user_part FOREIGN key (part_id) REFERENCES department5(id) ) Engine = InnoDB default charset = UTF8;
INSERT INTO userinfo5 (name, part_id) values ( ' Yang Han ', 2), ( ' big wave ', 1), ( ' high months', 2), ( ' plenum ', 3 ), ( ' white ', 4);
Grouping:
max: # Press par_id classification, if part_id same, he took that id's largest classification count:
In addition there min, sum, avg
if secondary screening results for aggregation function? You must use having Example To filter out greater than 1 part_id id:
it can also be used where, aggregate functions can not add but later
Even the operating table:
做法:select * from userinfo5,department5 where userinfo5.part_id = department5.id
It recommended the following wording:
(1) select * from userinfo5 left join department5 on userinfo5.part_id = department5.id; # userinfo5 left shows all, because there is no corresponding department5 userinfo5 in Liu Yang, Liu Yang is not displayed
(2) select * from userinfo5 right join department5 on userinfo5.part_id = department5.id; # department5 All the right
# Hidden line will appear when null; (3) select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
# Userinfo5 number of data in statistics; (4) select count (id) from userinfo5
Exercise book: http://www.cnblogs.com/wupeiqi/articles/5729934.html