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?
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);
There is an automatically defined unique key constraint within a primary key constraint.
To answer to question in bold: Yes, making a field unique does index it like s primary key.
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.
Your table should have these:
create table tablename (
...
user_id bigint, -- or whatever
date_created date,
unique key(user_id, date_created)
...
);
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