Table primary unique index as two columns? - mysql

How can I define a primary key in my table as two unique fields?
I have several columns in my table and I have two keys: ID and SOURCE. Values for ID can appear twice in the column, but the combination of ID + SOURCE can be relied on as a primary key.
For example:
ID | SOURCE | TEXT
------------------
UNIQUE - 2312 | FB | BLA BLA
UNIQUE - 2312 | MU | BLA BLA
I'm using MySQL database. How I can I make the primary key on two columns?

You may use the ALTER TABLE syntax, like this:
ALTER TABLE my_table ADD CONSTRAINT my_table_pk PRIMARY KEY (id, source);
Also, this can be defined in CREATE TABLE, like this:
CREATE TABLE my_table
(
id int,
source varchar(3),
text varchar(20),
PRIMARY_KEY(id, source)
)

Add a constraint to enforce uniqueness enforced on two columns:
Alter table table_name add CONSTRAINT constr_ID unique (ID,SOURCE);
OR add a constraint primary key enforced on two columns:
Alter table table_name add PRIMARY KEY (id, source);

Related

PostgreSQL, MonetDB and MySQL add foreign key to existing table

When I add a foreign key to a table that already has data, what does each of these database management systems do?
Do they analyze each value of the column to confirm it is a value from the referenced table primary key ?
Or do they have some other optimized mechanism ? And if that's the case, what is that mechanism ?
I can't confirm for MonetDB, but in PostgreSQL and MySQL (and most probably on MonetDB too) the answer is yes, they will check every value and will raise an error if the key does not exists on the referenced table.
Notice that the referenced column doesnt need to be the primary key for the referenced table - you can reference any column as a foreign key to the other table.
Yes, of course, a constraint that is not enforced would make no sense.
You can just try(this is for Postgres):
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE one
( one_id SERIAL NOT NULL PRIMARY KEY
, name varchar
);
INSERT INTO one(name)
SELECT 'name_' || gs::text
FROM generate_series(1,10) gs ;
CREATE TABLE two
( two_id SERIAL NOT NULL PRIMARY KEY
, one_id INTEGER -- REFERENCES one(one_id)
);
INSERT INTO two(one_id)
SELECT one_id
FROM one ;
DELETE FROM one WHERE one_id%5=0;
ALTER TABLE two
ADD FOREIGN KEY (one_id) REFERENCES one(one_id)
;
\d one
\d two
Result:
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table tmp.one
drop cascades to table tmp.two
DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 10
CREATE TABLE
INSERT 0 10
DELETE 2
ERROR: insert or update on table "two" violates foreign key constraint "two_one_id_fkey"
DETAIL: Key (one_id)=(5) is not present in table "one".
Table "tmp.one"
Column | Type | Modifiers
--------+-------------------+------------------------------------------------------
one_id | integer | not null default nextval('one_one_id_seq'::regclass)
name | character varying |
Indexes:
"one_pkey" PRIMARY KEY, btree (one_id)
Table "tmp.two"
Column | Type | Modifiers
--------+---------+------------------------------------------------------
two_id | integer | not null default nextval('two_two_id_seq'::regclass)
one_id | integer |
Indexes:
"two_pkey" PRIMARY KEY, btree (two_id)
The error message is the same as for an actual insert or update. And you can see that the engine bails out once it en counters the first conflicting row.

Mysql create table with two foreign keys referencing primary key

I have two mysql tables. The first one is created using the following code:
create table project(
project_id int not null auto_increment,
project_name varchar(30)
primary key(project_id));
The second table:
create table matches(
match_id int not null auto_increment,
match_name varchar(30),
project_id int(4) foreign key (project_id) references projects(project_id));
Both these commands works fine. I want to add the project_name column from the first table to the second table. I tried using
alter table projects drop primary key, add primary key(project_id, project_name);
and then
alter table matches add column project_name varchar(30), add foreign key (project_name) references projects(project_name);
But got the following error:
ERROR 1005 (HY000): Can't create table 'matches.#sql-55e_311' (errno: 150)
How do i include both the columns from the first table into the second table.
The current structure of my second table is as follows:
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| match_id | int(11) | NO | PRI | NULL | auto_increment |
| match_name | varchar(30) | YES | | NULL | |
| project_id | int(4) | NO | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
I want to add the project_name as the fourth column in my second table.
To use a compound Primary Key as Foreign Key, you'll have to add the
same number of columns (that compose the PK) with same datatypes to
the child table and then use the combination of these columns in the
FOREIGN KEY definition.
see related post here https://stackoverflow.com/a/10566463/4904726
so try this way
alter table matches add foreign key (project_id, project_name) references projects(project_id, project_name);
Do you understand what a FK constraint says? It says a list of values for some columns in a table must appear as a list of values for some columns forming a (declared) PK or UNIQUE NOT NULL in the referenced table. So what you are writing doesn't make sense. If you wanted a PK (project_id, project_name) then that should also be the form of your FK.
But (project_id, project_name) is not two 1-column PKs, it is one 2-column PK, and it is probably not what you want since presumably in projects it isn't just pairs that are unique it is each column. Presumably you want two 1-column PKs and two one-column FKs, one referencing each PK.
If projects project_id was NOT NULL you could write:
alter table projects add primary key(project_name);
alter table matches add column project_name varchar(30),
add foreign key (project_name) references projects(project_name);
But if project_name can be NULL in projects then you cannot make it a PK and you cannot sensibly have a FK to it. You can make it UNIQUE. Unfortunately MySQL lets you write such a FK declaration to a non-NULL UNIQUE column while it also tells you not to do it:
The handling of foreign key references to non-unique keys or keys that contain NULL values is not well defined for operations such as UPDATE or DELETE CASCADE. You are advised to use foreign keys that reference only keys that are both UNIQUE (or PRIMARY) and NOT NULL.
So if you want projects project_name to be nullable then should declare it UNIQUE but you should enforce the logical matches project_name match with a trigger.

