Beruflich Dokumente
Kultur Dokumente
Structure
3.1 Introduction
Objectives
3.2 CREATE DATABASE
3.3 CREATE INDEX
3.4 CREATE TABLE
3.5 ALTER DATABASE
3.6 ALTER TABLE
3.7 DROP DATABASE
3.8 DROP INDEX
3.9 DROP TABLE
3.10 DESCRIBE
3.11 Summary
3.12 Terminal Questions
3.13 Answers
3.1 Introduction
The Data Definition Language (DDL) in any database server is used to
define the structures used to store the data. The Data Definition language
presented in this unit is used to create indexes and tables within the
specified database. The methods used to alter or modify the existing data
structures like tables or indexes are discussed with appropriate examples.
To view the structure or metadata information regarding the created
structures in the database, we use the Describe command whose syntax is
presented
Objectives
After studying this units, you should be able to:
explain various Data Definition Language (DDL) Statements
apply various CREATE syntaxes to create the appropriate structures
apply various ALTER syntaxes to modify the appropriate structures
explain and apply the DROP syntaxes
explain the usage of various Describe syntaxes
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
If you manually create a directory under the data directory (for example, with
mkdir), the server considers it as a database directory and is shown in the
output of SHOW_DATABASES.
times faster than reading sequentially. If you need to access most of the
rows, it is faster to read sequentially, because this minimizes disk seeks.
Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT)
are stored in B-trees. Exceptions are that indexes on spatial data types use
R-trees, and that MEMORY tables also support hash indexes.
MySQL uses indexes for these operations:
To find the rows matching a WHERE clause quickly.
To eliminate rows from consideration. If there is a choice between
multiple indexes, MySQL normally uses the index that finds the smallest
number of rows.
To retrieve rows from other tables when performing joins. MySQL can
use indexes on columns more efficiently if they are declared as the
same type and size. In this context, VARCHAR and CHAR are
considered the same if they are declared as the same size. For
example, VARCHAR(10) and CHAR(10) are the same size, but
VARCHAR(10) and CHAR(15) are not.
Comparison of dissimilar columns may prevent use of indexes if values
cannot be compared directly without conversion. Suppose that a
numeric column is compared to a string column. For a given value such
as 1 in the numeric column, it might compare equal to any number of
values in the string column such as '1', ' 1', '00001', or '01.e1'. This rules
out use of any indexes for the string column.
To find the MIN() or MAX() value for a specific indexed column key_col.
This is optimized by a preprocessor that checks whether you are using
WHERE key_part_N = constant on all key parts that occur before
key_col in the index. In this case, MySQL does a single key lookup for
each MIN() or MAX() expression and replaces it with a constant. If all
expressions are replaced with constants, the query returns at once. For
example:
If a multiple-column index exists on col1 and col2, the appropriate rows can
be fetched directly. If separate single-column indexes exist on col1 and col2,
the optimizer will attempt to use the Index Merge optimization, or attempt
to find the most restrictive index by deciding which index finds fewer rows
and using that index to fetch the rows.
Index Merge optimization: The Index Merge method is used to retrieve
rows with several range scans and to merge their results into one. The
merge can produce unions, intersections, or unions-of-intersections of its
underlying scans. This access method merges index scans from a single
table; it does not merge scans across multiple tables.
CREATE INDEX is mapped to an ALTER TABLE statement to create
indexes. CREATE INDEX cannot be used to create a PRIMARY KEY; use
ALTER TABLE instead. Normally, you create all indexes on a table at the
time the table itself is created with CREATE TABLE. CREATE INDEX
enables you to add indexes to existing tables. A column list of the form
(col1,col2,...) creates a multiple-column index. Index values are formed by
concatenating the values of the given columns.
Indexes can be created that use only the leading part of column values,
using col_name(length) syntax to specify an index prefix length:
Prefixes can be specified for CHAR, VARCHAR, BINARY, and
VARBINARY columns.
BLOB and TEXT columns also can be indexed, but a prefix length must
be given.
Prefix lengths are given in characters for non-binary string types and in
bytes for binary string types. That is, index entries consist of the first
length characters of each column value for CHAR, VARCHAR, and
TEXT columns, and the first length bytes of each column value for
BINARY, VARBINARY, and BLOB columns.
For spatial columns, prefix values cannot be given.
The statement shown here creates an index using the first 10 characters of
the name column:
If names in the column usually differ in the first 10 characters, this index
should not be much slower than an index created from the entire name
column. Also, using column prefixes for indexes can make the index file
much smaller, which could save a lot of disk space and might also speed up
INSERT operations.
Prefix lengths are storage engine-dependent (for example, a prefix can be
up to 1000 bytes long for MyISAM tables, 767 bytes for InnoDB tables).
Note that prefix limits are measured in bytes, whereas the prefix length in
CREATE INDEX statements is interpreted as number of characters for non-
binary data types (CHAR, VARCHAR, TEXT). Take this into account when
specifying a prefix length for a column that uses a multi-byte character set.
FULLTEXT indexes are supported only for MyISAM tables and can include
only CHAR, VARCHAR, and TEXT columns. Indexing always happens over
the entire column; column prefix indexing is not supported and any prefix
length is ignored if specified.
In MySQL 5.1:
You can add an index on a column that can have NULL values only if
you are using the MyISAM, InnoDB, or MEMORY storage engine.
You can add an index on a BLOB or TEXT column only if you are using
the MyISAM, or InnoDB storage engine.
An index_col_name specification can end with ASC or DESC. These
keywords are allowed for future extensions for specifying ascending or
descending index value storage. Currently, they are parsed but ignored;
index values are always stored in ascending order.
OR
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_option] ...
[partition_options]
select_statement
OR
create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
| CHECK (expr)
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string'] [reference_definition]
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH | RTREE}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
reference_definition:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
select_statement:
[IGNORE | REPLACE] [AS] SELECT ... (Some legal select statement)
To make MySQL compatible with some ODBC applications, you can find the
AUTO_INCREMENT value for the last inserted row with the following query:
SELECT * FROM tbl_name WHERE auto_col IS NULL
MySQL optimizer to prioritize which index to use and also more quickly
to detect duplicated UNIQUE keys.
A PRIMARY KEY can be a multiple-column index. However, you cannot
create a multiple-column index using the PRIMARY KEY key attribute in
a column specification. Doing so only marks that single column as
primary. You must use a separate PRIMARY KEY(index_col_name, ...)
clause.
If a PRIMARY KEY or UNIQUE index consists of only one column that
has an integer type, you can also refer to the column as _rowid in
SELECT statements.
In MySQL, the name of a PRIMARY KEY is PRIMARY. For other
indexes, if you do not assign a name, the index is assigned the same
name as the first indexed column, with an optional suffix (_2, _3, ...) to
make it unique. You can see index names for a table using SHOW
INDEX FROM tbl_name.
Some storage engines allow you to specify an index type when creating
an index. The syntax for the index_type specifier is USING type_name.
Example:
BLOB and TEXT columns count from one to four plus eight bytes each
toward the row-size limit because their contents are stored separately.
Declaring columns NULL can reduce the maximum number of columns
allowed. NULL columns require additional space in the row to record
whether or not their values are NULL.
For MyISAM tables, each NULL column takes one bit extra, rounded up
to the nearest byte.
The maximum row length in bytes can be calculated as follows:
Row length = 1
+ (sum of column lengths)
+ (number of NULL columns + delete_flag + 7)/8
+ (number of variable-length columns)
delete_flag is 1 for tables with static row format. Static tables use a bit in the
row record for a flag that indicates whether the row has been deleted.
delete_flag is 0 for dynamic tables because the flag is stored in the dynamic
row header.
These calculations do not apply for InnoDB tables, for which storage size is
no different for NULL columns than for NOT NULL columns.
Each table has an .frm file that contains the table definition. The .frm file
size limit is fixed at 64KB. If a table definition reaches this size, no more
columns can be added. The expression that checks information to be
stored in the .frm file against the limit looks like this:
if (info_length+(ulong) create_fields.elements*FCOMP+288+
n_length+int_length+com_length > 65535L || int_count > 255)
If a storage engine is specified that is not available, MySQL uses the default
engine instead. Normally, this is MyISAM. For example, if a table definition
includes the ENGINE=INNODB option but the MySQL server does not
support INNODB tables, the table is created as a MyISAM table. This makes
it possible to have a replication setup where you have transactional tables
on the master but tables created on the slave are non-transactional (to get
more speed). In MySQL 5.1, a warning occurs if the storage engine
specification is not honored.
Engine substitution can be controlled by the setting of the
NO_ENGINE_SUBSTITUTION SQL mode.
The other table options are used to optimize the behavior of the table. In
most cases, you do not have to specify any of them. These options apply to
all storage engines unless otherwise indicated. Options that do not apply to
a given storage engine may be accepted and remembered as part of the
table definition. Such options then apply if you later use ALTER TABLE to
convert the table to use a different storage engine.
AUTO_INCREMENT: The initial AUTO_INCREMENT value for the
table. In MySQL 5.1, this works for MyISAM, MEMORY, and InnoDB
tables. It also works for ARCHIVE tables as of MySQL 5.1.6. To set the
first auto-increment value for engines that do not support the
AUTO_INCREMENT table option, insert a “dummy” row with a value one
less than the desired value after creating the table, and then delete the
dummy row.
For engines that support the AUTO_INCREMENT table option in
CREATE TABLE statements, you can also use ALTER TABLE tbl_name
AUTO_INCREMENT = N to reset the AUTO_INCREMENT value. The
value cannot be set lower than the maximum value currently in the
column.
AVG_ROW_LENGTH: An approximation of the average row length for
your table. You need to set this only for large tables with variable-size
rows.
When you create a MyISAM table, MySQL uses the product of the
MAX_ROWS and AVG_ROW_LENGTH options to decide how big the
resulting table is. If you don't specify either option, the maximum size for
MyISAM data and index files is 256TB by default. (If your operating
system does not support files that large, table sizes are constrained by
the file size limit.) If you want to keep down the pointer sizes to make the
index smaller and faster and you don't really need big files, you can
decrease the default pointer size by setting the
myisam_data_pointer_size system variable. If you want all your tables to
be able to grow above the default limit and are willing to have your
tables slightly slower and larger than necessary, you can increase the
default pointer size by setting this variable. Setting the value to 7 allows
table sizes up to 65,536TB.
[DEFAULT] CHARACTER SET: Specify a default character set for the
table. CHARSET is a synonym for CHARACTER SET. If the character
set name is DEFAULT, the database character set is used.
CHECKSUM: Set this to 1 if you want MySQL to maintain a live
checksum for all rows (that is, a checksum that MySQL updates
automatically as the table changes). This makes the table a little slower
to update, but also makes it easier to find corrupted tables. The
CHECKSUM TABLE statement reports the checksum. (MyISAM only.)
[DEFAULT] COLLATE: Specify a default collation for the table.
COMMENT: A comment for the table, up to 60 characters long.
CONNECTION: The connection string for a FEDERATED table.
DATA DIRECTORY, INDEX DIRECTORY: By using DATA
DIRECTORY='directory' or INDEX DIRECTORY='directory' you can
specify where the MyISAM storage engine should put a table's data file
and index file. The directory must be the full pathname to the directory,
not a relative path.
If you do not use PACK_KEYS, the default is to pack strings, but not
numbers. If you use PACK_KEYS=1, numbers are packed as well.
When packing binary number keys, MySQL uses prefix compression:
o Every key needs one extra byte to indicate how many bytes of the
previous key are the same for the next key.
o The pointer to the row is stored in high-byte-first order directly after
the key, to improve compression.
This means that if you have many equal keys on two consecutive rows, all
following “same” keys usually only take two bytes (including the pointer to
the row). Compare this to the ordinary case where the following keys takes
storage_size_for_key + pointer_size (where the pointer size is usually 4).
Conversely, you get a significant benefit from prefix compression only if you
have many numbers that are the same. If all keys are totally different, you
use one byte more per key, if the key is not a key that can have NULL
values. (In this case, the packed key length is stored in the same byte that is
used to mark if a key is NULL.)
PASSWORD: This option is unused. If you have a need to scramble
your .frm files and make them unusable to any other MySQL server,
please contact our sales department.
RAID_TYPE: RAID support has been removed as of MySQL 5.0.
ROW_FORMAT: Defines how the rows should be stored. For MyISAM
tables, the option value can be FIXED or DYNAMIC for static or variable-
length row format. myisampack sets the type to COMPRESSED.
Note: When executing a CREATE TABLE statement, if you specify a row
format which is not supported by the storage engine that is used for the
table, the table is created using that storage engine's default row format.
The information reported in this column in response to SHOW TABLE
STATUS is the actual row format used. This may differ from the value in the
Create_options column because the original CREATE TABLE definition is
retained during creation.
UNION: UNION is used when you want to access a collection of
identical MyISAM tables as one. This works only with MERGE tables.
You must have SELECT, UPDATE, and DELETE privileges for the
tables you map to a MERGE table.
partition_options can be used to control partitioning of the table created
with CREATE TABLE.
If used, a partition_options clause begins with PARTITION BY. This
clause contains the function that is used to determine the partition; the
function returns an integer value ranging from 1 to num, where num is
the number of partitions. (The maximum number of user-defined
partitions which a table may contain is 1024; the number of subpartitions
– discussed later in this section – is included in this maximum.) The
choices that are available for this function in MySQL 5.1 are shown in
the following list:
o HASH(expr): Hashes one or more columns to create a key for
placing and locating rows. expr is an expression using one or more
table columns. This can be any legal MySQL expression (including
MySQL functions) that yields a single integer value.
o KEY(column_list): This is similar to HASH, except that MySQL
supplies the hashing function so as to guarantee an even data
distribution. The column_list argument is simply a list of table
columns.
o For tables that are partitioned by key, you can employ linear
partitioning by using the LINEAR keyword. This has the same effect
as with tables that are partitioned by HASH. That is, the partition
number is found using the & operator rather than the modulus. You
may not use either VALUES LESS THAN or VALUES IN clauses
with PARTITION BY KEY.
o RANGE: In this case, expr shows a range of values using a set
of VALUES LESS THAN operators. When using range
partitioning, you must define at least one partition using VALUES
LESS THAN. You cannot use VALUES IN with range partitioning.
VALUES LESS THAN can be used with either a literal value or
an expression that evaluates to a single value.
o LIST(expr): This is useful when assigning partitions based on a
table column with a restricted set of possible values, such as a
state or country code. In such a case, all rows pertaining to a
MySQL creates new columns for all elements in the SELECT. For
example:
mysql> CREATE TABLE test (a INT NOT NULL
AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> ENGINE=MyISAM SELECT b,c FROM test2;
For each row in table foo, a row is inserted in bar with the values
from foo and default values for the new columns.
In a table resulting from CREATE TABLE ... SELECT, columns
named only in the CREATE TABLE part come first. Columns named
in both parts or only in the SELECT part come after that. The data
type of SELECT columns can be overridden by also specifying the
column in the CREATE TABLE part.
If any errors occur while copying the data to the table, it is
automatically dropped and not created.
CREATE TABLE ... SELECT does not automatically create any
indexes for you. This is done intentionally to make the statement as
flexible as possible. If you want to have indexes in the created table,
you should specify these before the SELECT statement:
CREATE TABLE t
(
c1 VARCHAR(10) CHARACTER SET binary,
c2 TEXT CHARACTER SET binary,
c3 ENUM('a','b','c') CHARACTER SET binary
);
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
The CHARACTER SET clause changes the default database character set.
The COLLATE clause changes the default database collation.
You can see what character sets and collations are available using,
respectively, the SHOW CHARACTER SET and SHOW COLLATION
statements.
The database name can be omitted from the first syntax, in which case the
statement applies to the default database.
The syntax that includes the UPGRADE DATA DIRECTORY NAME clause
was added in MySQL 5.1.23. It updates the name of the directory
associated with the database to use the encoding implemented in MySQL
5.1 for mapping database names to database directory names. This clause
is for use under these conditions:
It is intended when upgrading MySQL to 5.1 or later from older
versions.
It is intended to update a database directory name to the current
encoding format if the name contains special characters that need
encoding.
The statement is used by mysqlcheck (as invoked by
mysql_upgrade).
For example,if a database in MySQL 5.0 has a name of a-b-c, the name
contains instance of the „-‟ character. In 5.0, the database directory is also
named a-b-c, which is not necessarily safe for all filesystems. In MySQL 5.1
and up, the same database name is encoded as a@002db@002dc to
produce a filesystem-neutral directory name.
The action component here can be any of the keywords ADD, DROP,
ALTER, or CHANGE, and is followed by a field definition similar to that used
by the CREATE TABLE command. This definition consists of the name of
the field to be modified and (depending on the operation) a field definition
consisting of a new field name, type, and constraints.
Given below is an example which first creates a table and then adds a new
column to it.
The following example adds a primary key column to the existing members
table:
The following example changes the name and type of an existing field:
You can use the ALTER TABLE command to rename a table as shown
below:
You can also use the equivalent RENAME TABLE command as shown
below:
You can set (or remove) a default value for a field with the SET DEFAULT
and DROP DEFAULT clauses, and you can control the position of fields with
the FIRST and AFTER clauses to the ALTER TABLE command.
Now, if you were to set the name field to be UNIQUE, MySQL would return
an error about duplicate values in that field.
so be careful with this statement! If any of the tables named in the argument
list do not exist, MySQL returns an error indicating by name which non-
existing tables it was unable to drop, but it also drops all of the tables in the
list that do exist.
Important Note: When a table is dropped, user privileges on the table are
not automatically dropped.
Note that for a partitioned table, DROP TABLE permanently removes the
table definition, all of its partitions, and all of the data which was stored in
those partitions. It also removes the partitioning definition (.par) file
associated with the dropped table.
Use IF EXISTS to prevent an error from occurring for tables that do not
exist. A NOTE is generated for each non-existent table when using IF
EXISTS.
RESTRICT and CASCADE are allowed to make porting easier. In MySQL
5.1, they do nothing.
Note: DROP TABLE automatically commits the current active transaction,
unless you use the TEMPORARY keyword.
The TEMPORARY keyword has the following effects:
The statement drops only TEMPORARY tables.
The statement does not end an ongoing transaction.
No access rights are checked. (A TEMPORARY table is visible only to
the client that created it, so no check is necessary.)
Using TEMPORARY is a good way to ensure that you do not accidentally
drop a non-TEMPORARY table.
Syntax:
{DESCRIBE | DESC} tbl_name [col_name | wild]
3.11 Summary
This unit covers the following topics with respect to data definition language
statements in MySQL:
1. CREATE DATABASE: This topic covers the starting point of creating
the databases before any data objects are built into the databases. The
user must first create a database and build objects like tables, views,
stored procedures and so on inside the database. It gives the syntax
associated with creation of these databases within the MySQL server.
2. CREATE INDEX: The importance and syntaxes used in the creation of
indexes is mentioned in this topic.
Sikkim Manipal University Page No. 87
RDBMS with MySQL Unit 3
3. CREATE TABLE: Tables are the basic data structures used to store
data. The syntaxes and variations of creating the tables are discussed
here.
4. ALTER DATABASE: Database modifications and the respective
syntaxes are described along with suitable examples are discussed.
5. ALTER TABLE: Modifications to the existing tables and the
corresponding syntaxes are described with suitable examples are
discussed here.
6. DROP DATABASE: Databases can be dropped as and when required.
The details of this is discussed.
7. DROP INDEX: Indexes can be dropped depending on the requirement
and size of the database objects.
8. DROP TABLE: Tables Indexes can be dropped depending on the
requirement of the user or application.
9. DESCRIBE: Anyone who needs to know the structure and data types of
the database tables can use this command for this purpose.
3.13 Answers
Self Assessment Questions
1. CREATE SCHEMA
2. db.opt
3. prefix
4. TEMPORARY
5. DEFAULT
6. UNIQUE
7. .frm
Terminal Questions
1.
To find the rows matching a WHERE clause quickly.
To eliminate rows from consideration.
To retrieve rows from other tables when performing joins.
To find the MIN() or MAX() value for a specific indexed column
key_col.
(Refer Section 3.3)
2.
ALTER DATABASE enables you to change the overall characteristics of a
database. These characteristics are stored in the db.opt file in the database
directory. To use ALTER DATABASE, you need the ALTER privilege on the
database. ALTER SCHEMA is a synonym for ALTER DATABASE.
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
The CHARACTER SET clause changes the default database character set.
The COLLATE clause changes the default database collation.
You can see what character sets and collations are available using,
respectively, the SHOW CHARACTER SET and SHOW COLLATION
statements.
The database name can be omitted from the first syntax, in which case the
statement applies to the default database.
(Refer Section 3.5)
3.
a. CREATE TABLE members(mid INT(3), mname CHAR(8), mpass
VARCHAR(25));
ALTER TABLE members ADD email VARCHAR(255) NOT NULL;
b. ALTER TABLE members ADD PRIMARY KEY(email);
c. ALTER TABLE members CHANGE mid id INT(8) AUTO_INCREMENT
UNIQUE;
Sikkim Manipal University Page No. 89
RDBMS with MySQL Unit 3