You are on page 1of 5

School of Science and Engineering Computer Science Department

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.

Table Vendor is an example

VendorCode Name ContactPerson AreaCode Phone Country PreviousOrder


21225 Bryson, Inc. Smithson 0181 223-3234 UK Y
21226 SuperLoo, Inc. Flushing 0113 215-8995 SA N
21231 D&E Supply Singh 0181 228-3245 UK Y

II. Creating a table and Inserting values:

CREATE TABLE Vendor (


CREATE TABLE table_name ( VendorCode INTEGER PRIMARY KEY,
column1 datatype constraints, Name VARCHAR(35) NOT NULL UNIQUE,
column2 datatype constraints, ContactPersonVARCHAR(15),
column3 datatype constraints,
AreaCodeCHAR(4),
....
PhoneCHAR(8),
);
CountryCHAR(2) NOT NULL,
PreviousOrderCHAR(1)
);
School of Science and Engineering Computer Science Department

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.

- PRIMARY KEYand UNIQUE are used to enforce entity integrity or uniqueness


constraints on columns (attributes).
- NOT NULLis used to enforce the constraint that a column cannot take a NULL value.
DEFAULT can be used to specify a default value for a column when it is not entered
when inserting a row in the table.
- The CHECK constraint is used to limit the value range that can be placed in a column.

INSERT INTO table_name (column1, column2, column3,...)


VALUES (value1, value2, value3, ...);

Insert values to a table:


School of Science and Engineering Computer Science Department

INSERT INTO Vendor (vendorCode, name, contactPerson, areacode,


phone, country, previousOrder) VALUES (21225, 'Bryson, Inc.',
'Smithson', '0181', '223-3234', 'UK', 'Y'), (21226, 'SuperLoo,
Inc.', 'Flushing', '0113', '215-8995', 'SA', 'N');

III. Drop and modify table components:

Although dropping a table from a database is dangerous, you can still do it using one of the
following commands:

DROP TABLE table_name; DROP TABLE IF EXISTS table_name;

To modify table components, we can use ALTER TABLE statement to add, delete, or modify
columns in an existing table:

Adding a new column to the table

ALTER TABLE table_name ALTER TABLE Vendor


ADD column_name datatype; ADD age INTEGER;

Deleting a column from a table

ALTER TABLE table_name ALTER TABLE Vendor


DROP COLUMN column_name; DROP COLUMN AreaCode;

Renaming a column

ALTER TABLE table_name ALTER TABLE Vendor


RENAME COLUMN old_name to RENAME COLUMN Country to C
new_name; ountry_of_birth;

Changing data type of a column

ALTER TABLE table_name ALTER TABLE Vendor


ALTER COLUMN column_nameTY ALTER COLUMN AreaCodeTYPEV
PE datatype; ARCHAR(8);
School of Science and Engineering Computer Science Department

Finally, to retrieve all columns and rows from a table, use this legacy SQL command:

SELECT * FROM table_name;

You might also like