Foreign key referenced value used only once

I have this kind problem regarding sql:
Table 1: ID_1 (Primary Key) | NAME | LASTNAME | EMAIL...
Table 2: ID_2 (Primary Key) | NAME2 | LASTNAME2 | EMAIL...
Normally using constraints it is possible to make as column in TABLE 2
(NAME 2 for example) as a foreign key for NAME the first TABLE 1.
The general relation would be one-many. That's one value from column NAME in TABLE 1 used many times in column NAME2 in Table 2.
Now what I want to achieve is that making the rule one-to-one, so I cannot have duplicated values of NAME from table TABLE 1 in TABLE 2.
Does it require triggers or it can be set by constraint when creating the tables?
Steps to achieve this:
Create foreign key constraint
Create unique constraint
This can all be done while creating the table.
CREATE TABLE table2 (
...,
name2 varchar(255),
FOREIGN KEY (name2) REFERENCES table1(name),
UNIQUE (name2)
);
If you already have the table, use ALTER TABLE instead.
When trying to insert a row with value of name2 that is already in a table, an error will be raised.
Yes it can be set by constraint. You can have foreign keys and unique keys on the same field. The unique constraint will guarantee there is only one record with one reference to the primary table (from that foreign table)

How can I make two varchar fields in a table a primary key?

MySQL
Engine: InnoDB
I have a table, and I need two strings as primary key.
The reason for this is because I want ON DUPLICATE KEY UPDATE depended on those two strings.
How do I do that?
Ty in advance.
You should not use them as primary keys. Use them as UNIQUE indices instead, and ON DUPLICATE KEY UPDATE will still work. I would advise that you set your primary key to be a single integer type column that is completely independent of your data.
Have a look at this example: http://sqlfiddle.com/#!2/41f2d/1
you can alter table and define that two columns as primary key,
ALTER TABLE myTable ADD PRIMARY KEY (colA, colB);
or during creation of the table
CREATE TABLE myTable
(
colA INT,
colB INT,
-- other fields here, ....
CONSTRAINT tb_pk PRIMARY KEY (colA, colB)
)

MySQL Grouping rows by a shared ID

Say I have
ID | PRODUCT
1 | Apples
1 | Oranges
1 | Bananas
2 | Walnuts
2 | Almonds
3 | Steak
3 | Chicken
Is this possible to have this type of setup in MySQL? I created a test table, and made an ID column with primary index and auto incrementing. When I try to insert a couple rows all having the same ID, mysql returns an error.
Is this possible to do in mysql?
How did the duplicate records (ID) INSERTED when you set ID as Primary Key? Basically, it will not. Primary Keys are UNIQUE. If you want records to be like that, make another column which served as your primary key
CREATE TABLE sampleTable
(
ID INT NOT NULL AUTO_INCREMENT,
GROUP_ID INT NOT NULL,
PRODUCT VARCHAR(25),
CONSTRAINT pk_name PRIMARY KEY (ID),
CONSTRAINT uq_name UNIQUE (GROUP_ID, PRODUCT)
)
a UNIQUE constraint was added so to avoid duplicated rows.
That is possible, but it is not normalized as you have a repeating primary key. Primary keys must be unique, which means 1 can only occur once. If you have custom ID's for each product, then either use a compound primary key (id, product) or a surrogate key. A surrogate key would be an auto incrementing column that uniquely identifies the row. Your table would then look like this:
CREATE TABLE fruits (
auto_id int AUTO_INCREMENT PRIMARY KEY,
id int,
product varchar(15))
You mustn't create a primary key for the ID column, as this implies that it's unique.
Just take a normal index for your example.
On the other hand, in most cases, it makes sense to have a primary key, so I'd recommend adding a 3rd row (GENRE?) with a normal INDEX on it and leaving the primary key as it is.
When inserting data, just insert the GENRE and the PRODUCT, the ID will be automatically filled.