итак .. Я учу отношения многих со многими в mysql.
Я создал следующие таблицы.
create table post(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(40),
content text,
created int(11) default 0
);
create table category(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(35)
);
create table author(
id int not null primary key auto_increment,
name varchar(20)
);
create table postcategory(
id int not null primary key auto_increment,
post_id int,
category_id int,
foreign key(category_id) references category(id)
on delete no action,
foreign key(post_id) references post(id)
on delete no action
);
Это строки, которые я вставил
insert into author(name) values('James');
insert into author(name) values('Moon');
insert into author(name) values('Min');
insert into category(name) values('C#');
insert into category(name) values('Art');
insert into category(name) values('PHP');
insert into category(name) values('Programming');
insert into post(title) values ('Introduction to C#');
insert into post(title) values ('Modern Art');
insert into post(title) values ('PHP Classes');
insert into post(title) values ('Classes in C#');
insert into post(title) values ('Polymorphism in C#');
insert into post(title) values ('Classic Art');
insert into post(title) values ('OOP in PHP');
insert into postcategory(post_id,category_id) values(1,1);
insert into postcategory(post_id,category_id) values(4,1);
insert into postcategory(post_id,category_id) values(5,1);
insert into postcategory(post_id,category_id) values(2,2);
insert into postcategory(post_id,category_id) values(2,6);
insert into postcategory(post_id,category_id) values(3,3);
insert into postcategory(post_id,category_id) values(3,7);
insert into postcategory(post_id,category_id) values(1,4);
insert into postcategory(post_id,category_id) values(4,4);
insert into postcategory(post_id,category_id) values(5,4);
insert into postcategory(post_id,category_id) values(3,4);
insert into postcategory(post_id,category_id) values(7,4);
В основном у меня есть сообщение, автор, категория и объединяющая таблица с именем postcategory.
Пока я могу вспомнить два запроса с таблицей посткатегорий.
- Запрос всех сообщений в категории C #
- Запрос всех категорий с идентификатором поста № 1
Есть ли другие запросы (я уверен, что есть ..), которые я могу использовать с таблицей посткатегории?
Я просто пытаюсь выучить множество случаев использования отношений.