I record the datas of a competition in an MySQL database.
I need to have unique entry for each player so i’ve done this :
alter table xtu_datas_competition add unique index(email);
It works perfectly.
But there are several competitions, so i have also a colum id_ competition and i want to have unique entry for email and for specific colum id_ competition
For example :
2 identical email for 2 identical colum id_ competition : duplicate
2 identical email for 2 different colum id_ competition : not duplicate
How can i adapt my previous code ?
You simply have to define a unique constraint containing both columns:
alter table xtu_datas_competition add unique index(id_competition,email);
Hope this will help you.
Related
Im trying to create a table through phpMyAdmin where I can REPLACE INTO a row where two columns match.
I have a table called channel_data that has 2 assigned ID's. One for the relevant server (guild_id) and one for the relevant data's owner (owner_id).
REPLACE INTO channel_data (guild_id, owner_id, username, userlimit)
VALUES ('123456789123456789', '123456789123456789', 'foo', '100');`
Above is an example of what I want to achieve but I want it to replace the values of username & userlimit if a row is found with the same guild_id & owner_id; but if not - it creates a new row...
I hope this makes sense...
Thanks in advance :)
Create a unique multi-column index on the two columns.
ALTER TABLE channel_data
ADD UNIQUE INDEX (guild_id, owner_id);
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);
So my intention here is to make the "Employee" contain 2 references to the same Primary Key in the "EmployeeContactInformation" table. The reason for this, is I want my employee to contain 2 different copies of the same table. E.g. 1 for work contact info, another for home contact info.
How would I implement this and what relationship would I use?
I'm assuming 1 to Many?
Current database screenshot
Lets change the design of your database:
Creat a table named as ContactInfoTypes. It will hold the definitions of each ContactInfoTypes you want (in your case: workContactInfo and homeContactInfo). It will have two columns (contactTypeId, contactTypeName)Add an extra column to named as ContactInfoType (DATATYPE number) in your EmployeeContactInfo TABLE. The Column ContactInfoType will hold a Foreign-Key value of above TABLE ContactInfoTypes
When you insert a contact in TABLE: EmployeeContactInfo, you would have to Insert two rows (one with the number value stored against workContactInfo and one with the homeContactInfo).
Employee can have several contact Infos ( home and work like you mentioned). So therefore the relationship should be one to many
To model this you should add a foreign key of the one side of the relationship into the many side . Therefore you should add an employee_id column in the EmployeeContact table. This way each employee contact row will be connected to an employee
"Work extension" column in the employee table can be moved to the EmployeeContact table and renamed "extension" since it is a phone number extension that could be for a home phone or work phone
"Home phone number " column in the EmployeeContact table should be renamed to just "phone number" because the table is for both home and work
Should save another column in the EmployeeContact table which saves information about work/home
I have table Employers, with: ID, surname, name, language_id.
Other table is Languages, with: ID, language.
How to put if one employer speak more than one language?
You can create an other table with the following fields :
ID (int,primary_key)
id_Employers(int)
id_Languages(int)
And remove language_id from Employers table
You want to remove lanugauge_id from Employers Table and create one new table which contain multi relation between user_id and language_id so you want to take
YOUR NEW TABLE NAME(i.e : user_languages)
COLUMN NAME :
--------------------------
1. id (int,primary_key)
2. employers_id(int)
3. language_id(int)
4. create_time(DateTime)
Using this you will enter multiple entries for multiple languages from single user where you want to enter employers table primary key in employers_id and languages table primary key in language_id
i have a logical question for optimization tables relation in MySQL (and even other DBMS).
there are many table with different columns and structures in my database. for a specific reason, i need to create a new table, names "extra_data" with there columns (id, table, content). each table (even "extra_data" itself) MAY needs to store extra data for undefined columns. so this extra data that is a PHP array or object, first serialized and then transform to Base64 code and inserted to "eaxtra_data" table content column. id and table name of target table row, stored in id and table columns in "extra_data" too.
for example if there is two tables names "users" and "posts" and each of them need to store extra data in "extra_data" table:
"users" table:
id name gender ...
1 Tom Male ...
2 Mary Female ...
3 Jack Male ...
...
"posts" table:
id title date ...
1 news 4/11/2014 ...
2 article 4/51/2014 ...
...
"extra_data" table:
id table content
1 users #$!^...
2 users #$!^...
2 posts #$!^...
...
id and table is primary key in "extra_data" table. so problem is when target row (form example row 2 in users) has been changed or removed, related row in "extra_data" must updated automatically if it's necessary. but the way to set foreign key in many table and in two columns (id, table) together, is imprecise to me! thanx for any help
DarkMaze,
I see where you come from with this "extra_data" table. I don't agree, though. Once it goes against the principles of database normalization and this logic is not clear on a design perspective. I'd rather serialize whatever data you want to add to extra_data content column and save it into its respective table.
Back on the question, the only solution I see so far is to add triggers on every table related to "extra_data" in order to add/remove/update records. Which is not very productive.
Cheers,
http://en.wikipedia.org/wiki/Database_normalization