Principles of Database Systems (H) Lecture 2 Notes
Introduction to SQL
To manage data in a database, a full-blown language will introduce too much complexity to both users and database systems. What we need is a special DSL that specializes in data query and manipulation, hence SQL was invented. The SQL is composed of four parts:
- Data Manipulation Language, DML
- Data Definition Language, DDL
- Data Query Language, DQL
- Data Control Language, DCL
The most common used one is the DQL, or more specifically, the
SELECT statement:
1 | SELECT column_name FROM table_name WHERE conditions |
CREATE, ALTER, and DROP:
1 | -- Create a table |
INSERT, UPDATE,
and DELETE: 1 | INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); |
Caution
The SQL language lacks standardization, and multiple variants exist. In this course, we will stick to the one that PostgreSQL uses.
Caution
The principle of “all rows are distinct” can be enforced for tables in SQL, but not enforced for query results in SQL.
Caution
The SQL keyword and identifier are case-insensitive.
Text datatypes:
char(length): Fixed-length strings.varchar(max_length): Variable-length strings that are limited by a maximum length.varchar2(max_length): Only appears in Oracle SQL.text: Stores big text (~10KB).clob: Stores much bigger texts (up to 4GB).nchar/nvarchar: Unicode equivalent ofchar/varchar.
Numerical datatypes:
int: 4-byte integer type.float: IEEE-compliant 4-byte floating point type.numeric(precision, scale):precisionis the number of digits, and thescaleis the number of these digits after the decimal point.
Data datatypes:
date: Date and times (down to seconds with Oracle, not with others).datatime: Also date and times, down to seconds (other than Oracle, DB2, and PostgreSQL)timestamp: down tosecond, replace datetime in DB2/PostgreSQL.
Binary datatypes
raw(max_length): Fixed-length binaries.varbinary(max_length): Variable-length binaries that can have a maximum length ofmax_lengthbits.blob: Binary equivalent ofclob.bytea: Same asblob, used in PostgreSQL.
By default, columns are permitted to have a null as it
value. To prohibit this, use NOT NULL decorator when
creating the column.
Some DBMS systems support adding comments to database objects, e.g. in PostgreSQL:
1 | COMMENT ON object_type object_name is string|null |
Constraints
For example, we want create a table for storing personal information:
1 | CREATE TABLE people |
null data, so we need to add not null
constraints for mandatory data: 1 | CREATE TABLE people |
primary key constraint:
1 | CREATE TABLE people |
primary key constraint implies
not null. SQL will not warn about redundant constraints.
Another potential problem is the capitalization. For example, the three
strings:
"FOO""foo""Foo"
is considered to be not equal in Oracle, PostgreSQL, and
DB2, but they are equal in SQL Server, MySQL, and SQLite. So we
need to standardize the names, using unique constraint for
uniqueness and check for standardization:
1 | CREATE TABLE people |
- Title: Principles of Database Systems (H) Lecture 2 Notes
- Author: Zhige Chen
- Created at : 2025-09-19 18:33:02
- Updated at : 2025-10-10 13:19:55
- Link: https://nofe1248.github.io/2025/09/19/dbh-02/
- License: This work is licensed under CC BY-NC-SA 4.0.