Unique value in field for specific field in sql - mysql

I have to create a new table in SQL, but I have a problem.
I want to have a unique value in one field but only for a specific field in same table, similar like a one to many.
Table:
ID_Order
Supplier
ID_Supplier_Order
And now I want to have ID_Order unique for the table, and ID_Supplier_Order only for specific Supplier. Can I do it in one table, or I have to create a second for Suppliers?

Taken from:
Add unique constraint to combination of two columns
CREATE UNIQUE INDEX uq_yourtablename
ON dbo.yourtablename(column1, column2);

Related

how to create a parent child relation between two tables mysql

i have two tables, employee details table(empno, emp_name , branch) and attendance table(date, Ot, attendence). I want to create a relation between two tables
Basically for this case you cannot. Your two tables must have something in common so that you can make one table related to the other one.
In this case, you probably need to add additional columns in the attendance table to indicate the attendence record belonging to which employee. For example, add "empno" column in the attendence table, and then set this "empno" as a foreign key of the employee details table by:
ALTER TABLE attendance ADD FOREIGN KEY (empno) REFERENCES employee_details(empno);
The SQL script may differ depending on which SQL database you're using.

Create two columns with pairwise uniqueness

Is it possible, in MySQL, to create two unique indexes, together?
For example, there is a table that holds two IDs - customer ID and product ID.
I would like to create uniqueness for the pair only.
In simple words, combination of user and product can be present only once.
The same user with another products can be present as much as needed.
And the same product with another users can be present as much as needed.
But the pair itself to be unique. How?
This query actually has an OR relation. I am looking for an AND relation.
ALTER TABLE top ADD CONSTRAINT unique_pair UNIQUE (uid, pid)
This does what you describe:
ALTER TABLE top ADD CONSTRAINT unq_top_unique_pair UNIQUE (uid, pid);
As does:
CREATE UNIQUE INDEX unq_top_uid_pid ON TOP(uid, pid);
Both of these create a unique index, which is then used for enforcing the constraint.

Assign a unique id to 3 tables which have the same column name in mysql

I want to know how to give unique id to 3 tables which have the same column name in mysql.
when inserting a new value the value should be compared with all three table column values and assign a new unique id to the inserted table. Is this possible in mysql.
Thanks
You'll need to create a temporary table that has all the values aggregated together so you can check.
What do you mean by "unique id"? A suitable hash function like SHA1() usually gives you something reasonably unique.

How do I add foreign key constraints in my innodb table without indexing the columns?

i read some advice about table indexing, and i fount out that indexes should be introduced later when your sql queries are running slow..
my problem is I index almost all my columns in one table. this table is tied to other table, for example the users table column country is tied to the country table
user table country table
id id
countryId name
I added an index in the countryId column, so that I can link it to the country table's id because i can't create a foreign key constraints when I don't have an index on the column.
Basically i have many columns that are link to other table just to add constraints to my column.
for example, the country table is not more than 100, so if someone just accidentally inserted a value of 101, mysql won't accept that data because the country table only have 1-100..
So how can i avoid adding indexes and still have the function of table data constraints?
That advice is awful.
Index columns that you will be searching on or where you need particular constraints such as unique values.
The only reason NOT to add indexes is that they do take up space. But, unless you're working with so much data that you'll need a database administrator, you probably won't have to worry about it.

check duplication using mysql select

I have a category say ecommerce.add ebay and amazon.when i update ebay as amazon,it should n't update.How do i d it?
I suggest you check out unique indexes and primary keys. These will cause an insert or update to fail rather that allow duplicate entries to be made.
CREATE UNIQUE INDEX name_unique ON tablename (name(10));
Replace name_unique with the name you want for the index, tablename with the name of your table, and name(10) with the column name and how many characters you want to be unique (the length of the column if you want the entire value to be unique).