Basic SQL Flashcards
SQL stands for
Structured Query Language
SQL is a comprehensive database language, meaning
It has statements for data definitions, queries, and updates
DDL stands for
Data Definition Language
DML stands for
Data Manipulation Language
SQL standards are divided into
Core specification PLUS specialized extensions
SQL core
supposed to be implemented by all RDBMS veodors that are SQL complaint
SQL extensions
can be implemented as optional modules to be purchased independently for specific DB applications such as data mining, spatial data, temporal data, data warehousing, online analytical processing, etc
Table row and column correspond to relational model terms
relation, tuple, attribute
CREATE statement can create
schemas, tables, domains, as well as views, assertions, triggers
SQL schema is
a DB structure
SQL schema is identified by
schema name, and includes authorization identifier to indicate the user or account who owns the schema, as well as descriptors for each element in the schema
Schema elements include
tables, constraints, views, domains, and other constructs (authorization grants) that describe the schema
How to create schema (2 ways)
CREATE schema statement that can include all schema element definitions
Can be assigned name and authorization identifier and the elements can be defined later
Create schema called COMPANY owned by user with authorization identifier Jsmith
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
catalog
collection of schemas in SQL environment
SQL environemnt
installation of an SQL-compliant RDBMS on a computer system
a catalog always contains a special schema called
INFORMATION_SCHEMA
INFORMATION_SCHEMA provides
information on all the schemas in the catalog and all the element descriptors in these schemas
CREATE TABLE command used for
specify new relation by giving it name and specifying its attributes and initial constraints
Order how attributes are specified when creating table
attribute name, data type to specify domain of values, any constraints (NOT NULL)
When are key, entity integrity, and referential integrity constraints specified
within CREATE TABLE statement after the attributes are declared or can be added later using the ALTER TABLE command
can explicitly attach schema name to relation name, separated by a period
CREATE TABLE COMPANY.EMPLOYEE rather than CREATE TABLE EMPLOYEE
base relations are created through
CREATE TABLE statements
base relations
relation and its tuples are created and stored as a file by the DBMS
virtual relations
created through CREATE VIEW statement, which may or may not correspond to an actual physical file
attributes in base table considered to be ordered
in sequence in which they are specified in the CREATE TABLE statement, but rows aren’t considered to be ordered within a relation
Example code of creating a table
CREATE TABLE DEPARTMENT ( Dnam VARCHAR(15) NOT NULL, Number INT NOT NULL, Mgr_ssn CHAR(9) NOT NULL, Mgr_start_date DATE, PRIMARY KEY (Dnumber), UNIQUE (Dname), FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE (Ssn) );
basic data types available for attributes
numeric, character, string, bit string, Boolean, date, time
Numeric datatypes include
Integer numbers: INT, and floating point (real) numbers: FLOAT, REAL, DOUBLE PRECISION), DECIMAL,
DECIMAL(i,j)… what does I and J stand for
what would DECIMAL(5,2) mean
i is the precision (total number of decimal digits)
j is the scale (number of digits after the decimal point) -default zero
3 digits before the decimal, 2 after decimal
Character string data types
CHAR, VARCHAR, CLOB
CHAR(n)
fixed length… n is the number of characters.
If value of Smith for an attribute of type CHAR(10)
it’s padded with 5 blank characters to become ‘Smith ‘
VARCHAR(n)
varying length. n is number of max characters
if str1 comes before str2 in alphabetic order it is considered
to be less than str2
concatenation of strings
‘abc’ || ‘XYZ’ results in a single string ‘abcXYZ’.
CLOB (CHARACTER LARGE OBJECT)
specify columns that have large text values, such as documents. CLOB maximum length can be specified in kilobytes (K), megabytes (M), or gigabytes (G)
CLOB(20M) specifies
maximum length of 20 megabytes
bit-string data
either of fixed length n—BIT(n)—or varying length—BIT VARYING(n), where n is the maximum number of bits
write an example of a bit string
Literal bit strings are placed between single quotes but preceded by a B to distinguish
them from character strings; for example, B‘10101’
BLOB
BINARY LARGE OBJECT
specify columns that have large binary values, such as images
can be specified in kilobits (K), megabits (M), or gigabits (G)
BLOB(30G)
specifies a maxi- mum length of 30 gigabits
Boolean
TRUE/FALSE/UNKNOWN (because SQL has 3valued logic)
DATE number of position
10
date components
YYYY-MM-DD
TIME number positions
> =8
time components
HH:MM:SS
comparison with dates and time
Literal date values represented by
DATE ‘2008-09-27’ or TIME ‘09:12:47’
Domain can be declared and domain name used with the attribute specification. show:
CREATE DOMAIN SSN_TYPE AS CHAR(9);
Can use SSN_TYPE in place of CHAR(9) when defining table
NOT NULL
may be specified if NULL is not permitted for a particular attribute
primary keys required not to
be null
How to define default value for an attribute
append the clause DEFAULT to attribute definition
default value included in any new tuple if
explicit value not provided for that tattribute
if no default clause specified
default value is null for attributes that don’t have a NOT NULL constraint
Example of setting default for attribute
Dno INT NOT NULL DEFAULT 1,
CHECK clause
can restrict attribute or domain values
CHECK clause… department numbers are restricted to integer numbers between 1 and 20
Number INT NOT NULL CHECK (Dnumber >0 AND Dnumber