Databases Flashcards
How do you add a row to a SQL table?
insert into “table-name” (“columns”…)
values ( ‘value’)
What is a tuple?
A single row of a table, which contains a single record for that relation.
How do you add multiple rows to a SQL table at once?
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
How do you get back the row being inserted into a table without a separate select statement?
adding returning *; at the end
How do you update rows in a database table?
UPDATE table_name
SET some_column = some_value
WHERE some_column = some_value;
Why its important to include a where clause in your update statements?
you might update every row
How do you delete rows from a database table?
DELETE FROM students
WHERE enrolled_status = ‘not_current’;
Optional
ALTER TABLE students
DROP COLUMN history;
How do you accidentally delete all rows from a table?
By typing delete from *
What is SQL and how is it different from languages like JavaScript?
SQL (Structured Query Language) is design to managing and manipulating data. It’s not a programing language.
How do you retrieve specific columns from a database table?
using SELECT “specific-column”
FROM “table-name”
How do you filter rows based on some specific criteria?
Using where clause.
SELECT column1, column2, …
FROM table_name
WHERE condition;
What are the benefits of formatting your SQL?
everything looks “clearer”, code is much easier to read
What are four comparison operators that can be used in a where clause?
,= ,!= , <=, >=
How do you limit the number of rows returned in a result set?
Using limit clause
How do you retrieve all columns from a database table?
Using asterisk *
How do you control the sort order of a result set?
Using order by clause.
SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, … ASC|DESC;
What is a foreign key?
Foreign key is a link in one table that refers to the primary key in another table .
How do you join two SQL tables?
Using join clause followed by table-name
How do you temporarily rename columns or tables in a SQL statement?
Using alias e.g “a”.”table-name”
What are some examples of aggregate functions?
avg(), count(), min(), max(), sum(),
What is the purpose of a group by clause?
The purpose of a group by clause is to group results by one or more column.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
What is PostgreSQL and what are some alternative relational databases?
Open Source relational database management system that store data and provides access to data points that are related to one another. Alternative are NoSQL databases which store data using mechanism different than tabula relationship for example using data structure key=value pairs
What is one way to see if PostgreSQL is running?
Command sudo service postgresql status
What is database schema?
It’s a collection of tables, it defines how data in relational database should be organized.