mysql - Creating a relationship between tables and creating a foreign key -
how make relation between these tables?
create table `categories` ( `id` smallint(6) not null, `name` varchar(256) not null, `description` text not null, `position` smallint(6) not null, primary key (`id`) ) engine=innodb; create table `pm` ( `id` bigint(20) not null, `id2` int(11) not null, `title` varchar(256) not null, `user1` bigint(20) not null, `user2` bigint(20) not null, `message` text not null, `timestamp` int(10) not null, `user1read` varchar(3) not null, `user2read` varchar(3) not null ) engine=innodb; create table `topics` ( `parent` smallint(6) not null, `id` int(11) not null, `id2` int(11) not null, `title` varchar(256) not null, `message` longtext not null, `authorid` int(11) not null, `timestamp` int(11) not null, `timestamp2` int(11) not null, primary key (`id`,`id2`) ) engine=innodb; create table `users` ( `id` bigint(20) not null, `username` varchar(255) not null, `password` varchar(255) not null, `email` varchar(255) not null, `avatar` text not null, `signup_date` int(10) not null ) engine=innodb;
if topics has users author,
if storing id value of users author of topics in authorid column,
the "author" relationship one-to-many relationship. topics related 1 (or zero) users. (and users can "author" of zero, 1 or more topics.)
here's example of foreign key definition have database (innodb storage engine) enforce integrity of relationship:
alter table topics add constraint fk_topics_users_author foreign key (authorid) references users (id) on update cascade on delete restrict
Comments
Post a Comment