Structured Query Language (SQL) - Cultural View of Technology
Structured Query Language (SQL) - Cultural View of Technology Structured Query Language (SQL) - Cultural View of Technology
SQL 4 Null and three-valued logic (3VL) The idea of Null was introduced into SQL to handle missing information in the relational model. The introduction of Null (or Unknown) along with True and False is the foundation of three-valued logic. Null does not have a value (and is not a member of any data domain) but is rather a placeholder or “mark” for missing information. Therefore comparisons with Null can never result in either True or False but always in the third logical result, Unknown. [11] SQL uses Null to handle missing information. It supports three-valued logic (3VL) and the rules governing SQL three-valued logic (3VL) are shown below (p and q represent logical states). [12] The word NULL is also a reserved keyword in SQL, used to identify the Null special marker. Additionally, since SQL operators return Unknown when comparing anything with Null, SQL provides two Null-specific comparison predicates: The IS NULL and IS NOT NULL test whether data is or is not Null. [13] Note that SQL returns only results for which the WHERE clause returns a value of True. I.e., it excludes results with values of False, but also those whose value is Unknown. p AND q p True False Unknown q True True False Unknown False False False False Unknown Unknown False Unknown p OR q p True False Unknown q True True True True False True False Unknown Unknown True Unknown Unknown p NOT p True False False True Unknown Unknown p = q p True False Unknown q True True False Unknown False False True Unknown Unknown Unknown Unknown Unknown Universal quantification is not explicitly supported by SQL, and must be worked out as a negated existential [14] [15] [16] quantification. There is also the " IS DISTINCT FROM " infixed comparison operator which returns TRUE unless both operands are equal or both are NULL. Likewise, IS NOT DISTINCT FROM is defined as "NOT ( IS DISTINCT FROM ") Data manipulation The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete data: • INSERT adds rows (formally tuples) to an existing table, e.g.,: INSERT INTO My_table VALUES (field1, field2, field3) ('test', 'N', NULL); • UPDATE modifies a set of existing table rows, e.g.,: UPDATE My_table SET field1 = 'updated value' WHERE field2 = 'N'; • DELETE removes existing rows from a table, e.g.,: DELETE FROM My_table WHERE field2 = 'N'; • TRUNCATE deletes all data from a table in a very fast way. It usually implies a subsequent COMMIT operation.
SQL 5 • MERGE is used to combine the data of multiple tables. It combines the INSERT and UPDATE elements. It is defined in the SQL:2003 standard; prior to that, some databases provided similar functionality via different syntax, sometimes called "upsert". Transaction controls Transactions, if available, wrap DML operations: • START TRANSACTION (or BEGIN WORK, or BEGIN TRANSACTION, depending on SQL dialect) mark the start of a database transaction, which either completes entirely or not at all. • SAVE TRANSACTION (or SAVEPOINT ) save the state of the database at the current point in transaction CREATE TABLE tbl_1(id int); INSERT into tbl_1(id) value(1); INSERT into tbl_1(id) value(2); COMMIT; UPDATE tbl_1 SET id=200 WHERE id=1; SAVEPOINT id-1upd; UPDATE tbl_1 SET id=1000 WHERE id=2; ROLLBACK to id-1upd; SELECT id from tbl_1; • COMMIT causes all data changes in a transaction to be made permanent. • ROLLBACK causes all data changes since the last COMMIT or ROLLBACK to be discarded, leaving the state of the data as it was prior to those changes. Once the COMMIT statement completes, the transaction's changes cannot be rolled back. COMMIT and ROLLBACK terminate the current transaction and release data locks. In the absence of a START TRANSACTION or similar statement, the semantics of SQL are implementation-dependent. Example: A classic bank transfer of funds transaction. START TRANSACTION; UPDATE Account SET amount=amount-200 WHERE account_number=1234; UPDATE Account SET amount=amount+200 WHERE account_number=2345; IF ERRORS=0 COMMIT; IF ERRORS0 ROLLBACK; Data definition The Data Definition Language (DDL) manages table and index structure. The most basic items of DDL are the CREATE, ALTER, RENAME, DROP and TRUNCATE statements: • CREATE creates an object (a table, for example) in the database. • DROP deletes an object in the database, usually irretrievably. • ALTER modifies the structure of an existing object in various ways—for example, adding a column to an existing table. Example: CREATE TABLE My_table ( my_field1 INT, my_field2 VARCHAR(50), my_field3 DATE NOT NULL,
- Page 1 and 2: Structured Query Language (SQL) SQL
- Page 3 and 4: Nested SQL 72 Object-oriented SQL 7
- Page 5 and 6: SQL 1 SQL Paradigm Multi-paradigm A
- Page 7: SQL 3 result set. SELECT * FROM Boo
- Page 11 and 12: SQL 7 REVOKE SELECT, UPDATE ON My_t
- Page 13 and 14: SQL 9 2008 SQL:2008 Legalizes ORDER
- Page 15 and 16: SQL 11 [9] ANSI/ISO/IEC Internation
- Page 17 and 18: SQL:2003 13 [11] http://www.iso.org
- Page 19 and 20: Advantage Database Server 15 • Sm
- Page 21 and 22: Call Level Interface 17 Call Level
- Page 23 and 24: Check Constraint 19 Check Constrain
- Page 25 and 26: Commit (data management) 21 Commit
- Page 27 and 28: Correlated subquery 23 Correlated s
- Page 29 and 30: CUBRID 25 Linux Foundation Silver S
- Page 31 and 32: CUBRID 27 References [1] http://www
- Page 33 and 34: Cursor (databases) 29 Scrollable cu
- Page 35 and 36: Cursor (databases) 31 Disadvantages
- Page 37 and 38: Data Definition Language 33 Data De
- Page 39 and 40: Data Manipulation Language 35 Data
- Page 41 and 42: Database Console Commands (Transact
- Page 43 and 44: Database Console Commands (Transact
- Page 45 and 46: Database Console Commands (Transact
- Page 47 and 48: DbForge Studio for MySQL 43 • Dat
- Page 49 and 50: Declarative Referential Integrity 4
- Page 51 and 52: Devgems Data Modeler 47 External li
- Page 53 and 54: Embedded SQL 49 External links •
- Page 55 and 56: EnterpriseDB 51 EnterpriseDB also o
- Page 57 and 58: Epictetus Database Client 53 Extern
<strong>SQL</strong> 5<br />
• MERGE is used to combine the data <strong>of</strong> multiple tables. It combines the INSERT and UPDATE elements. It is<br />
defined in the <strong>SQL</strong>:2003 standard; prior to that, some databases provided similar functionality via different<br />
syntax, sometimes called "upsert".<br />
Transaction controls<br />
Transactions, if available, wrap DML operations:<br />
• START TRANSACTION (or BEGIN WORK, or BEGIN TRANSACTION, depending on <strong>SQL</strong> dialect) mark the<br />
start <strong>of</strong> a database transaction, which either completes entirely or not at all.<br />
• SAVE TRANSACTION (or SAVEPOINT ) save the state <strong>of</strong> the database at the current point in transaction<br />
CREATE TABLE tbl_1(id int);<br />
INSERT into tbl_1(id) value(1);<br />
INSERT into tbl_1(id) value(2);<br />
COMMIT;<br />
UPDATE tbl_1 SET id=200 WHERE id=1;<br />
SAVEPOINT id-1upd;<br />
UPDATE tbl_1 SET id=1000 WHERE id=2;<br />
ROLLBACK to id-1upd;<br />
SELECT id from tbl_1;<br />
• COMMIT causes all data changes in a transaction to be made permanent.<br />
• ROLLBACK causes all data changes since the last COMMIT or ROLLBACK to be discarded, leaving the state <strong>of</strong><br />
the data as it was prior to those changes.<br />
Once the COMMIT statement completes, the transaction's changes cannot be rolled back.<br />
COMMIT and ROLLBACK terminate the current transaction and release data locks. In the absence <strong>of</strong> a START<br />
TRANSACTION or similar statement, the semantics <strong>of</strong> <strong>SQL</strong> are implementation-dependent. Example: A classic<br />
bank transfer <strong>of</strong> funds transaction.<br />
START TRANSACTION;<br />
UPDATE Account SET amount=amount-200 WHERE account_number=1234;<br />
UPDATE Account SET amount=amount+200 WHERE account_number=2345;<br />
IF ERRORS=0 COMMIT;<br />
IF ERRORS0 ROLLBACK;<br />
Data definition<br />
The Data Definition <strong>Language</strong> (DDL) manages table and index structure. The most basic items <strong>of</strong> DDL are the<br />
CREATE, ALTER, RENAME, DROP and TRUNCATE statements:<br />
• CREATE creates an object (a table, for example) in the database.<br />
• DROP deletes an object in the database, usually irretrievably.<br />
• ALTER modifies the structure <strong>of</strong> an existing object in various ways—for example, adding a column to an existing<br />
table.<br />
Example:<br />
CREATE TABLE My_table<br />
(<br />
my_field1 INT,<br />
my_field2 VARCHAR(50),<br />
my_field3 DATE NOT NULL,