drop table if exists users;
create table users(
user_id int unsigned not null auto_increment primary key,
username varbinary(32) not null,
unique key users_username_idx(username)
)engine=innodb;
insert into users (username) values ('f00'),('bar'),('bish'),('bash'),('bosh'),('F00');
drop table if exists user_friends;
create table user_friends(
user_id int unsigned not null,
friend_user_id int unsigned not null,
primary key (user_id, friend_user_id)
)engine=innodb;
insert into user_friends values
(1,2),(1,3), (2,1),(2,5), (3,5), (4,1),(4,2),(4,3),(4,5),(4,6), (5,4),(5,1);
-,
(user_id, friend_user_id)
select * from user_friends where user_id = 4;
, PK, user_id
friend_user_id, - .
.
delete from user_friends where user_id = 4 and user_friend_id = 5;
( )
drop table if exists user_friends;
create table user_friends(
friend_id int unsigned not null auto_increment primary key,
user_id int unsigned not null,
friend_user_id int unsigned not null,
unique key user_friends_idx (user_id, friend_user_id)
)engine=innodb;
-,
,
index on (user_id, friend_user_id). 2 PK
, .
. /
2 .cc.
select * from user_friends where user_id = 4;
, , friend_id PK,
, 1 , PK.
delete from user_friends where friend_id = 10;
, , , , :)