E
E
entermix2015-12-24 16:40:43
MySQL
entermix, 2015-12-24 16:40:43

How to properly set up indexes?

Let's say there are tables:
users
id, email, ...
articles
id, user_id, ...

ALTER TABLE `articles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_client_id` (`user_id`) USING BTREE;

ALTER TABLE `articles`
  ADD CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

It is necessary to make it possible to add user_id = 0 to the articles table, i.e. article without binding to the user (now there will be an error) is this possible with the preservation of indexing?
Or does it need a separate table for links?
users_articles:
user_id, article_id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romy4, 2015-12-24
@entermix

Use not user_id=0 but user_id=null. so it is more correct from all points of view and does not cut indexing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question