| transaction_id | amount | customer_id |
|---|---|---|
| primary key | foreign key | |
| 1000 | 4.99 | 3 |
| 1001 | 2.86 | 2 |
| 1002 | 3.33 | 3 |
| 1003 | 2.15 | 1 |
| customer_id | name |
|---|---|
| primary key | |
| 1 | Hans |
| 2 | John |
| 3 | Laila |
CREATE TABLE customers {
customer_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR (255)
);
INSERT INTO customers (name)
VALUES ("Hans"),
("John"),
("Laila");
SELECT * FROM customers;| customer_id | name |
|---|---|
| 1 | Hans |
| 2 | John |
| 3 | Laila |
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY AUTO_INCREMENT,
amount DECIMAL (5, 2),
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
SELECT * FROM transactions;
| transaction_id | amount | customer_id |
|---|
transactions_ibfk_1ALTER TABLE transactions
DROP FOREIGN KEY transactions_ibfk_1;ALTER TABLE transactions
ADD CONSTRAINT fk_customer_id
FOREIGN KEY(customer_id) REFERENCES customers (customer_id);
DELETE FROM transactions;
SELECT * FROM transactions;ALTER TABLE transactions AUTO_INCREMENT = 1000;INSERT INTO transaction(amount, customer_id)
VALUES (4.99, 3)
(2.86, 2)
(3.33, 3)
(2.15, 1);
SELECT * FROM transaction;DELETE FROM customers WHERE customer_id = 3;