SQL Flashcards
SQL Definition
High Level, declarative programming language
Specifies what the user wants, not how
Terms:
Table is a:
Row is a:
Column is a:
Table is a: relation
Row is a: tuple
Column is an attribute:
Table Creation in SQL:
CREATE TABLE STUDENT(
Fname VARCHAR(15) NOT NULL,
SSN INT NOT NULL);
Domains:
A constant
CREATE DOMAIN SSN_TYPE AS CHAR(9);
Primary Key Declartion
CREATE TABLE STUDENT(
Fname VARCHAR(15) NOT NULL,
SSN INT NOT NULL,
PRIMARY KEY(SSN));
Unique Key Declaration
Provides alternative keys
CREATE TABLE STUDENT( Fname VARCHAR(15) NOT NULL, SSN INT NOT NULL, SID INT UNIQUE PRIMARY KEY(SSN));
Default declaration
For when a value isn’t specified:
Dno INT NOT NULL DEFAULT 3;
SQL Insert
INSERT INTO EMP VALUES(
“Bob”, “Jones”, 9995478414);
For specific attributes:
INSERT INTO EMP(Fname, Lname) VALUES(
“Bob”, “Jones”);
Anything not set is NULL
SQL Delete
DELETE FROM EMP
WHERE Fname = “Bob” AND Lname = “Smith”;
Delete entire table
DELETE FROM EMP
SQL Update
UPDATE EMP
SET Lname = “Steppers
WHERE Ssn = 4784514784;
Update more than one person
UPDATE EMP
SET Sal = Sal * 1.1
WHERE Dno = 5
SQL Join
SELECT Fname, Lname
FROM EMP, DEPT
WHERE Dname = “Research” AND Ename = “John”
Attribute Wildcard
Can use * to denote all attributes from a selected entity
SELECT *
FROM EMP
SQL UNION, INTERSECT, EXCEPT
(SQL STATEMENT)
UNION
(SQL STATEMENT)
Same goes for the intersection and except
Where Wildcard
For zero or more characters
Select Lname
FROM EMP
WHERE Lname LIKE ‘sellers.%’
For exactly one character
SELECT Lname
FROM EMP
WHERE Lname LIKE ‘sellers.18_’
Arithmetic Ops
SELECT 1.1 * SALARY AS INCREASE