Premium Only Content
Java Spring Boot [Mid 2024] Part 9: Relational Mappings
*******************OneToOne*******************
CREATE TABLE address (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
street VARCHAR(255),
city VARCHAR(255),
state VARCHAR(255)
);
CREATE TABLE customer (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(255),
last_name VARCHAR(255),
address_id BIGINT,
FOREIGN KEY (address_id) REFERENCES address(id)
);
INSERT INTO address (street, city, state) VALUES ('123 Main St', 'Cityville', 'CA');
INSERT INTO customer (first_name, last_name, address_id) VALUES ('John', 'Doe', 1);
*******************OneToMany*******************
ALTER TABLE address
ADD COLUMN customer_id BIGINT,
ADD CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES customer (id);
UPDATE address
SET customer_id = (SELECT id FROM customer WHERE id = address.id);
INSERT INTO customer (first_name, last_name) VALUES ('Jane', 'Smith');
INSERT INTO address (street, city, state, customer_id) VALUES
('789 Pine St', 'CityC', 'SC', 1),
('101 Elm St', 'CityD', 'SC', 2);
ALTER TABLE customer
DROP FOREIGN KEY customer_ibfk_1;
ALTER TABLE customer
DROP COLUMN address_id;
******************ManytoMany*******************
CREATE TABLE customer_address (
customer_id BIGINT,
address_id BIGINT,
PRIMARY KEY (customer_id, address_id),
FOREIGN KEY (customer_id) REFERENCES customer(id),
FOREIGN KEY (address_id) REFERENCES address(id)
);
INSERT INTO customer_address (customer_id, address_id)
SELECT customer_id, id as address_id
FROM address;
ALTER TABLE address
DROP FOREIGN KEY fk_customer;
ALTER TABLE address
DROP COLUMN customer_id;
-
12:54
Brad Owen Poker
13 hours ago $1.79 earnedI Make Final Table! I’m Going To $10,400 WPT World Championship!!
10.8K1 -
10:00
TheMightyMcClures
22 days ago $1.31 earnedWe Fed 500 Families!
12.3K4 -
6:23
The Shannon Joy Show
14 hours agoWas Covid a MILITARY OPERATION?
11.9K6 -
LIVE
The Bubba Army
22 hours agoTRUMP SIGNS EPSTEIN BILL! - Bubba the Love Sponge® Show | 11/20/25
1,709 watching -
27:24
DeVory Darkins
22 hours agoTrump secures RECORD BREAKING INVESTMENT as Charlotte Schools issue SHOCKING Update
38.5K48 -
34:15
ZeeeMedia
17 hours agoPolice Demand Government Critic Hands Over His BLOOD | Daily Pulse Ep 147
68.8K36 -
1:47:50
Side Scrollers Podcast
16 hours agoSide Scrollers Presents: OVERCOCKED
38.4K12 -
3:33:22
HLL8LLY
5 hours ago $1.47 earnedBattlefield 6 Live Gameplay
11.8K -
34:04
Code Blue Cam
20 hours agoTeen Plays Real-Life GTA… Ends Horrifically
29.3K11 -
13:33
MattMorseTV
18 hours ago $36.63 earnedThey actually TOOK THE BAIT…
88.8K71