Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER TABLE add constraint

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

like image 959
ABLX Avatar asked Apr 17 '12 14:04

ABLX


People also ask

Can ALTER TABLE add constraints?

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.

How can constraints be added to a table?

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.

How add constraint in SQL Server using alter?

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);

How do you add constraints to an existing table syntax?

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.


1 Answers

Omit the parenthesis:

ALTER TABLE User      ADD CONSTRAINT userProperties     FOREIGN KEY(properties)     REFERENCES Properties(ID) 
like image 183
Andomar Avatar answered Sep 18 '22 17:09

Andomar