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
SELECT columns FROM table;To define a variable in SQL
SELECT COUNT(*) AS n FROM table;Insert into table
INSERT INTO table (column, ...) VALUES(value, ...);Delete the whole table
DELETE FROM table;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:
Create a joint/associative table:
Delete a table:
Insert:
Delete:
Show basic information about a table (Usually the column information):
Show all the tables in the database:
Order the result you see:
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:
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 KEYIn SQL, it is usally to define the
PRIMARY KEYcolumn 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 ASCwill first sort the table bycolumn1in descending order and then bycolumn2in ascending order.JOINmust appear beforeWHERE. For example,
We can use
INTERSECTto get the intersection of two columns. For example,
In
WHEREstatement, you can specify more than one condition and use theANDorORto 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_callstable, the unit ofdurationis seconds.
Divide and Conquer
Useful Snippts
Will update at the end of this course. My log.sql is quite long
Last updated