3

I have 2 tables:

  1. Authors(id, name, add, DOB) where the fields are INTEGER, CHAR(20), CHAR(20), DATE respectively. Primary key = id.

  2. Books(bid, title, publisher) where the fields are INTEGER, CHAR(20), CHAR(20) respectively. Primary key = bid.

I want to create another table Write with fields aid, bid, datePublished, where:

  1. aid = INTEGER ... this is actually the id from Authors.
  2. bid = INTEGER ... this is actually the bid from Books
  3. Primary Key = (aid, bid).

How do I do this?

I tried the following lines of code, and all of them gave syntax errors:

CREATE TABLE Write (
    aid INTEGER, 
    bid INTEGER, datePublished DATE, 
    PRIMARY KEY NONCLUSTERED (aid,bid), 
    FOREIGN KEY (aid) REFERENCES Authors(id), 
    FOREIGN KEY (bid) REFERENCES Books(bid)
);

CREATE TABLE Write (
    aid INTEGER, 
    bid INTEGER, 
    datePublished DATE, 
    PRIMARY KEY (aid,bid)
);
Phil
  • 157,677
  • 23
  • 242
  • 245

3 Answers3

3

Funny problem you're having. The reason is that Write is a reserved keyword in mysql and you can only use it as an identifier if you suround it by backticks in your queries. I recomend you picking another name or, if you really want to use Write, replace your create query with

CREATE TABLE `Write` (
    aid INTEGER, 
    bid INTEGER, 
    datePublished DATE, 
    PRIMARY KEY (aid,bid)
);

Just remember that any other query on the table Write would require the backticks as well, which might lead to annoying errors if someone forgets them.

Erik
  • 2,276
  • 1
  • 20
  • 20
1

Write is a reserved word, no table can be named that (it can using back tics but to make your life easy stay away from names that requires back tics).

Here is the complete schema http://sqlfiddle.com/#!2/dfe22/1

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
0

Try this :

CREATE TABLE Write (aid INTEGER, bid INTEGER, datePublished DATE PRIMARY KEY (aid,bid));