Section 4 - Inserting Data Flashcards
Write syntax for INSERTing two names and ages into a table named cats.
The column names are and .
INSERT INTO cats(name, age)
VALUES (‘Dobby’, 7),
(‘Panther’, 10);
What is the name of the command you use to put data into tables?
INSERT
When you INSERT data into a table, does the order of the data matter?
YES
Why does the order of the data matter when INSERT-ing data into a table.
The data needs to match the order of the column you’re inserting the data into.
Essentially, you would be attempting to INSERT data into incorrect columns.
For example, if you were inserting data into a VARCHAR column named and into a INT column named , if you tried to insert 31 and ‘Chad’, into the columns in that order, you would return an error.
Why would you return an error? Because you’d be trying to insert text into an INT column.
You would also not only be putting the age into the wrong column, but it would also be inserted as the wrong data type, because it would be read by the system as a string/VARCHAR/letters/characters, and not as a number.
Create a table called employees.
Have a column for first name and a column for years worked.
CREATE TABLE employees(
first_name VARCHAR(100),
years_worked INT
);
What is the general command for seeing the data we have in a table.
Eg, how do we check out data?
Use the SELECT command.
What does the SELECT command do?
This allows us to see the data we have in our table.
What would be our command for seeing all of the data we have in a table called sports_teams?
SELECT * FROM sports_teams;
What would this command do?
SELECT * FROM world_record_holders;
This would show you all the data held in the table named world_record_holders.
Can you insert your data in a different order than the order of the columns in the table?
YES
For instance, if you had a cats table, with a breed column and an average_life _span column, in that order, you could insert data in the opposite order.
When setting up the command, you would simply write it as INSERT INTO cats(average_life_span, breed) rather than writing breed and average_life_span in the opposite order.
What is the syntax for bulk (more than 1) values into a table?
Write an example for inserting two dogs into a table named dogs, with columns named and .
INSERT INTO dogs(name, breed)
VALUES(‘Joe’, ‘lapdog’),
(‘Good Boy’, ‘golden doodle’);
Write syntax for creating a table named , which includes columns named , , and .
CREATE TABLE people( first_name VARCHAR(30), last_name VARCHAR(50), age INT );
How can you use quotes inside of an inserted value?
1) Escape the quotes with a backslash. For example:
“This text has "quotes" in it.
OR
“This text has 'quotes' in it.
2) You can also alternate single and double quotes. For example:
‘This text has “quotes” in it.’
OR
“This text has ‘quotes’ in it.”
Pretend we’re inserting the below text. In this example, what is this particular syntax being used for? / What is this syntax doing?
‘What is happening in “this” instance.’
We are alternating single and double quotes in order to be able to use quotation marks inside of a value insertion.
For example, maybe we want to insert the last name of O’Connel into our last_name column, but we don’t want the apostrophe/quote mark in O’Connel to make us return a synax error.
What is the command for seeing Warnings?
SHOW WARNINGS;
What does the SHOW WARNINGS command do?
Shows any warnings, such as if the data is truncated, we inserted the wrong type of data in a particular column, etc.
Can we SHOW WARNINGS anytime?
NO.
You can only show a warning immediately after the warning is noted. If you perform another action before using the SHOW WARNINGS command, you’ll return “empty set”.
What does NULL mean?
It means that the value of the entry is not known.
If the value of an entry is not known, what is it?
It is NULL.
TRUE or FALSE:
NULL means the value equals zero.
FALSE.
This is false false false false false. VERY FALSE!
If we our table doesn’t allow a value to be NULL, what will happen if you try to insert data that’s NULL?
The data would not be inserted, and you would return an error.
What is the command from preventing NULL values from being inserted in our tables/databases?
NOT NULL
Create a table named cars, with one column of variable character length, that cannot be null.
CREATE TABLE cars(
type_of_car VARCHAR(100) NOT NULL
);
At what point do you specify whether a field can be NULL?
When you setup the table initially.