CSC 3326 03 Lab
CSC 3326 03 Lab
Lab 3
Introduction to SQL
Data Definition – Data manipulation
School of Science and Engineering Computer Science Department
I. Introduction:
SQL (Structured Query Language) is the query language for relational database systems.
Although SQL is an ANSI (American National Standards Institute) standard, there are some
slightly different versions and implementations of SQL. However, to be compliant with the
ANSI standard, they all support at least the major statements commands (such as INSERT,
UPDATE, DELETE, SELECT ...) in a similar manner.
In relational database systems, data is contained in tables. A table is a collection of rows (tuples
or records) of the same structure (tuples can vary in length). Additionally, a table consists of
columns (attributes). Below is an example of a table called Vendor which contains three rows
(one for each vendor) and seven columns.
1. SQL Data Types:Each column in a table has a specific data type. Here are some common
SQL data types :
Exact numeric:
o integer (int), smallint, bigint: for integer data.
o numeric (decimal): for exact numeric data other than integer data.
o money (for some SQL implementations like in PostgreSQL): for currency
amounts.
Serial:
o smallserial, serial, bigserial : a number that start with 1 and auto increment every
time you add new rows to the table.
Approximate numeric:
o real, double, float: for floating-point numbers.
Date and time:
o date, time, timestamp: for date and/or time of day.
String types:
o character(char),varchar: for character strings.
o Text.
Note: Feel free to refer to the following resource for more insight on SQL Data Types along with
explanations on few notable differences between what could look like similar data types. Check:
https://www.cs.toronto.edu/~nn/csc309/guide/pointbase/docs/html/htmlfiles/
dev_datatypesandconversionsFIN.html
2. SQL Data Constraints: Constraints are used to specify rules for the data in a table. Primary
Key, Unique and Not Null are examples of constraints.
Although dropping a table from a database is dangerous, you can still do it using one of the
following commands:
To modify table components, we can use ALTER TABLE statement to add, delete, or modify
columns in an existing table:
Renaming a column
Finally, to retrieve all columns and rows from a table, use this legacy SQL command: