Review of SQL server
Review of SQL server
Syntax:
CREATE DATABASE database_name
ALTER DATABASE database_name
Modify Name = new_database_name
DROP DATABASE database_name
Simple Table Declarations
[Use Database_Name]
CREATE TABLE TableName(
Attribute1 datatype [options],
Attribute2 datatype [options],
...
AttributeN datatype [options]
)
Example
Delete table:
[USE Database_Name]
DROP TABLE TableName;
Add attribute:
[USE Database_Name]
ALTER TABLE TableName
ADD
Attribute1 Datatype [Options],
Attribute2 Datatype [Options],
…;
Modifying Relation Schemas
Modify attribute:
[USE Database_Name]
ALTER TABLE TableName
ALTER COLUMN Attribute Datatype [Options];
Delete attribute:
[USE Database_Name]
ALTER TABLE TableName
DROP COLUMN Attribute1, Attribute2,…;
Declaring Keys
PRIMARY KEY:
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype PRIMARY KEY,
Attrbute2_Name DataType [OPTIONS],
…
);
Or
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],
…
PRIMARY KEY(AttributeKey1, AttributeKey2,…)
);
Or
[USE DatabaseName]
ALTER TABLE Table_Name ADD PRIMARY KEY(AttributeKey1, AttributeKey2,…);
Declaring Keys
UNIQUE KEY:
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],
Attrbute2_Name DataType [OPTIONS] UNIQUE, …);
Or
[USE DatabaseName]
CREATE TABLE Table_Name(
Attribute1_Name Datatype [OPTIONS],
Attrbute2_Name DataType [OPTIONS],
…
UNIQUE(UniqueAttribute1, UniqueAttributeKey2,…)
);
Or
[USE DatabaseName]
ALTER TABLE Table_Name ADD UNIQUE(UniqueAttribute1,
UniqueAttributeKey2,…));
Identity option
IDENTITY:
IDENTITY (seed_value, increment_value);
Seed_value: initial value
Increment_value: increment step
UPDATE:
UPDATE Table_Name
SET AttributeName = value,
[,…, Attribute_k= value_k]
[FROM ...]
[WHERE Conditions]
Example: Update command
DELETE data in table
DELETE:
DELETE From Table_Name
[FROM ...]
[WHERE Conditions]
Example: Delete command
Show the information of INVOICEs that have the amount more than
300$
Queries involving more than one relation
Capabilities of SQL SELECT Statements
SELECT *
FROM Customer A, Invoice B
WHERE A.CUST_NUM = B. CUST_NUM
SELECT *
FROM Customer A INNER JOIN Invoice B
on A. CUST_NUM = B. CUST_NUM
SET operations
Example: UNION
INTERSECT
{A ∩ B} = {a | a is in A and B}
Difference
SQL Server
Oracle
UNION & UNION ALL
STUDENT1 STUDENT2