The tables User and Properties were created properly
CREATE TABLE Properties ( ID int AUTO_INCREMENT, language int, stonecolor int, gamefield int, UserID int, PRIMARY KEY(ID), FOREIGN KEY(language) REFERENCES Language(ID), FOREIGN KEY(stonecolor) REFERENCES StoneColor(ID), FOREIGN KEY(gamefield) REFERENCES GameField(ID) ) ENGINE = INNODB; CREATE TABLE User ( ID int AUTO_INCREMENT, vorname varchar(30) NOT NULL, name varchar(30) NOT NULL, email varchar(40) NOT NULL, password varchar(40) NOT NULL, nickname varchar(15) NOT NULL, score int, isadmin int DEFAULT 0, gamesPlayed int, properties int NOT NULL, PRIMARY KEY(ID), UNIQUE (email), UNIQUE (nickname) ) ENGINE = INNODB;
But ALTER TABLE User
doesn't work.
ALTER TABLE User ( ADD CONSTRAINT userPropertie FOREIGN KEY(properties) REFERENCES Properties(ID) )
Can't figure out why?
I used this as reference http://www.w3schools.com/sql/sql_foreignkey.asp
Error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( ADD CONSTRAINT userPropertie FOREIGN KEY(properties) REFERENCES Properties(' at line 2
You add informational constraints by using the CREATE TABLE or ALTER TABLE statement when you specify the NOT ENFORCED option on the DDL. Along with the NOT ENFORCED option, you can further specify the constraint to be either TRUSTED or NOT TRUSTED.
The constraint can be created within the CREATE TABLE T-SQL command while creating the table or added using ALTER TABLE T-SQL command after creating the table. Adding the constraint after creating the table, the existing data will be checked for the constraint rule before creating that constraint.
The basic syntax of an ALTER TABLE command to ADD CHECK CONSTRAINT to a table is as follows. ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
Use the ADD CONSTRAINT clause to specify a primary key, foreign key, referential, unique, or check constraint on a new or existing column or on a set of columns. This syntax fragment is part of the ALTER TABLE statement. Notes: For NULL and NOT NULL constraints, use instead the MODIFY Clause.
Omit the parenthesis:
ALTER TABLE User ADD CONSTRAINT userProperties FOREIGN KEY(properties) REFERENCES Properties(ID)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With