A foreign key (one to many)
Role: 1 . Constraint 2 save space.
----------------------------------------- --------------------------------------- create table department ( id int auto_increment primary key, depart_name varchar(32) not null default '', num int not null default 0 )engine=Innodb charset=utf8; create table userinfo ( id int auto_increment primary key, name varchar(32) not null default '', depart_id int not null default 1, # Constraint foreign key name (fk_userinfo_depart) foreign key (column name (depart_id)) references the table name (Department) (column name associated (ID)), # constraint fk_userinfo_depart Foreign Key (depart_id) References Department (ID) ) Engine = Innodb = charset UTF8; ps: 1 . You can not create a separate statement the foreign key out, the following ways alter table userinfo add constraint fk_userinfo_depart foreign key (depart_id) references department(id); alter table userinfo drop foreign key 外键名称(fk_userinfo_depart ); 2 . When a foreign key, must be associated with the table's primary key ID 3 . Practice time, will write the statement in the text, and then perform the test in the past 4. The primary key index: Find the acceleration can not be empty + + can not be repeated
1, the only index:
1 The only index: create table t5( id int, num int, unique(num) )engine=Innodb charset=utf8;
effect: The value of the num column can not be repeated Find accelerate create table t6( id int, num int, unique(id, num) ) Engine = Innodb charset = utf8;
co-unique index action: Id value of the num column and the column can not be repeated Find accelerate create table t6( id int, num int, unique(id, num......) )engine=Innodb charset=utf8;
2, one to many:
Sector Table: id depart_name 1 PR 2 common part 3 Security Department Employees table: id name age depart_id(外键) Eighty 12 1 2 2 xxxx 13 1 3 xxxx 13 2
3, one-:
user table: id name age 1 intelligence 23 2 Eagon 34 Eighty 3 45 4 owen 83 Blog table: id url user_id (foreign key + unique constraint) . 1/2 linhaifeng 2 / intelligence 1 3/3 Eighty 4/4 Eighty
4. many-:
user table: id name phone 1 root1 1234 2 root2 1235 3 root3 1236 4 root4 1237 5 root5 1238 6 root6 1239 7 root7 1240 8 root8 1241 Host Table: id hostname 1 c1.com 2 c2.com 3 c3.com 4 c4.com 5 c5.com To facilitate the query, the user has the following number of users on a certain number of hosts and host, we need to create the third table: user2host: id userid hostid 1 1 1 2 1 2 3 1 3 4 2 4 5 2 5 6 3 2 734 time of creation, userid and hostid must be a foreign key, and then co-unique index unique (userid, hostid)
Second, the operation of the data row:
increase: insert into table (1 column name, column name 2,) values (value 1, value 2); insert into table (1 column name, column name 2,) values (value 1, value 2), (value 1, value 2), (the value of n, n-value); insert into table (1 column name, column name 2,) select a column name, a column name 2 from table; delete: delete from 表名; Delete from table name WHERE ID> 10 Delete from table WHERE ID <10 Delete from table WHERE ID <= 10 Delete from the table name WHERE ID> = 10 Delete from table name WHERE ID! = 10 Delete from table where id = 10 and name = ' XXX ' ; and : and the two conditions must be established Delete from table ID = 10 WHERE or name = ' XXX ' ; or : or as long as a condition is satisfied
modify: update 表名 set name='zekai', age=23 where id > 10; Inquire: Basic: select * from 表名; select name , age from 表名;
1, where the query conditions:
select * from 表名 where id=10; select * from 表名 where id >10 and id<15; the SELECT * from table name the above mentioned id the WHERE> 10 ; =! : unequal and > = <= the BETWEEN and : closed interval select * from t4 where id between 9 and 12; in : in one collection select * from t4 where id in (9,10,11....); select * from t4 where id in (select id from t3 where id between 2 and 4)
2. Tsuhaifu:
alex SELECT * from table name like WHERE ' ALE% ' - all at the beginning of the ALE (a plurality of strings) the SELECT * from table name like the WHERE ' ale_ ' - all at the beginning of ale (a character)
3, taking a few restrictions:
* SELECT from table index offset limit, the number of data extraction; SELECT * from T3 limit 0, 10 ; a first page SELECT * from T3 limit 10, 10 ; the second page page = input('page:') the index page of data offset amount (offset) 1 0 10 2 10 10 3 20 10 4 30 10 page (page-1)*offset offset Page Core SQL: select * from t3 limit (page-1)*offset, offset;
4 Sort by:
order by In descending order: select * from t4 order by 列名 desc; descending Ascending: select * from t4 order by 列名 asc; ascending Multi-row: create table t7( id int auto_increment primary key, num int not null default 0, age int not null default 0 )charset=utf8; insert into t7 (num, age) values (2, 12),(3,13),(4, 12); select * from t4 order by num desc, name asc; If the previous value of the column equal, will be further sorted according to the value one.
5, grouping
select age, aggregate functions (COUNT (NUM) / SUM (NUM) / max (NUM) / min (NUM) / AVG (NUM)) from table group by the name of the column; select age, avg(num) from t7 group by age; select age, count(num) from t7 group by age; Age SELECT, COUNT (NUM) as CNT from T7 by Age Group; Show Aliases as having secondary deletion of selected: select age, count(num) as cnt from t7 group by age having cnt>1; where and having difference: . 1 ). HAVING similar and where, can be screened data 2 ). Where role for columns in a table, query data 3). Having play for the query result column, secondary screening data, and with the group by using
6, even the operating table
* SELECT from UserInfo, Department; (Cartesian product) select * from userinfo, department where userinfo.depart_id=department.id; Left connection: select * from userinfo left join department on userinfo.depart_id=department.id; The table below shows all the left, the right is not used does not show Right Connections: select * from userinfo right join department on userinfo.depart_id=department.id; Table on the right shows all left no association is represented by null En: The data are displayed both sides ps: a. Just remember to connect the left left join b. a plurality of tables can be connected via a particular condition Note that the order of the query: select name,sum(score) from 表 where id > 10 group by score having age> 12 order by age desc limit 2, 10