SQL Flashcards
SQL
What’s the syntax to refer to both a table name and column name in a SELECT statement?
tableName.columnName
In SQLite, how do you format the output of a select statement?
output the name of each column
.headers on
now we are in column mode, enabling us to run the next two .width commands
.mode column
adjusts and normalizes column width
.width auto
customize column device-width
.width NUM1, NUM2
In SQLite, what is the syntax for selecting a row where a column value is null?
SELECT * FROM cats WHERE name IS NULL;
In SQLite, what is the syntax for inserting a row that includes null values?
INSERT INTO cats (name, age, breed) VALUES (NULL, NULL, “Tabby”);
What is the syntax to retrieve two columns from two different tables when the column names are identical?
SELECT cats.name, dogs.name FROM cats, dogs;
In SQLite, what is the syntax for inserting a row into a table?
INSERT INTO cats (name, age, breed) VALUES (‘Maru’, 3, ‘Scottish Fold’);
In SQLite, do you have to add the primary key when inserting a row into a table?
No. Primary Key columns are auto-incrementing. As long as you have defined an id column with a data type of INTEGER PRIMARY KEY, a newly inserted row’s id column will be automatically given the correct value.
In SQLite, what is the syntax for an UPDATE statement?
sqlite> UPDATE cats SET name = “Hana” WHERE name = “Hannah”;
In SQLite, what is the syntax to delete a row from a table?
sqlite> DELETE FROM cats WHERE id = 2;
What does it mean to “type” a table?
“Typing refers to adding data types to row definitions when creating a table.”
In SQLite, what is the TEXT datatype?
Any alphanumeric characters which we want to represent as plain text.
In SQLite, what is the BLOB datatype?
generally used for holding binary data
In SQLite, what are the datatypes?
TEXT, INTEGER, REAL, BLOB
In SQLite, what is the INTEGER datatype? And when do you use it?\n
Anything we want to represent as a whole number. If it’s a number and contains no letter or special characters or decimal points then we should store it as an integer. If we use it to perform math or create a comparison between two different rows in our database, then we definitely want to store it as an integer. If it’s just a number, it’s generally not a bad idea to store it as an integer. You might never add two house address numbers together, but you might want to sort them numerically. For example in the preceding case, you might want to get the biggest number and not the longest piece of text.
In SQLite, how many actual datatypes are there?
SQLite allows the programmer to use other common datatypes outside of the four mentioned above. This is why we are referring to TEXT INTEGER REAL BLOB as datatype “categories”. All other common datatypes are lumped into one of the four existing datatypes recognized by SQLite.
In SQLite, what is the REAL datatype?
Anything that’s a plain old decimal like 1.3 or 2.25. SQLite will store decimals up to 15 characters long. You can store 1.2345678912345 or 1234.5678912345, but 1.23456789123456789 would only store 1.2345678912345. In other database systems this is called ‘double precision.’
What is the Double Precision datatype?
A decimal number with up to 15 digits.
What is the syntax to execute a file in the CLI?
sqlite3 mydb.db < my_sql_file.sql
In SQLite, what command will exit out of the sqlite CLI?
.quit