Foreign key operation data line 42

外键;

create table department (
id int auto_increment primary key,
depart_name varchar(32) not null default ''
)engine=Innodb charset=utf8;

nsert into department (depart_name) values ('公关'), ('关关'),('关公');

create table userinfo (
id int auto_increment primary key,
name varchar(32) not null default '',
epart_id int not null default 1,

# constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
constraint fk_userinfo_depart foreign key (depart_id) references department(id)
)engine=Innodb charset=utf8;

insert into userinfo (name, depart_id) values ('root1', 1);
作用;
1.约束
2.save space


Note;
1. You can not create a foreign key statement separate out
(new field)
the ALTER the Table UserInfo the Add constraint A (constraint) fk_userinfo_depart foreign key (depart_id) references department (id);
remove the foreign key
alter table usertable drop foreign key foreign key name (fk_userinfo_depart);

when 2. foreign key must be associated with the table's primary key ID
when 3 exercises, the statement written in the text, and then test performed in the past
4 primary key index;
1. Find accelerate
2. not empty
3. can not be repeated

 

 

 

Foreign key variant; (Key)


1. unique index;
effect;
value 1.unique column can not be repeated,
2. Find the acceleration
Create Table T1 (
ID int,
NUM int,
UNIQUE (NUM)
) = Engine Innodb charset = UTF8;

United unique index;
Create Table T2 (
ID int,
NUM int,
unipue (ID, NUM)
) = Engine Innodb charset = UTF8;

 

2. to-many:
one to many or many-called
three tables: Press, author information, book

Many (or many): a Press can publish more than one book

Associatively: foreign key

for the department table:
the above mentioned id depart_name
1 PR
2 Ministry of Public
3 Security Department

employees table:
the above mentioned id name Age depart_id (foreign key)
1 LXXX 12 2
2 xxxx 13 1
3 2 13 xxxx

3. one:
two table: student table and the customer table

One: A student is a customer, a customer could become a school, that is, one to one relationship

Associatively: foreign key + unique

user table:
ID name Age
. 1 Zekai 23 is
2 street from the Egon 34 is
. 3 LXXX 45
. 4 Owen 83

blog table:
ID URL user_id (foreign key + unique constraint)
. 1 / linhaifeng 2
2 / Zekai. 1
. 3 / LXXX. 3
4/4 LXXX


4. many-:
three tables: Press, author information, book

To-many: one author can write more than one book, a book can have multiple authors, two-way-to-many, many-that is
   associated way: foreign key + a new table

user table:
the above mentioned id name Phone
1 root1 1234
2 1235 root2
. 3 1236 root3
. 4 root4 1237
. 5 root5 1238
. 6 root6 1239
. 7 root7 1240 is
. 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:

the above mentioned id userid hostid
1 1 1
2 1 2
3 1 3
4 2 4
5 2 5
6 3 2
7 3 4
time of creation, userid and hostid must be a foreign key, and then co-unique index uNIQUE (userid, hostid)

Django ORM will design

 


Second operation data line;
increase;
INSERT INTO table name (column 1, column 2) values (value 1, value 2);
INSERT INTO table name (column 1, column 2) values (value 1, value 2), ( value 1, value 2), (the value of n, the value n-);
the inster INTO table name (column 1, column 2) SELECT column 1, column 2 from table;


delete;
delete from table name;
delete from table 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 WHERE ID = 10
Delete from table where id = 10 and name = ' xxx '; and: and the two conditions must be established
delete from table where id = 10 or name =' xxx '; or: as long as a condition is established or

modified;
Update table set name =' qe ', age = 12 where id> 10;
inquiry;
basic;
the SELECT * from table name;
the SELECT name, Age from table name;
senior:

. A condition of the WHERE query:
select * from ID = 10 table WHERE;
select * from ID table name WHERE> ID 10 and <15;
select * from ID table name WHERE> 10;
=:! range with
> = <=


BETWEEN and: closed interval
select WHERE ID BETWEEN from T4 *. 9 and 12 is;

in: in one set
SELECT * WHERE ID from T4 in (9,10,11 ....);


SELECT * from T4 WHERE ID in (SELECT ID T3 from ID WHERE between 2 and 4)

can be used as such, but do not recommend use;

B wildcard;.
SELECT * from table where name like 'aa%': in all (a plurality of matching strings) at the beginning AA
SELECT * from table where name like 'aa_': in the beginning of all aa (a string match)

. c take a limit;
SELECT * from limit table; index offset, how many removed

select * from t3 limit 0, 10 ; a first page
select * from t3 limit 10, 10 ; the second page
Page INPUT = ( 'Page:')

Page index offset amount of data (offset)
. 1 0 10
2 10 10
. 3 20 is 10
. 4 30 10

Page (Page-. 1) * offset offset

tab core the SQL:

SELECT * from T3 limit (Page-. 1) * offset, offset;
. D sorted
by order by
descending;
SELECT * from T4 order by column name desc ; Descending
ascending:
SELECT * from T4 Order by column name asc; ascending

multi-column:

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 column value equal, will be further sorted according to the value a.


E packets.

select age aggregate function (count (num) / sum ( num) / max (num) / min (num) / agv (num)) from table group by the column name;

select age, AVG (NUM) from T7 group by Age ;

SELECT Age, COUNT (NUM) by Age Group from T7;

SELECT Age, COUNT (NUM) from AS T7 CNT by Age Group; Show aliases AS

HAVING secondary screening;
SELECT Age, COUNT (NUM) AS CNT from T7 Group by age having cnt> 2;


the difference between where and having:
.. 1) and where HAVING Similarly, data can be screened
2) play a role where columns for tables, query data.
. 3) HAVING role for the query result column, secondary screening data, and. group by with the use of


. f table operation even
select * from userinfo, department; (Cartesian product)

SELECT * from UserInfo, Department WHERE userinfo.depart_id = department.id;

Left connection:

the SELECT * from UserInfo left the Join Department userinfo.depart_id ON = department.id;
the left table shows all, did not use the right does not show

the right connection:

the SELECT * from UserInfo right ON the Join Department userinfo.depart_id = department.id;
table on the right shows all, not associated with the left or null

within the connection:
left and right sides of the data will show

PS:
. a left connected only to remember the Join left

. b can be connected to multiple tables by a particular condition

attention query sequence:
Elect name, SUM (Score) from table where id> 10 group by score having age> 12 order by age desc limit 2, 10

Guess you like

Origin www.cnblogs.com/komorebi/p/11025499.html