8. Add
To enter our data we must use the reserved words INSERT INTO followed by the name of the table, the word VALUES and the data in parentheses.
INSERT INTO [tabla] VALUES (valor1, valor2, …), (valor1, valor2, …), …;
If we need to include the flamenco genre we would have to indicate the 2 existing columns in the Genre table (GenreId, Name).
INSERT INTO Genre VALUES (NULL, 'Flamenco');
The word NULL is to indicate that I do not want to enter anything into GenreId. It is an auto-incrementing column (the number grows each time I add a row).
Every table has a special column called Primary Key. It is a number that keeps growing and is used to tell one row apart from another.
Multiple values
If I separate the parentheses with commas, I can insert all the values I need at the same time.
INSERT INTO Genre VALUES (NULL, 'Flamenco'), (NULL, 'Ska'), (NULL, 'Rock nacional');
Specifying columns
If my table has many columns, let's say 20, indicating what will go in each one can be torture. That is exactly why SQL lets us be flexible.
INSERT INTO [tabla] (columna1, columna2, …) VALUES (valor1, valor2, …), (valor1, valor2, …), …;
Let's take the case of the Customer table. It has 13 columns.
- CustomerId
- FirstName
- LastName
- Company
- Address
- ...
If I only want to add a customer with their FirstName, LastName and Email, I should declare it with the columns I need after the name of the table.
INSERT INTO Customer (FirstName, LastName, Email) VALUES ('Mickey', 'Mouse', 'mickey@disney.com');
Do I want to add many customers at once in the same statement? Separated by commas, just like before.
INSERT INTO Customer (FirstName, LastName, Email) VALUES ('Mickey', 'Mouse', 'mickey@disney.com'), ('Pato', 'Donald', 'donald@disney.com'), ('Peter', 'pan', 'peter@disney.com');
Activity 1
- Add 5 artists you like to the
Artisttable. - Enter the
MediaTypeWav. - Create 2 records related to the artists you have created.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Desafíos de programación atemporales y multiparadigmáticos
Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.
Buy the bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.