CONCATenate columns to a surrogate key
Another strategy to add a surrogate key to an existing table is to concatenate existing columns with the CONCAT()
function.
Let's think of the following example table:
CREATE TABLE cars (
make varchar(64) NOT NULL,
model varchar(64) NOT NULL,
mpg integer NOT NULL
)
The table is populated with 10 rows of completely fictional data.
Unfortunately, the table doesn't have a primary key yet. None of the columns consists of only unique values, so some columns can be combined to form a key.
In the course of the following exercises, you will combine make
and model
into such a surrogate key.
This exercise is part of the course
Introduction to Relational Databases in SQL
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Count the number of distinct rows with columns make, model
___
FROM cars;