0% found this document useful (0 votes)
8 views3 pages

06 Handout 1

This document provides an introduction to SQL, focusing on data definition commands such as creating and deleting databases and tables, as well as modifying table structures. It outlines various SQL data types, operators, and constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT. Additionally, it includes syntax examples for each command and constraint to aid understanding and implementation.

Uploaded by

sai.romero.coi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

06 Handout 1

This document provides an introduction to SQL, focusing on data definition commands such as creating and deleting databases and tables, as well as modifying table structures. It outlines various SQL data types, operators, and constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT. Additionally, it includes syntax examples for each command and constraint to aid understanding and implementation.

Uploaded by

sai.romero.coi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to SQL (Part I) SQL Data Definition Commands

 CREATE DATABASE – creates a new database


Fundamentals o Syntax: CREATE DATABASE database_name;
 A query is a specific request for data manipulation issued o Example: CREATE DATABASE myDB;
by the end-user or the application to the DBMS.  DROP DATABASE – deletes an existing database
 SQL: o Syntax: DROP DATABASE database_name;
o Stands for Structured Query Language o Example: DROP DATABASE myDB;
o Pronounced as S-Q-L or “sequel”  CREATE TABLE – creates a new table in a database
o Consists of commands that: o Syntax: CREATE TABLE table_name (column1 datatype, …);
 Create database and table structures o Example: CREATE TABLE Students
 Perform various types of data (StudentID varchar(11), LastName
manipulation and data administration varchar(99), FirstName varchar(99),
 Query the database to extract useful Section varchar(5));
information  DROP TABLE – deletes an existing table in a database
 Popular Database Management Tools o Syntax: DROP TABLE table_name;
o Microsoft SQL Server o Example: DROP TABLE Students;
o MySQL o To delete only the table's data:
o Oracle RDBMS  Syntax: TRUNCATE TABLE table_name;
o Microsoft Access  Example: TRUNCATE TABLE Students;
 SQL Data Types:  ALTER TABLE – Adds, deletes, or modifies columns in an
Category Common Data Types existing table
Exact numeric bigint, bit, decimal, int, money, o Syntax to add: ALTER TABLE table_name ADD column
numeric datatype;
Approximate numeric float, real o Example: ALTER TABLE Students ADD
Date and time date, datetime, time MiddleName varchar(99);
Character strings char, text, varchar o Syntax to delete: ALTER TABLE table_name
Unicode nchar, ntext, nvarchar DROP COLUMN column;
character o Example: ALTER TABLE Students DROP COLUMN
strings Section;
Binary strings binary, image, varbinary o Syntax to modify: ALTER TABLE table_name
Other data types cursor, sql_variant, ALTER COLUMN column datatype;
table, o Example: ALTER TABLE Students ALTER
 SQL Operators: COLUMN MiddleName nvarchar(99);
Category Operators
Arithmetic +, -, *, /, % SQL Constraints
Comparison =, >, <, >=, <=, <>  NOT NULL on CREATE TABLE – ensures that a column
Compound +=, -=, *=, /=, %= cannot have a NULL value upon creating a table
Logical AND, OR, NOT, LIKE, IN, o Example: CREATE TABLE Students (StudentID
BETWEEN, EXISTS, ANY, ALL varchar(11) NOT NULL, LastName varchar(99) NOT
NULL, FirstName varchar(99) NOT NULL, Section
varchar(5));

06 Handout 1
 NOT NULL on ALTER TABLE – ensures that a column in o Example: ALTER TABLE Orders ADD FOREIGN KEY
an existing table cannot have a NULL value (CustomerID) REFERENCES Customers (CustomerID);
o Example: ALTER TABLE Students ALTER COLUMN
Section varchar(5) NOT NULL;
 UNIQUE on CREATE TABLE – ensures that all values in a
column are different upon creating a table
o Example: CREATE TABLE Students (StudentID
varchar(11) NOT NULL UNIQUE, LastName
varchar(99) NOT NULL, FirstName varchar(99) NOT
NULL, Section varchar(5));
 UNIQUE on ALTER TABLE – creates a UNIQUE constraint on
a column of an existing table
o Syntax: ALTER TABLE table_name ADD UNIQUE
(column);
o Example: ALTER TABLE Students ADD UNIQUE
(StudentID);
 PRIMARY KEY on CREATE TABLE – uniquely identifies each
row in a table
o Example: CREATE TABLE Students (StudentID
varchar(11) NOT NULL PRIMARY KEY, LastName
varchar(99) NOT NULL, FirstName varchar(99) NOT
NULL, Section varchar(5));
 PRIMARY KEY on ALTER TABLE – creates a PRIMARY
KEY
constraint on a column of an existing table
o Syntax: ALTER TABLE table_name ADD PRIMARY KEY
(column);
o Example: ALTER TABLE Students ADD PRIMARY KEY
(StudentID);
 FOREIGN KEY on CREATE TABLE – uniquely identifies a
row in another table
o Example: CREATE TABLE Orders (OrderID int NOT
NULL PRIMARY KEY, TableNumber int NOT NULL,
CustomerID int FOREIGN KEY REFERENCES
Customers (CustomerID));
 FOREIGN KEY on ALTER TABLE – creates a FOREIGN
KEY
constraint on a column of an existing table
o Syntax: ALTER TABLE table1_name ADD
FOREIGN KEY (table1_column) REFERENCES
table2_name (table2_column);
06 Handout 1
 CHECK on CREATE TABLE – ensures that all values
in a column satisfy a specific condition upon
creating a table
References:
o Example: CREATE TABLE Students Coronel, C. and Morris, S. (2017). Database systems: design, implementation, and
(StudentID varchar(11) NOT NULL, management (12th ed.). USA: Cengage Learning.
LastName varchar(99) NOT NULL, Elmasri, R. and Navathe, S. (2016). Fundamentals of database systems (7th ed.). USA:
FirstName varchar(99) NOT NULL, Age int Pearson Higher Education.
Kroenke, D. and Auer, D. (2016). Database processing: fundamentals, design,
CHECK (Age>=15));
and implementation. England: Pearson Education Limited.
 CHECK on ALTER TABLE – ensures that all values
in a column of an existing table satisfy a specific
condition
o Syntax: ALTER TABLE table_name ADD
CHECK
(condition);
o Example: ALTER TABLE Students ADD
CHECK (Age>=15);
 DEFAULT on CREATE TABLE – sets a default value
for a column when there is no value specified
o Example: CREATE TABLE Students
(StudentID varchar(11) NOT NULL,
LastName varchar(99) NOT NULL,
FirstName varchar(99) NOT NULL, Section
varchar(5) DEFAULT 'Not yet enrolled');
 DEFAULT on ALTER TABLE – sets a default value
for a column of an existing table when there is no
value specified
o Syntax: ALTER TABLE table_name ADD
CONSTRAINT
constraint_name DEFAULT 'value' FOR column;
o Example: ALTER TABLE Students ADD
CONSTRAINT df_section DEFAULT 'Not yet
enrolled' FOR Section;

06 Handout 1

You might also like