DB Lec4
DB Lec4
SQL
Table List
sailors
We will use these instances of
the
sailors and reservation
relations in our examples.
sailors1
reservation
Logical Operators
• OR returns:
– True -- if either operand evaluates to true
– False -- if both operands evaluate to false
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
SQL IN Example
• Character (String)
CHAR [(length)]
CHARACTER [(length)]
VARCHAR [(length)]
• Fixed point types have a decimal precision (total number of digits) and scale
(which cannot exceed the precision). The default scale is 0. NUMERIC scales must
be represented exactly.
• DECIMAL values can be stored internally with a larger scale (implementation
defined).
• FLOAT [(precision)]
REAL
DOUBLE
• The floating point types have a binary precision (maximum significant binary
digits).
• Precision values are implementation dependent for REAL and DOUBLE, although
the standard states that the default precision for DOUBLE must be larger than for
REAL.
• FLOAT also uses an implementation defined default for precision (commonly this is
the same as for REAL), but the binary precision for FLOAT can be explicit.
Data Type
• Datetime
DATE
TIME [(scale)] [WITH TIME ZONE]
TIMESTAMP [(scale)] [WITH TIME ZONE] TIME and
CREATE TABLE s
(sno VARCHAR(5) NOT NULL PRIMARY KEY,
name VARCHAR(16),
city VARCHAR(16)
)
CREATE TABLE p
(pno VARCHAR(5) NOT NULL PRIMARY KEY,
descr VARCHAR(16),
color VARCHAR(8)
)
CREATE TABLE sp
(sno VARCHAR(5) NOT NULL REFERENCES s,
pno VARCHAR(5) NOT NULL REFERENCES p,
qty INT,
PRIMARY KEY (sno, pno)
)
CREATE TABLE Statement
Create for sp with a constraint that the qty column
can't be negative:
CREATE TABLE sp
(sno VARCHAR(5) NOT NULL REFERENCES s,
pno VARCHAR(5) NOT NULL REFERENCES p,
qty INT CHECK (qty >= 0),
PRIMARY KEY (sno, pno)
)
CREATE VIEW Statement
The CREATE VIEW statement creates a new database view.
A view is effectively a SQL query stored in the catalog.
Or
• http://www.w3schools.com/sql/default.asp
• http://www.tizag.com/sqlTutorial/