I want to restrict one column in `orders` MySQL table to primary key from **2 different tables**.
Example:
I have one table orders
|order_id|item_1 |item_2 |price|
|1 |service_id1|product_id1|10.00|
|2 |product_id1|service_id1|10.00|
Columns item_1 and item_2 are basic indexes and I want to restrict each of them to following tables:
services
|service_id |name |price|
|service_id1|service1|5.00 |
|service_id2|service2|5.00 |
products
|product_id |name |price|
|product_id1|product1|5.00 |
|product_id2|product2|5.00 |
Basically i just want to insert data from both services and products into one column: orders.item_1 or orders.item_2. Is it possible? I added both name restrictions in phpmyadmin but it doesn't work like it suppose to work, generating error 1452. Maybe I'm overthinking and the solution is easier than I think or I just want too much from mysql.
This is only a foreign key constraint
Joining both in a single column makes no sense, but is doable
what you essentially have is a bridge taböe, to join different ids from different tables. As the ids are indexed this is ver fast, especially when you are looking for few items .
in the sample you need to have in both tables a 1 to add to orders a 1 as reference, and so you always must have products and services in sync with each other
CREATE tABLE products (product_id bigint Primary KEY auto_increment, name VARCHAR(255),price DECIMAL (10,2))
CREATE tABLE services (sercvice_id bigint Primary KEY auto_increment, name VARCHAR(255),price DECIMAL (10,2))
CREATE TABLe orders (order_id BIGINT AUTO_INCREMENT PRIMARY KEY
,item_1 BIGINT
, item_2 BIGINT
, price DECIMAL(10,2)
,
CONSTRAINT FK_OrdersProducts FOREIGN KEY (item_1)
REFERENCES products(product_id),
CONSTRAINT FK_OrdersServices FOREIGN KEY (item_2)
REFERENCES services(sercvice_id)
, INDEX (item_1,item_2)
)
db<>fiddle here
CREATE tABLE products (product_id bigint Primary KEY auto_increment, name VARCHAR(255),price DECIMAL (10,2))
CREATE tABLE services (sercvice_id bigint Primary KEY auto_increment, name VARCHAR(255),price DECIMAL (10,2))
CREATE TABLe orders (order_id BIGINT AUTO_INCREMENT PRIMARY KEY
,item_1 BIGINT
, price DECIMAL(10,2)
,
CONSTRAINT FK_OrdersProducts FOREIGN KEY (item_1)
REFERENCES products(product_id),
CONSTRAINT FK_OrdersServices FOREIGN KEY (item_1)
REFERENCES services(sercvice_id)
)
db<>fiddle here
Related
im trying to create a products table with a primary and foreign key with this.
...
here is the code that i have tried so far
CREATE TABLE products
(
prod_id int NOT NULL,
prod_name int NOT NULL,
price varchar(15)
5on_hand varchar(15),
supp_id varchar(20),
PRIMARY KEY (prod_id),
FOREIGN KEY (supp_id)
);
any help would be greatly appreciated
A foreign key is a reference to a field in another table. To specify a foreign key, you have to specify what field(s) it refers to. The database cannot guess this.
So, guessing that supp_id refers to the id in the Suppliers table, your foreign key clause should look like this:
FOREIGN KEY (supp_id) REFERENCES Supplier(id)
I would strongly encourage the following:
All tables have an primary key column that is an integer and auto-incremented.
Names should be character strings.
Prices should be decimals.
So, I'm thinking something like this is appropriate:
CREATE TABLE Products (
ProductId int NOT NULL auto_increment primary key,
Name varchar(255) NOT NULL,
Price decimal(19, 4),
OnHand integer, -- assuming this is quantity on-hand
SupplierId int,
FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
);
Hello I'm new to MySQL but I would like to create a unique ID based on two primary keys created in two other tables.
Here is just the part that interests us:
CREATE TABLE patient(
id_patient int NOT NULL AUTO_INCREMENT,
...,
PRIMARY KEY (id_patient))
CREATE TABLE surgeon(
id_surgeonint NOT NULL AUTO_INCREMENT,
...,
PRIMARY KEY (id_surgeon))
CREATE TABLE case(
id_patient int NOT NULL,
id_surgeon int NOT NULL,
CONSTRAINT FK_case_patient FOREIGN KEY (id_patient) REFERENCES patient(id_patient),
CONSTRAINT FK_case_surgeon FOREIGN KEY (id_surgeon) REFERENCES surgeon(id_surgeon),
CONSTRAINT id_case PRIMARY KEY (id_patient, `_`,id_surgeon));
I think i don't get the trick. I wanted the following result :
SELECT * FROM case;
id_case | id_patient | id_surgeon
32_56 | 32 | 56
18_66 | 18 | 66
I know that the speed of calculation will not be optimal but this id-case is really necessary and must be visible during the select
You can create a computed column, and if you really want its values on disk you can persist the computed column
CREATE TABLE case(
id_patient int NOT NULL,
id_surgeon int NOT NULL,
id_case varchar(30) AS CONCAT(id_patient, '_', id_surgeon),
CONSTRAINT FK_case_patient FOREIGN KEY (id_patient) REFERENCES patient(id_patient),
CONSTRAINT FK_case_surgeon FOREIGN KEY (id_surgeon) REFERENCES surgeon(id_surgeon),
CONSTRAINT pk_case PRIMARY KEY (id_patient,id_surgeon));
However unless you have the requirement that the pair (patient, surgeon) uniquely identifies a case you should consider adding in an extra field (e.g surgery date)
I'm trying to create a table that has a composite PK constraint using Rep_ID, Store_ID, and Quarter and I'm trying to create a FK constraint on Rep_ID and Store_ID
This is my statement:
CREATE TABLE REP_CONTRACTS(
Store_ID INT(8),
Name INT(5),
Quarter CHAR(3),
Rep_ID INT(5),
PRIMARY KEY (Rep_ID, Store_ID, Quarter),
Rep_ID INT REFERENCES BOOK_STORES(Rep_ID),
Store_ID INT REFERENCES BOOK_STORES(Store_ID)
);
These are my tables:
Book Stores:
Column Name Datatype Constraint Comments
Store_ID INT(8) PRIMARY KEY column
Name VARCHAR(30) Should be UNIQUE and NOT NULL
Contact VARCHAR(20)
Rep_ID INT(5)
Rep Contracts
Column Name DataType
Store_ID INT(8)
Name INT(5)
Quarter CHAR(3)
Rep_ID INT(5)
I have already created the book store table, I'm trying to create the rep contracts table
I also get the error Duplicate column name 'Rep_ID'. Add a differentiating column alias. when running this query
you are declaring REPID twice in the table, which is why you are getting the duplication error. You may also want to create the column "Store ID" before using it in the Primary Key Statement.
CREATE TABLE REP_CONTRACTS(
Store_ID INT(8),
Name INT(5),
Quarter CHAR(3),
Rep_ID INT(5) REFERENCES BOOK_STORES(Rep_ID),
Store_ID INT REFERENCES BOOK_STORES(Store_ID),
PRIMARY KEY (Rep_ID, Store_ID, Quarter)
);
I something like this valid?
These are the columns in my ACCOUNTS table:
user_id (Primary Key)
user_name
user_pass
user_email
user_date
Accounts are allowed to have multiple characters. Heres how i want to write that table:
CHARACTERS table:
character_ID
user_ID (foreign key)
character_name
...(random columns)
Can i make my PRIMARY KEY (character_ID, user_ID)?
Or is there a better way to distinguish this?
EDIT: here is how i would create the table:
CREATE TABLE characters(
-> char_id INT(8) NOT NULL AUTO_INCREMENT,
-> char_name VARCHAR(50) NOT NULL,
-> user_id INT(8)
-> PRIMARY KEY(char_id, user_id),
-> FOREIGN KEY (user_id) REFERENCES users(user_id)
-> )
-> ;
Yes you can make both (char_id, user_id) as primary key in characters table and
then it becomes a composite key but foreign key allowed to have duplicates, because of which
they may not be best to use as a Primary Key.
So generally it's recommended and best practice to use a field that can uniquely
determine all other fields in a record and designate that as primary key.
It can be a synthetic one like (auto_increment in MySQL, IDENTITY in SQL Server).
or it can be normal/general like "Social Security Number" etc.
So, in your case if char_id can alone uniquely determine all other fields then I would make
it as PK char_id INT NOT NULL AUTO_INCREMENT primary key
In MySQL I have these 3 tables :
CREATE TABLE IF NOT EXISTS Seasons
(
season_id INT NOT NULL AUTO_INCREMENT,
start_date DATE,
end_date DATE,
club_num INT,
desc TEXT,
PRIMARY KEY(season_id)
);
ALTER TABLE Seasons AUTO_INCREMENT=10000;
CREATE TABLE IF NOT EXISTS Clubs
(
club_id INT NOT NULL AUTO_INCREMENT,
club_name VARCHAR(70),
PRIMARY KEY(club_id)
);
ALTER TABLE Clubs AUTO_INCREMENT=100000;
CREATE TABLE IF NOT EXISTS ClubsCloths
(
season_id INT NOT NULL,
club_id INT NOT NULL,
first_shirt VARCHAR(50),
second_shirt VARCHAR(50),
PRIMARY KEY(season_id,club_id),
FOREIGN KEY (season_id) REFERENCES Seasons(season_id),
FOREIGN KEY (club_id) REFERENCES Clubs(club_id)
);
In the last one I have 2 foreign keys that reference to first and second table. Now I want to know is it wisely to have 2 foreign key in a one table ?
thanks
It is normal. The ClubsCloths table is used to support many-to-many relationship between Seasons and Clubs.
It's perfectly normal to have several foreign keys to different tables (or the same table, doesn't matter).