Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Unique Key in MySQL table referring to date

Tags:

sql

php

mysql

Question on preventing duplicated entry in my simple web form.

My table record user input from a web form, and distinguished by date e.g. DATE(). How to prevent user with the same name to enter information twice in a single date, e.g. same username cannot be entered twice in the same date, but can be entered at other date?

like image 811
cys Avatar asked Jul 13 '10 09:07

cys


People also ask

How do I create a unique key 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);

Is unique key created automatically?

There is an automatically defined unique key constraint within a primary key constraint.

Is unique key indexed?

To answer to question in bold: Yes, making a field unique does index it like s primary key.

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

Use SQL Server Management StudioIn 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.


1 Answers

Your table should have these:

create table tablename (
...
user_id bigint, -- or whatever
date_created date,
unique key(user_id, date_created)
...
);
like image 93
ceteras Avatar answered Sep 20 '22 23:09

ceteras