Hi,
How do you implement unique constraint on 2 columns.i.e consider i have 3 column id,name and active.If the there is a row in a table with 1,apple,true than it should not allow the entry of a row with values 2,apple,true.But if the there is already row with 1,apple,false than it should allow the entry of 2,apple,true.Here the constraint must be applied on name and active column.
Thank you.
1 3651 Atli 5,058
Expert 4TB
Hi.
So basically, there should never be two rows with identical values for both the name and active columns?
If so, consider this: -
mysql> CREATE TABLE test (
-
-> `id` Serial Primary Key,
-
-> `name` VarChar(255) Not Null,
-
-> `active` Bool Not Null,
-
-> Unique Key `NameActive` (`name`, `active`)
-
-> );
-
Query OK, 0 rows affected (0.33 sec)
-
-
mysql> INSERT INTO test(name, active) VALUES ('apple', true);
-
Query OK, 1 row affected (0.01 sec)
-
-
mysql> INSERT INTO test(name, active) VALUES ('apple', true);
-
ERROR 1062 (23000): Duplicate entry 'apple-1' for key 3
-
-
mysql> INSERT INTO test(name, active) VALUES ('apple', false);
-
Query OK, 1 row affected (0.00 sec)
Is that what you are after?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: KULJEET |
last post by:
foreign key also refer to unique constraint.
(GREAT...)
1.then table that containt unique constraint act as master table?????
2.IS unique constraint will replace with primary key??
3.Is unique...
|
by: Agoston Bejo |
last post by:
I want to enforce such a constraint on a column that would ensure that the
values be all unique, but this wouldn't apply to NULL values. (I.e. there
may be more than one NULL value in the column.)...
|
by: June Moore |
last post by:
Hi,
I would like to add a unique index that consists of two fields in a
table.
e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination
must be Unique.
Can anyone tell me the...
|
by: Kamil |
last post by:
Hello
What should I use for better perfomance since
unique constraint always use index ?
Thanks
Kamil
|
by: Dave |
last post by:
Can you create a unique constraint on multiple columns, or does it have
to be implemented as a unique index?
If possible can someone please post some sample code?
Thanks,
|
by: Prince Kumar |
last post by:
Is there any way I can define an Unique constraint or unique index
which allows more than one null values for the same column combination
in DB2?
ie, If my index is defined on (col3, col4) where...
|
by: Brian Keating |
last post by:
Hi there,
Is it possible to add a unique constraint on two columns in a table, so that
the constraint is a composite of the two? i.e. these two columns together
should be unique...?
i.e....
|
by: D. Dante Lorenso |
last post by:
I'm trying to build a table that will store a history of records
by enumerating the records. I want the newest record to always
be number ZERO, so I created a trigger on my table to handle the...
|
by: Laurence |
last post by:
Hi there,
How to differentiate between unique constraint and unique index? These
are very similar but I cannot differentiate them?
Could someone give me a hand?
Thanks in advance
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |