VA Session 3 & 4 Flashcards
SQL
- Structured Query Language
- standard relational database language (can be used with majority of relational DBMS software tools)
SQL Use
- Create databases
- Add, modify & delete a) database structures & b) records in database
- Query databases (data retrieval)
Case sensitivity
- No: keywords, table & column names – SELECT = select = SeLeCt; but commands usually written with capital letters, table etc. names small
- yes: Values of columns with textual types
SQL Commands Categories
- Data Definition Language (DDL)
create & modify structure of database e.g. CREATE, ALTER, DROP - Data Manipulation Language (DML)
Insert, modify, delete & retrieve data e.g. INSERT TO, UPDATE, DELETE, SELECT - Data Control Language (DCL)
- Transactional Control Language (TCL)
SQL data types
- Each column of each SQL created relation has a specific data type
CHAR(n)
fixed length n-character string
VARCHAR(n)
variable length character string with max. size of n characters
NUMERIC(x,y)
number with x digitis, y of which are after decimal point
Subqueries
- queries within a larger SQL query
- Inner queries used to return information that will be use by outer query
- Enclosed by ()
- usually added within WHERE clause of outer query
- e.g. SELECT … FROM … WHERE … IN (SELECT … FROM …. WHERE…)
Joining data from several datas - ways
- 1) WHERE clause
- 2) JOIN clause (sometimes preferred due to readability)
- 3) operators as UNION or INTERSECTION
Alias
- Alternative name usable in a query instead of full name
- Pro: shorter & if 2 tables have same name
- e.g. SELECT p.productid, v.vendorname FROM product p, vendor v ;
Renaming
- renaming columns that appear in result
- e.g. p.productid pid, v.vendorname vname FROM product p, vendor v ;
Views
- Mechanism in SQL to save structure of a query in RDBMS
- virtue table <-> not actual table: if view invoked, executes a query & retrieves data from table
- virtual table whose contents are defined by a query
- used to focus, simplify, and customize the perception each user has of the database
- security mechanisms by letting users access data through the view, without granting the users permissions to directly access the underlying base tables of the view
- Like table: view consists of a set of named columns and rows of data that come from tables referenced in the query defining the view and are produced when view referenced
- acts as a filter on the underlying tables referenced in the view
Set operators
- combine results of >= 2 SELECT statements (, querying relations, views or other SELECT queries) that are union compatible (= contain same number of columns & data types of columns in sets match)
- Union, intersection & difference
Absolute value of x
ABS(x)
Length of string s
LENGTH(s)
e.g. Return products with name that has 5 characters:
SELECT *
FROM Products
WHERE LENGTH(name) = 5;
Converts string s to lowercase & uppercase
LOWER(s)
UPPER(s)
Larger & smaller value of x and y
MAX(x,y)
MIN(x,y)
Produces a random number
RANDOM()
Number x rounded to d decimal places
ROUND(x,d)
Calculating a cumulative
SUM
Null returned
there is no value returned with the query (!= 0 -> if at any point of calculation / comparison NULL, the result will be NULL)