Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Create Table with Unique Constraints

Tags:

sql

mysql

I am just starting with SQL syntax, and am trying to create a table.

Here is my 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 'CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName), ) ENGINE = INNODB' at line 7

And here is my SQL:

CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` TEXT NOT NULL,
`firstName` TEXT NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id)
CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName),
) ENGINE = INNODB;

I tried this in NodeDB (which I am developing in), and then PHPMyAdmin.

like image 820
Arlen Beiler Avatar asked Jul 19 '14 12:07

Arlen Beiler


People also ask

How do I create a unique constraint in MySQL?

Sometimes we want to add a unique key to the column of an existing table; then, this statement is used to add the unique key for that column. Following are the syntax of the ALTER TABLE statement to add a unique key: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column_list);

How do you create a unique table constraint in SQL?

To create a unique constraintIn Object Explorer, right-click the table to which you want to add a unique constraint, and select Design. On the Table Designer menu, select Indexes/Keys. In the Indexes/Keys dialog box, select Add.

How do I add a unique constraint to an existing table?

The syntax for creating a unique constraint using an ALTER TABLE statement in SQL Server is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.

Can we define multiple unique constraints on a table in MySQL?

A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.


1 Answers

Fix the comma and make the names varchar():

CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) NOT NULL,
`firstName` varchar(255) NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id),
CONSTRAINT uc_people_2nd UNIQUE (lastName, firstName)
) ENGINE = INNODB;

This works on SQL Fiddle.

Note that you don't have to give a unique constraint a name. You can also drop the constraint keyword, so the following works just fine:

UNIQUE (lastName, firstName)

EDIT:

The text data type is described here on the page with other "large-objects". These are special types that are arbitrarily long (think megabytes). They have limits when used in indexes. In particular, they need a length prefix. So, you cannot declare that a text column is unique. Only that they are unique in the first N characters (up to about 1000).

For names, that is way overkill. MySQL supports string types of various sorts. The most useful is varchar(). These are appropriate for a name field. They can be used with indexes easily. And MySQL supports a plethora of functions on them.

In other words, if you do not know what text is, you do not need it. Learn about and use varchar() and char() (or nvarchar() and nchar() if you need national character set support). Forget about text. One day if you need it, you'll rediscover it.

like image 157
Gordon Linoff Avatar answered Sep 29 '22 21:09

Gordon Linoff