This document discusses SQL basics including transactions, concurrency control, and schema level objects. It explains the ACID properties of transactions including atomicity, consistency, isolation, and durability. It also covers concurrency control, isolation levels, schema objects like stored procedures and functions, domains, sequences, assertions and more. Key concepts are explained with SQL syntax examples.
2. TRANSACTIONS AND
CONCURRENCY CONTROL
The concept of a user session involves the user first connecting to the database. This is like
dialing a phone number, but with a password, to get to the database.
The Standard SQL syntax for this statement is:
CONNECT TO <connection target>
<connection target> ::=
<SQL-server name>
[AS <connection name>]
[USER <user name>]
| DEFAULT
3. Transactions and ACID
Four characteristics we want in a transaction: the ACID properties.
• Atomicity
• Consistency
• Isolation
• Durability
4. Atomicity
Atomicity means that the whole transaction becomes persistent in the database or nothing in
the transaction becomes persistent. The data becomes persistent in Standard SQL when a
COMMIT statement is successfully executed.
A ROLLBACK statement removes the transaction and restores the database to its prior
(consistent) state before the transaction began. The COMMIT or ROLLBACK statement can be
explicitly executed by the user or by the database engine when it finds an error. Most SQL
engines default to a ROLLBACK unless they are configured to do otherwise.
5. Consistency
When the transaction starts, the database is in a consistent state and when it becomes
persistent in the database, the database is in a consistent state. The phrase “consistent state”
means that all of the data integrity constraints, relational integrity constraints, and any other
constraints are true.
6. Isolation
Isolation is also called serializability because it means that transactions act as if they were
executed in isolation from each other.
7. Durability
The database is stored on a durable media, so that if the database program is destroyed, the
database itself persists. Furthermore, the database can be restored to a consistent state when
the database system is restored. Log files and back-up procedure
8. Concurrency Control
Concurrency control is the part of transaction handling that deals with how multiple users
access the shared database without running into each other—sort of like a traffic light system.
One way to avoid any problems is to allow only one user in the database at a time. The only
problem with that solution is that the other users are going to get slow response time.
9. The Isolation Levels
In standard SQL, the user gets to set the isolation level of the transactions in his session. The
isolation level avoids some of the phenomena we just talked about and gives other information
to the database. The syntax for the <set transaction statement> is:
10. SCHEMA LEVEL OBJECTS
A database is not just a bunch of tables, even though that is where most of the work is done.
There are stored procedures, user-defined functions, and cursors that the users create. Then
there are indexes and other access methods that the user cannot access directly.
11. CREATE SCHEMA Statement
There is a CREATE SCHEMA statement defined in the standards that brings an entire schema into
existence all at once. In practice, each product has very different utility programs to allocate
physical storage and define a schema. Much of the proprietary syntax is concerned with physical
storage allocations.
CREATE PROCEDURE, CREATE FUNCTION, and CREATE TRIGGER
Procedural construct statements put modules of procedural code written in SQL/PSM or other
languages into the database
12. CREATE DOMAIN Statement
The DOMAIN is a schema element in Standard SQL that allows you to declare an in-line macro
that will allow you to put a commonly used column definition in one place in the schema. The
syntax is:
14. CREATE SEQUENCE
Sequences are generators that produce a sequence of values each time they are invoked.
The syntax looks like this:
CREATE SEQUENCE <seq name> AS <data type>
START WITH <value>
INCREMENT BY <value>
[MAXVALUE <value>]
[MINVALUE <value>]
[[NO] CYCLE];
15. CREATE ASSERTION
In Standard SQL, the CREATE ASSERTION allows you to apply a constraint on the tables within a schema but not have the constraint
attached to any particular table. The syntax is:
<assertion definition> ::=
CREATE ASSERTION <constraint name> <assertion check>
[<constraint attributes>]
<assertion check> ::=
CHECK (<search condition>)
As you would expect, there is a DROP ASSERTION statement, but no ALTER ASSERTION statement. An assertion can do things
that a CHECK() clause attached to a table cannot do, because it is outside of the tables involved. A CHECK() constraint is always
TRUE if the table is empty.