Structured query langauge (SQL) Flashcards
What is SQL?
‘Structured Query Language’, that can do data definition, management, access control, and queries as well as control flow.
Is SQL declarative or procedural?
It is declarative even though it also includes some procedural elements.
How many values does BOOLEAN have
3:
TRUE, FALSE, UNKOWN (accounts for null)
How can you check if a value is null in SQL?
IS NULL and IS NOT NULL
How do you check the length of a char in a column?
SELECT CHAR_LENGTH(“Philiong”) AS LengthOfString;
which methods can you use when dealing with dates?
CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP
How do you specify that an attribute may not be null?
add the: NOT NULL constraint
How do you define a default value to an attribute?
when you define an attribute add the clause DEFAULT to the attribute definition.
What value will an attribute be given if no value or default is provided?
NULL
How can you make sure that an added value is between a certain range?
(When declaring an attribute for a table with a statement)
add the CHECK keyword to a definition like this : age INT NOT NULL CHECK (age > 0 AND age < 121)
What is the default action that SQL uses for referential integrity?
NO ACTION
What does the NO ACTION referential integrity do
It rejects the operation if it will cause a violation
How can you get around the NO ACTION rejection?
You can attach a triggered action on any foreign key constraint like SET NULL, CASCADE and SET DEFAULT that can trigger on the ON DELETE or ON UPDATE
What is an ASSERTION?
It is a check that can involve multiple tables.
e.g: CREATE ASSERTION MaxNumberOfEmployee CHECK (NOT EXISTS (SELECT Eid FROM Employee GROUP BY Eid HAVING COUNT(*) > 20))
What does the schema do in a database system?
It defines tables, relationships and the fields of the tables.
What is the syntax for creating a database?
CREATE DATABASE IF NOT EXISTS database_name
What is the syntax for creating tables?
CREATE TABLE Employee
What is the syntax for deleting tables?
DROP TABLE IF EXISTS Employee
What is the syntax for changing tables?
ALTER TABLE Employee
ADD last_name VarChar(20 NOT NULL)
What are the commands for
- retrieving data
- inserting data
- updating data
- deleting data
- select
- insert
- update
- delete
What is the syntax for retrieving data?
SELECT (DISTINCT | ALL ) ( * | Attribute) (AS newName)
FROM tableName
WHERE condition
How can you make aliases in SQL?
SELECT Name AS FirstName
FROM Employee e
How can you insert?
INSERT is used to add a single row to a table
INSERT INTO EMPLOYEE (Fname, Ssn) VALUES (‘Andrea’, ‘1234’);
How can you delete?
DELETE FROM table_name WHERE
only DELETE FROM table_name all records will be deleted