Need to add constraint on the basis of another column - mysql

We need to add a foreign key constraint on the basis of another column. As you can see in the screenshot below, we have columns refer_id and reference_type. We need to add a constraint on the column refer_id based on the value contained in column reference_type. We will always have two values of data type ENUM: Manual or User.
If reference_type has a value of Manual, we need to add a foreign key to the refer_id column on table reference_detail whose primary key column is id. If reference_type has a value of User, then we need to add a foreign key to the refer_id column on table users whose primary key is id.
Please help me how to add constraints on the basis of these conditions.
Please see table structure in below screenshot.

You can't add conditional foreign key(At least not in MySQL). A foreign key on the table can only reference a single primary column, and even if you can(in some RDBMS) it is considered a bad practice.
What you can do is that, you can create two child tables which references each parent table reference_detail(ENUM:Manual) and users(ENUM:Users).
It will be more clearer than your approach and easier to query. This is what I can make out of the database structure you have described.
Hope this helps.

Related

Can we make existing column as primary key?

I have one column name Token and I'm generating random numbers and saving them in token but sometimes it saves duplicate tokens so I want to make it unique.
I want to know will it affect existing records.
If you try to add a unique constraint (or primary key constraint) to a column that contains non-unique values, the alter statement will just fail. You need to first update the column so all values are unique (or remove duplicates), and then alter the table.
ALTER table Student add primary key (studentID)
Use the Alter command to edit table's DDL and then add a primary key to it by specifying the column.
If the primary key already exist, then first you will have to drop it before defining another PK by -
ALTER table STUDENT drop CONSTRAINT <constraint_name>
Try doing this
ALTER table_namePersons ADD UNIQUE (Token);
After doing this if you'll try to insert a duplicate key you will have an error and catching it you can generate another token

Foreign Key Constraint syntax

I'm new to learning SQL syntax and came across this example in a book. I understand the need for foreign keys and using the constraint function in order to set the key to another table that is created (EMPLOYEE_TBL in this example).
My question is why it listed the line CONSTRAINT EMP_ID_FK FOREIGN KEY (EMP_ID). What exactly is the EMP_ID_FK portion? Since you just need to use the CONSTRAINT function to set a field on you child table to the parent table, couldn't you just write it as CONSTRAINT FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE_TBL (EMP_ID)); instead?
Am I understanding this incorrectly? Any help would be appreciated. Thanks!
CREATE TABLE EMPLOYEE_PAY_TBL
(EMP_ID CHAR(9) NOT NULL,
DATE_HIRE DATE NULL,
DATE_LAST_RAISE DATE NULL,
CONSTRAINT EMP_ID_FK FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE_TBL (EMP_ID));
The clause CONSTRAINT EMP_ID_FK simply gives a name to the constraint. This becomes necessary later if you want to disable or drop the constraint. You are correct that the name (EMP_ID_FK) is optional; if you omit it you can also omit the CONSTRAINT keyword, as the clause FOREIGN KEY is enough to tell the interpreter what it is you want.
Naming your constraints is entirely optional but is considered good practice for documentary purposes and in case, as I said, you later need to do something with the constraint. If you omit the name, the database will automagically name the constraint for you, but good luck finding out what that name is.
Put it like this.
Table 1 has to have a reference to Table 2. Table 2 has a Primary Key ID that is a unique value, that means it can be accessed with this ID, as it is unique in all the rows of this table. Now table Table 1 needs to reference to that ID. You need to store that ID in Table 1 so you can reference to it and you know to which Table 2, Table 1 points. You use the naming convention Table2_ID_FK to know that that Field in Table 1 is a reference to the ID of table 2. The constraint is to set the actual relation between these tables.

SQL Force one column to be unique for every row that shares the same foreign key

Just wondering if this can be enforced by the database or not...
I have a table that has a foreign key, and another column that needs to be unique across rows where the foreign key is the same. Duplicate entries are allowed as long as the foreign keys are different.
Is there a way to do this? I can't seem to figure out a way to set a unique constraint that is based on some condition rather than applied to the entire table.
You can create UNIQUE constraint on both columns too.
ALTER TABLE myTableName
ADD CONSTRAINT tb_UQ UNIQUE (FKColumn, OtherColumn)
UPDATE 1
SQLFiddle Demo
You can add a unique constraint on two columns in MySQL:
alter table add unique index table(fk, othercolumn)

MySQL Foreign Keybut Query

I have a column that's a foreign key. Adding rows to the column is fine as long as it the row I'm adding has data that is present in the parent table. Although, some rows don't have an entry that belongs in the parent table. I'd like to keep the column with a foreign key but even if it doesn't have a parent, the column should still store it.
Are there any ways to do this within MySQL?
Regards
A foreign key constraint enforces that the value exists in the referenced table. If you try to insert a value that doesn't exist in the referenced table then it will fail.
You have two options:
Store NULL instead of the ID that doesn't exist.
Don't use a foreign key constraint.

Referencing a unique field in another table

I have a table that has a primary key and a unique key.
From another table, is it possible to reference this unique key just the way Primary Key's and Foreign Keys are referenced & used ?
Would like to define this in the SQL definitions so that data correlations between these two tables are automatically cross referenced.
Foreign keys specifically state they only need a unique and not-null key to work. Using a primary key just means it's a unique non-null key.
So, yes you can! You can even reference unique multi-column groups if you need to.
Search for the word "unique" within this page, you'll find further explanations.