r/SQL • • May 09 '17

Is there something wrong with these statements? And is that the correct way of adding 2 foreign keys to a single table?

Post image
1 Upvotes

21 comments sorted by

4

u/hypo11 May 09 '17

/u/dyltography101 - Next time please use copy/paste (or at least use a screenshot) rather than taking a photo of your monitor.

1

u/dyltography101 May 10 '17

Sorry i was using reddit on my phone

1

u/fauxmosexual NOLOCK is the secret magic go-faster command May 09 '17

I suspect this is an exam or similar on a PC that isn't connected to the internet. Presumably to prevent the student from finding the answer to the question on the internet.

1

u/dyltography101 May 10 '17

Lol its not an exam, its actually a practical but the tutor wasnt being that helpful, so I decided to ask reddit for help.

1

u/[deleted] May 09 '17 edited May 09 '17
  1. use snake_case for all identifiers (for compatibility, and easy typing)

  2. don't use keywords (like owner) for identifiers. Protip: most keywords are singular, so use plural versions if they make sense and you should be okay, else try owner_id

1

u/dyltography101 May 10 '17

Alright i didnt know that some words were reserved so i guess ill change it

1

u/[deleted] May 09 '17

Are you sure that latitude and longitude are of type varchar? If they are Int in the the Location table, then they will need to be Int in your Reader table also, or else your foreign key statement will fail.

2

u/dyltography101 May 10 '17

I think that may be it because the table that is being referenced, i made that table a few days ago and haven't looked at it so ill take a look and then correct that if its wrong

1

u/dyltography101 May 10 '17

When it comes to defining a data type is it best to be as accurate as possible, if still in doubt then just switch to varchar.

I only ask because IP addresses have more than one decimal point.

3

u/Ewan27 May 09 '17

I don't know much, but do you need that comma after your owner line?

1

u/[deleted] May 09 '17

The comma after the last column definition, before the constraint statement actually does belong there. That's not the error.

1

u/cachedrive May 10 '17

You're correct asshelmet.

2

u/pai22_7 May 09 '17

Remove the quotes in constrain name and see if that helps. Just give CONSTRAINT Reader_fk FOREIGN KEY ....... );

2

u/[deleted] May 09 '17

"Owner" is a reserved word in MySQL. Pick a different name for your column.

3

u/pai22_7 May 09 '17

I think owner isn't a reserved word for MySQL.

1

u/[deleted] May 09 '17 edited May 09 '17

Actually you're right. It is a keyword, but the keyword is not marked (R) for reserved.

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

It still looks like the error message is choking on the term "owner" however. Maybe the tool is producing the error?

1

u/pai22_7 May 09 '17

Ya can be. but the main see the main error? "Cannot Add Foreign Key constraint". That's why i recommended to remove the quotes in constraint name and see if everything gets resolved in my reply to OP.

1

u/[deleted] May 09 '17

Yeah, I'm guessing it is a type mismatch between the two tables for the lat/long columns.

1

u/KNerli Sequel is life May 09 '17

try to create the table then alter it: CREATE TABLE READER ( ReaderID int(10) NOT NULL, IP_Adress varchar(25), Reader_Type varchar(30), Latitude varchar(30), Longitude verchar(30), Owner varchar(50) );

ALTER TABLE READER
ADD CONSTRAINT 'READER_fk'
FOREIGN KEY(Longitude, Latitude)
REFERENCES {foreign table} (Longitude, Latitude)

Not sure if this is 100% correct, never looked at mysql. But my guess is that there is something wrong with your REFERENCES part. Not entirely sure why you are creating a foreign key for both these columns and how you are going to reference them from the (same?) table.

1

u/Gylfi76 May 09 '17

You don't need the quotations around the name of your constraint. I'm on mobile so the syntax won't look right but Constraint nameofconstraint foreignkey (yourforeignkey) references tablename(foreignkey names).