Unit 5 - SQL (Part 1) Flashcards
What is SQL?
it’s a query language (standard query language)
used to communicate with a RDBMS (relational database management system) = mySQL, Oracle, Postgres
1st step in SQL
CREATE DATABASE database_name;
DROP DATABASE database_name; (to delete)
Build a SQL table from the following schema:
Students (Sid, name, login, age, gpa, DOB)
CREATE TABLE Students
(Sid VARCHAR (20),
name VARCHAR (30),
login VARCHAR (20),
age INTEGER,
gpa: DECIMAL (2, 1),
DOB: DATE
);
What else can you add to a table when creating it (optional)?
Constraints and checks
What are some examples of constraints that you can use?
NOT NULL and Default value
e.g.,
CREATE TABLE Students (
Sid VARCHAR (20) NOT NULL,
name VARCHAR (30) NOT NULL,
login VARCHAR (20) DEFAULT ‘no email’,
age INTEGER, DEFAULT 18
);
What is the syntax of ‘check’ and what are some examples of checks you can use?
Syntax is CHECK (Boolean_expression)
CREATE TABLE Students(
Sid VARCHAR (20) NOT NULL,
name VARCHAR (30) NOT NULL,
age INTEGER DEFAULT 18,
gpa: DECIMAL (2, 1),
CHECK (Age >= 18),
CHECK (gpa >= 0.0 and gpa <=4.0)
);
How do you add a primary key to a table?
Each table MUST have a primary key, add in ‘create table’ –
CREATE TABLE Students(
Sid VARCHAR (20) NOT NULL,
name VARCHAR (30) NOT NULL,
age INTEGER DEFAULT 18,
gpa: DECIMAL (2, 1),
PRIMARY KEY (Sid)
);
Can also have as a LIST of key attributes
How do you reference a foreign key? e.g., create table Boat, which has a primary key ‘Bid’ and another key ‘Sailor ID’ which references ‘Sid’ in table Sailor.
CREATE TABLE Boat(
Bid VARCHAR(20),
…
SailorID VARCHAR(20)
PRIMARY KEY (Bid)
FOREIGN KEY (SailorID) REFERENCES Sailor(Sid)
After creating a table, how do you insert records?
Two ways:
INSERT
INTO table_name
VALUES (value 1, value 2,…, value n);
^^ values are in the same order as headers (can add multiple values, just separate paranetheses with comma)
INSERT
INTO table_name (Attribute 1, Attribute 2,… Attribute n)
VALUES (value 1, value 2,… value n);
^ you can specify which attributes you’re adding to; attributes not specified will have default value or NULL (if allowed)
How do you update value of attributes after adding them?
UPDATE table_name
SET attribute 1 = newvalue1, attribute2= newvalue2,…
WHERE condition;
^ where is optional, tells the system WHICH RECORDS to apply the update query; if no condition, all records will be updated
How to delete records from a table
DELETE
FROM table_name;
^ deletes all records
DELETE
FROM table_name;
WHERE condition or conditions;
^ specify which records to apply the delete query
What’s the difference between DELETE, TRUNCATE and DROP?
TRUNCATE TABLE table name;
deletes all records from the table (but leaves the table itself)
DELETE
FROM table_name;
also deletes all records from the table (but leaves the table)
DROP TABLE table_name;
deletes the table itself
How do you update columns?
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE table_name
DROP COLUMN column_name;
What’s the basic structure of an SQL query? What is the MINIMUM structure of a query?
SELECT attributes <– project operator in relational algebra
FROM relations <– which table/tables to pull from
a query MUST contain SELECT and FROM
How to display ALL columns/ attributes in the result?
Asterisk (aka wildcard)
SELECT *
FROM Students;
This will show all rows (attributes) and columns (records/tuples)