Week 7 - SQL
Welcome to CS50! This is my review for Week 7's content about SQL.
Lecture
In SQL, we have a CRUD paradigm, where
C stands for CREATE, sometimes includes INSERT
R stands for READ
U stands for UPDATE
D stands for DELETE, sometimes includes DROP
In SQL, we use the tradition that all the key words, like SELECT..., should be captalized. This is just to make our code easy to look at. Not captalizing the key words won't affect the execution of our SQL code.
Show the selected columns from the table in the terminal
To define a variable in SQL
Insert into table
Delete the whole table
This command will delete the whole table, which sometimes may cause horror story to happen.
7. An Interesting SQL attack: SQL Injection Attack Hackers will use the property of the comment (--) in SQL to hack into the user's account without knowing the password.
If the hacker input malan@harvard'--
as the username
In this case, the hacker doesn’t need to know the password but he can enter the account. To avoid this problem, we should use the code below:
Section
Some basic jargons in Database:
Database: A collection of data organized for creating, reading, updating, and deleting. (This is CRUD principle)
Database Management System (DBMS): Software via which you can interact with a data base. And below are some examples:
MySQL
Oracle
PostgreSQL
SQLite ...
SQL: A language via which you can create, read, update, and delete data in a database.
Design Principles for a database:
Create one table for each entity in your dataset. For example, you should create two tables for the books and authors.
All tables should have a primary key. This is a unique id that differentiate one row from every other row.
The information in the table should depend on the primary key only.
Basic SQL Commands:
Create a table:
In this code, column0
and so on is just the name for one specific column. Remember that you should also define the PRIMARY KEY for this table, which abides by our designing principle stated above.
Create a joint/associative table:
In this code, column0
and column1
are the foreign keys that reference to the primary keys in other tables.
Delete a table:
Insert:
When inserting, you don’t need to include the id
/PRIMARY KEY
column.
Delete:
Note that WHERE
is usaully used to filter rows. It should appear right after you SELECT
your columns.
Show basic information about a table (Usually the column information):
Show all the tables in the database:
Order the result you see:
Note that ORDER BY
is usually at the end of the statement. You can specify the ordering by add DESC
(descending) and ASC
(ascending) right after the column_name
after ORDER BY
.
Aggregate functions:
COUNT(*)
: Count the number of rows in the table.SUM(column_name)
: Sum the values in the column.MAX(column_name)
: Find the maximum value in the column.MIN(column_name)
: Find the minimum value in the column.AVG(column_name)
: Find the average value in the column.Usage:
This will output a number, which is the average of a specifc column from the table.
Shorts
All our queries will refer to rows of the table since we specify all of the columns in that table.
In a joint table, it is okay to establish a joint primary key - a combination of two columns that is always guaranteed to be unique. Otherwise, there will be duplicates if we only specify one column as the
PRIMARY KEY
In SQL, it is usally to define the
PRIMARY KEY
column asINTEGER
. Otherwise, you need to configure that column to autoincrement.
Problem Set 7
Nothing more to note down.
In
ORDER BY
, we can specify more than one parameters and its order matters. For example,ORDER BY column1 DESC, column2 ASC
will first sort the table bycolumn1
in descending order and then bycolumn2
in ascending order.JOIN
must appear beforeWHERE
. For example,
We can use
INTERSECT
to get the intersection of two columns. For example,
In
WHERE
statement, you can specify more than one condition and use theAND
orOR
to connect them. For example,
In SQL, we can use
%
to match any string. For example,'Harry Potter%'
will match any string that starts with'Harry Potter'
.
Things to notice in the problem statement
In the
phone_calls
table, the unit ofduration
is seconds.
Divide and Conquer
Useful Snippts
Will update at the end of this course. My log.sql
is quite long
Last updated