Data Definition Language
Data Definition Language
Week 6
National College of Ireland
Dublin, Ireland.
Boolean Data
Boolean data consists of the distinct truth values TRUE and FALSE. Unless
prohibited by a NOT NULL constraint, boolean data also supports the
UNKNOWN truth value as the NULL value.
SELECT true, false;
Character Data
Character data consists of a sequence of characters from an implementation-
defined character set, it is defined by the vendor of the particular SQL dialect.
For example: the branch number column Bno of the Branch table, which has
a fixed length of four characters, is declared as: Bno VARCHAR(4)
The column address of the PrivateOwner table, which has a variable number of
characters up to a maximum of 40, is declared as: address VARCHAR(40) 4
ISO SQL Data Types
Bit Data
The bit data type is used to define bit strings or a sequence of
binary digits (bits), each having either the value 0 or 1.
The format for specifying the bit data type is
BIT [VARYING] [length]
For example, to hold the fixed length binary string '0011', we declare a column
bitString, as:
bitString BIT(4) Precision is the number of
digits in a number. Scale is the
number of digits to the right of
the decimal point in a number.
Exact Numeric Data
The number consists of digits, an optional decimal point, and an optional
sign. An exact numeric data type consists of a precision and a scale. For
example, the exact numeric value −12.345 has precision 5 and scale 3.
5
ISO SQL Data Types
• http://dev.mysql.com/doc/ref
man/5.7/en/string-
functions.html
• https://www.w3schools.com/s
ql/func_mysql_trim.asp
6
Integrity Enhancement Feature
1. Required Data
• Some columns must contain a valid value; they are not allowed to
contain nulls.
• For example, every member of staff must have an associated job
position (for example, Manager, Assistant, and so on).
• The ISO standard provides the NOT NULL column specifier in the
CREATE and ALTER TABLE statements to provide this type of
constraint.
• For example, to specify that the column position of the Staff table
cannot be null, we define the column as:
position VARCHAR(10) NOT NULL 8
Integrity Enhancement Feature
2. Domain Constraints
Every column has a domain, in other words a set of legal values.
• For example, the sex of a member of staff is either 'M' or 'F', so the
domain of the column sex of the Staff table is a single character string
consisting of either 'M' or 'F'.
PRIMARY KEY(Sno)
• Can only have one PRIMARY KEY clause per table. Can still
ensure uniqueness for alternate keys using UNIQUE:
UNIQUE(telNo) 10
IEF - Referential Integrity
• FK is column or set of columns that links each row in the child table containing
foreign FK to row of parent table containing matching PK.
• Referential integrity means that, if FK contains a value, that value must refer to
existing row in parent table.
PK PK
PK
FK
PK
PK
FK
13
IEF - Referential Integrity
• For example, in the Property_For_Rent table, the staff number Sno is a
foreign key referencing the Staff table.
• In deletion rule, if a staff record is deleted from the Staff table, the
values of the corresponding Sno column in the Property_For_Rent table
are set to NULL:
RENAME TABLE
tbl_name TO new_table_name [, tbl_name2 TO new_table_name2, ...]
Example:
Assuming a table called old_table exists!
new_table TO old_table;
SET FOREIGN_KEY_CHECKS = 0;
20
Views
24
SQL - DROP VIEW
• For example,
SELECT COUNT(cnt)
FROM StaffPropCnt;
• Similarly,
SELECT *
FROM StaffPropCnt
WHERE cnt >= 2;
Advantages Disadvantages
• Data independence • Update restriction
• Currency • Structure restriction
• Improved security • Performance
• Reduced complexity
• Convenience
• Customization
• Data integrity 31
Transactions
• SQL defines transaction model based on COMMIT and ROLLBACK.
READ COMMITTED |
ON ObjectName
TO {AuthorizationIdList | PUBLIC}
43
REVOKE Specific Privileges
Revoke privilege SELECT on Branch table from all users.
REVOKE SELECT
ON Branch
FROM PUBLIC;
Stored Procedure:
A program that performs one or more actions and is called as an executable
PL/SQL statement. You can pass information into and out of a procedure through
44
its parameter list.
Module Resources
Recommended Book Resources Dr. Muhammad M Iqbal*
• Thomas Connolly, Carolyn Begg 2014, Database Systems: A Practical Approach
to Design, Implementation, and Management, 6th Edition Ed., Pearson
Education [ISBN: 1292061189] [Present in our Library]
Supplementary Book Resources
• Gordon S. Linoff, Data Analysis Using SQL and Excel, Wiley [ISBN:
0470099518]
• Eric Redmond, Jim Wilson, Seven Databases in Seven Weeks, Pragmatic
Bookshelf [ISBN: 1934356921]
• Baron Schwartz, Peter Zaitsev, Vadim Tkachenko, High Performance MySQL,
O'Reilly Media [ISBN: 1449314287]
Other Resources
• Website: http://www.thearling.com
• Website: http://www.mongodb.org
45
• Website: http://www.mysql.com