SQL Flashcards
What is a relational database?
- Data is stored in relations (or tables) depending on grouped attributes
- each table has a schema
What is the typical format of a schema?
nameOfTable(Attribute1, Attribute2..)
- Example from the diagram above: Measurements(Tag#, Height, Weight)
In relational databases, columns are ___ and rows are ___
In relational databases, columns are attributes and rows are different entities
What does SQL stand for?
Standard query language
What is DDL and which SQL commands fall into it?
Data definition language
- Create/alter/delete databases, tables and their attributes.
What is DML and which SQL commands fall into it?
Data Manipulation Language
- Add/remove/update and query rows in tables Transact-SQL
What is the query for creating a table in a database?
CREATE tableName(attributeName attribute Type, …);
What is the query for creating a database in SQL?
CREATE DATABASE databaseName;
What are the data types in SQL?
INT, FLOAT, CHAR, VARCHAR, DATE, DATETIME, XML, BLOB
What does the UNIQUE keyword mean in SQL?
You can specify that you want some attribute (or set of attributes) to be unique, so all the values in that column must be different. Though this is not commonly used due to primary keys.
What is a primary key?
- an attribute that is unique in a table and is used to reference rows in another table
What is the SQL for a primary key?
CREATE TABLE people (name VARCHAR(20), birthday DATE, CONSTRAINT PK_people PRIMARY KEY (name));
What is a foreign key?
The primary key from another table used to reference things in this table.
- used to link two table together
What is the SQL for a foreign key?
CREATE TABLE staff (employee VARCHAR(20) , staffnum INT, CONSTRAINT FK_staff FOREIGN KEY (employee) REFERENCES people(name));
What is the SQL to delete/drop a database?
DROP DATABASE databaseName;
What is the SQL to delete/drop a table?
DROP TABLE tableName;
What is the SQL for adding an attribute to a table
ALTER TABLE people ADD email VARCHAR(30);
What is the SQL for changing an attribute to a table?
ALTER TABLE people MODIFY email VARCHAR(100)
What is the SQL for removing an attribute to a table?
ALTER TABLE people DROP COLUMN email;
What is the SQL for inserting into a table?
INSERT INTO people VALUES (‘phoebe’, 19, ‘student’)
if the schema is people(name, number, course) how do you insert into the database leaving a field empty in SQL?
INSERT INTO people(name, course) VALUES (‘Danny’, ‘G402’);
What is an SQL injection?
- A common attack where malicious users input into a form SQL commands to DROP TABLE … or INSERT INTO
What are 4 common methods for avoiding SQL injection attacks?
- Input validation ◦ Check the input is valid for the field entered
- Use prepared statements ◦ You write the query, except have some ? or similar which you can later replace with say a first name
- Stored procedures ◦ Like prepared statements, but stored in the database instead of in your programming language
- Escaping ◦ Make sure that you escape all characters users input – should be functions for this
What is the SQL command to delete from a table where the person is called John?
DELETE FROM people WHERE name = ‘John’;`
What is the SQL command to delete from a table where the person is called John?
DELETE FROM people WHERE name = ‘John’;
What are the conditions for WHERE clauses?
- comparisons: <, >, <=, >=, ==, !=
- operators: OR, AND, NOT, XOR, BETWEEN, LIKE
Give an example of an SQL statement that uses the LIKE with WHERE clause?
SELECT * FROM people WHERE name LIKE ‘O%r’;
What does the command DELETE FROM people WHERE name IN (list of names) do?
It deletes all the rows that have a name that is in that list
What is the SQL command to update a row?
UPDATE people SET course = ‘G407’ WHERE name = ‘oliver’
What is the format for querying a database?
SELECT
FROM
WHERE
GROUP BY
HAVING (same as WHERE but inside GROUP BY)
ORDER BY