tags:

views:

105

answers:

2

How do I define relation rules in mysql?

Table1: bookId authorId bookName

Table2: authorId authorName

This is how I would like the Mysql server to behave: When I try to insert a row into table1 with a authorId that doesn't exsist in table2, mysql would produce a error.

A: 

First result in:

http://www.google.com/search?q=mysql+foreign+keys&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a

Itay Moav
That supposes someone knows that the term "foreign keys" is relevant to the problem. If you don't want to answer the question, then don't post an answer!
Bill Karwin
You probably missed the link I gave. Any way I expect (you can not agree with me, but it will be your problem) people to do some basic research before letting others do the work for them. All basic SQL tutorials will cover this. In my book, you are trying to promote zombie coders.
Itay Moav
You can teach a man to fish, and that's good, but you don't have to be so condescending about it.
Bill Karwin
You are right, most of the time, but more and more I see people who uses the platform (and the eagerness of people for rep) to cut corners they shouldn't. Put your self in his place, Can you imagine yourself asking, for example, how to do conditions in PHP when you where a noob? There are noob questions, and then there are lazy questions.
Itay Moav
I knew there was a feature for "Relation rules" or as I now know it "Foreign keys". I searched quite a while but couldn't find anything, so I went here and asked the question instead so that people looking for the same answer could easily find it!
Baversjo
@Itay: A friend of mine, when he was a child, asked his mother how to spell "psychology." She reacted with great annoyance and barked back at him, "there's the dictionary, look it up yourself!!" He wasn't being lazy -- he was asking a reasonable question. Her response was unfair and unhelpful. Do you see the parallel?
Bill Karwin
If I use your metaphor, the guy came, with the dictionary in his hands, and asked how to spell Psychology. Let's agree we do not agree on this. yes?
Itay Moav
+6  A: 

You have to define authorid as a foreign key. You need to do something like this:

Alter Table Table1 Add Foreign Key (authorid) References Table2 (authorid);

Make sure your tables are innodb because it won't work on myisam tables. You can find the documentation here.

Eric Hogue
Thank you very much :D
Baversjo