1 Structured Query Language: 2.1 Create Database
1 Structured Query Language: 2.1 Create Database
SQL, or Structured Query Language is the most popular declarative language used to work
with Relational Databases. Originally developed at IBM, it has been subsequently standard-
ized by various standards bodies (ANSI, ISO), and extended by various corporations adding
their own features (T-SQL, PL/SQL, etc.).
There are two primary parts to SQL: The DDL and DML (& DCL).
1
The ... is where column definitions go. The general format for a column definition is the
column name followed by column type. For example:
PERSONID INT
Which defines a column name PERSONID, of type INT. Column names have to be comma
separated, ie:
CREATE TABLE PERSON (
PERSONID INT,
LNAME VARCHAR(20),
FNAME VARCHAR(20) NOT NULL,
DOB DATE,
PRIMARY KEY(PERSONID)
);
The above creates a table named person, with person id, last name, first name, and date
of birth. There is also the ‘primary key’ definition. A primary key is a column value that
uniquely identifies a database record. So for example, we can have two ‘person’ records with
the same last name and first name, but with different ids.
Besides for primary key, there are many other flags we can specify for table columns. For
example, in the above example, FNAME is marked as NOT NULL, which means it is not allowed
to have NULL values3 .
Many databases implement various extensions to the basics, and you should read the
documentation to determine what features are present/absent, and how to use them.
2
ALTER TABLE <table-name>
DROP <field-name>
Note that very few databases let you drop a field. The drop command is mostly present to
allow for dropping of constraints (such as indexes, etc.) on the table.
The general syntax to modify a field (change its type, etc.) is:
ALTER TABLE <table-name>
MODIFY <field-name> <new-field-declaration>
Note that you can only do this to a certain extent on most databases. Just as with ‘drop’,
this is mostly useful for working with table constraints (changing ‘not null’ to ‘null’, etc.)
3
The column-list indicates what columns you’re interested in (the ones which you want
to appear in the result), the table-list is the list of tables to be used in the query, and
search-condition specifies what criteria you’re looking for.
An example of a short-hand version to retrieve all ‘person’ records we’ve been using:
SELECT * FROM PERSON;
= equals to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
<> not equal to
There is also IS, which can be used to check for NULL values, for example:
column-name IS NULL
We can also use AND, OR4 and parenthesis to group expressions.
Besides for these operators, we can also call built-in functions (as well as stored procedures
we define ourselves—that is, if the database supports stored procedures).
An example of the operators in use would be: something < 5 OR something is NULL
AND somedate = TO DATE(’01/03/93’,’MM/DD/YY’)5 .
4
3.5 DELETE Statement
The ‘delete’ is used to remove elements from the database. The syntax is very similar to
update and select statements:
Basically we select which records we want to delete using the where clause. An example use
would be:
Basically, a privilege can be something like ‘update’ or ‘select’, etc., or it can be ‘all’ when
granting ‘all’ privileges. An ‘object’ can be pretty much anything, but is often a database,
or database table. The ‘who’ is generally a database login/user.
Some databases (like MySQL) will actually create the user if they don’t already exist.
In some cases, you also have the option of specifying the user password, via: ‘identified by’
parameter.
5 Flavors of SQL
Basically every database implements its own version of SQL. The basic statements (like
the ones described) are almost always there and are almost always exactly the same on all
databases.
5
Extensions come in when you’re trying to do something fancy, like create stored proce-
dures, or work with a specific database environment. Microsoft SQL Server and Sybase use
a variant called T-SQL, for Transact-SQL. To contrast, Oracle uses PL/SQL, for Procedural
Language SQL. Some databases allow developers to write stored procedures in Java (like
Oracle) or something as simple as C language (PostgreSQL). And some databases don’t
even have stored procedures (at this time), like MySQL.