In MySQL, you can create relationships between tables using foreign keys. Foreign keys establish a link between two tables based on a column or set of columns, typically primary key columns in one table and corresponding columns in another table. Here's how you can create relationships in MySQL:
Let's say you have two tables: orders and order_details, where each order can have multiple order details.
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE, -- Other columns ); CREATE TABLE order_details ( detail_id INT PRIMARY KEY, order_id INT, product_id INT, quantity INT, -- Other columns FOREIGN KEY (order_id) REFERENCES orders(order_id) );
In this example:
orders table has an order_id column as the primary key.order_details table has a detail_id column as the primary key and an order_id column to establish the relationship with the orders table.FOREIGN KEY constraint in the order_details table references the order_id column in the orders table.Let's say you have two tables: employees and employee_details, where each employee has one corresponding employee detail record.
CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), -- Other columns ); CREATE TABLE employee_details ( detail_id INT PRIMARY KEY, employee_id INT UNIQUE, address VARCHAR(100), email VARCHAR(100), -- Other columns FOREIGN KEY (employee_id) REFERENCES employees(employee_id) );
In this example:
employees table has an employee_id column as the primary key.employee_details table has a detail_id column as the primary key and an employee_id column to establish the relationship with the employees table. The employee_id column is also marked as UNIQUE to enforce a one-to-one relationship.FOREIGN KEY constraint in the employee_details table references the employee_id column in the employees table.How to create a primary key in MySQL?
ALTER TABLE your_table ADD PRIMARY KEY (column_name);
How to create a foreign key in MySQL?
ALTER TABLE child_table ADD CONSTRAINT fk_name FOREIGN KEY (parent_column) REFERENCES parent_table (parent_column);
How to create a composite primary key in MySQL?
ALTER TABLE your_table ADD PRIMARY KEY (column1, column2);
How to add a foreign key constraint using ALTER TABLE in MySQL?
ALTER TABLE child_table ADD CONSTRAINT fk_name FOREIGN KEY (parent_column) REFERENCES parent_table (parent_column);
How to create a unique key constraint in MySQL?
ALTER TABLE your_table ADD CONSTRAINT uk_name UNIQUE (column_name);
How to drop a primary key constraint in MySQL?
ALTER TABLE your_table DROP PRIMARY KEY;
How to drop a foreign key constraint in MySQL?
ALTER TABLE child_table DROP FOREIGN KEY fk_name;
How to disable and enable foreign key constraints in MySQL?
-- Disable SET foreign_key_checks = 0; -- Enable SET foreign_key_checks = 1;
How to rename a column in MySQL?
ALTER TABLE your_table CHANGE old_column new_column datatype;
How to create an index on a column in MySQL?
CREATE INDEX idx_name ON your_table (column_name);
thymeleaf uipickerview pose-estimation ajax eslint-config-airbnb android-tabhost milliseconds smartcard-reader ios8.1 uicontrol