Skip to content

Instantly share code, notes, and snippets.

@Bogdaan
Created June 24, 2020 18:05
Show Gist options
  • Save Bogdaan/fad194360521571b3aa6e1d1644d21ad to your computer and use it in GitHub Desktop.
Save Bogdaan/fad194360521571b3aa6e1d1644d21ad to your computer and use it in GitHub Desktop.
set foreign_key_checks = 0;
drop table men;
drop table hobbies;
drop table man_hobbies;
set foreign_key_checks = 1;
create table if not exists men
(
id bigint unsigned auto_increment not null,
name varchar(255) not null,
website varchar(255) not null,
birthday date not null,
PRIMARY KEY (id)
) engine = InnoDB
charset = utf8;
create table if not exists hobbies
(
id bigint unsigned auto_increment not null,
title varchar(255) not null,
PRIMARY KEY (id)
) engine = InnoDB
charset = utf8;
create table if not exists man_hobbies
(
id_man bigint unsigned not null,
id_hobby bigint unsigned not null,
PRIMARY KEY (id_man, id_hobby),
CONSTRAINT fk_man FOREIGN KEY (id_man) REFERENCES men (id) ON DELETE CASCADE,
CONSTRAINT fk_hobby FOREIGN KEY (id_hobby) REFERENCES hobbies (id) ON DELETE CASCADE
) engine = InnoDB
charset = utf8;
insert into men (id, name, website, birthday) value (1, 'Богдан', 'https://hcbogdan.com', '1992-12-06');
insert into hobbies (id, title)
values (1, 'talk'),
(2, 'bike'),
(3, 'code'),
(4, 'have-a-fun');
insert into man_hobbies (id_man, id_hobby)
values (1, 1),
(1, 2),
(1, 3);
select *
from men m
join man_hobbies mh on m.id = mh.id_man
join hobbies h on mh.id_hobby = h.id
where h.title in ('bike');
# SELECT *
# FROM men
# WHERE age >25 AND hobby IN ('memes' , 'movies' , 'sport' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment