other ddl commands - cs 3200 · if a modification to a table would lose tuples the alter command...
TRANSCRIPT
OTHER DDL Commands
Topic 2Lesson 7 – More commands for table structure modification
2
Chapter 5, 8, 11 Murach’s MySQL
3
CREATE TABLE AS Operation in SQLUse the CREATE TABLE AS command when you want to
store the result of a query as a base table.
The structure of the table is defined by the structure of the result. The data types are determined by the data types of the returned fields.
This command combines the CREATE and the INSERT command.
SYNTAX:CREATE TABLE table_name AS select_statement
4
EXAMPLE: CREATE TABLE AS Create a copy of the Student table.CREATE TABLE student_copy ASSELECT * FROM students;
ID Name School Credits_Earned
Credits_Req
Yr_o_grad
1 Smith Khoury 32 120 2019
2 Shah D’Amore McKim 64 128 2019
3 Li Khoury 50 120 2020
ID Name School Credits_Earned
Credits_Req
Yr_o_grad
1 Smith Khoury 32 120 2019
2 Shah D’Amore McKim 64 128 2019
3 Li Khoury 50 120 2020
5
Remove a table • Use the command DROP to remove the table from a
database. All data as well as the definition of the table is removed from the database.
DROP TABLE Student;
6
Alter the structure of a table The alter command allows a user to change the structure of
the table such as add/change/delete fields, add / change/ remove constraints.
SYNTAX: ALTER TABLE [db_name.]table_name{ADD column_name data_type[column_attributes] |
DROP COLUMN column_name |MODIFY column_name data_type[column_attributes] |
RENAME COLUMN old_column_name TO new_column_name
}
7
EXAMPLE: ALTER a tableAdd a field to a tableALTER TABLE student ADD column email VARCHAR(20);
Drop a field from a tableALTER TABLE student DROP column credits_earned;
Change a field constraint ALTER TABLE student MODIFY column email VARCHAR(20)
DEFAULT “UNKNOWN”;
If a modification to a table would lose tuples the ALTER command will fail.
8
DATA TYPES in MySQL
MySQL supports:• Character• Numeric• Date and time• Large Object (LOB)• Spatial• JSON
Relational Data Model
9
Character DATA TYPES in MySQL
• CharacterType BytesCHAR(M) Mx4 VARCHAR(M) L+1
M = length + 1
Relational Data Model
10
INTEGER types and their sizes
Type BytesBIGINT 8INT 4 MEDIUMINT 3SMALLINT 2
TINYINT 1
11
Fixed point typeThe DECIMAL type allows the creator to specify the number
of positions after the decimal point.
FORMAT: DECIMAL(M, D)
M specifies the number of positions for the entire numberD specifies the number of positions after the decimal point.
Useful for representing money.
12
Floating point and Double typeThe FLOAT type provides single precision, while a DOUBLE
provides double precision. A FLOAT has place places of accuracy while a DOUBLE has 16 places of accuracy. If you try to store a larger value than what can fit in the number, the number will be truncated so it can fit.
The FLOAT type is not appropriate for Business applied applications.
Type SizeDOUBLE 8FLOAT 4
13
ZEROFILL and UNSIGNED field attributesData type Original value Value stored Value displayedINT 99 99 99INT -99 -99 -99 INT UNSIGNED 99 99 99INT UNSIGNED -99 None NoneINT ZEROFILL 99 99 0000000099INT(4) ZEROFILL 99 99 0099
14
Enumerated typeAn enumerated type allows the creator of a table to limit the
values of a field. This concept is closer to Edgar Codd’s definition of a domain.
The values for an enumerated set are stored as integer values.
The issue is all values of the enumerated needs to be known as table create time.
EXAMPLE:CREATE TABLE toy ( toy_type ENUM (‘block’, ‘peg’, ‘arch’)
PRIMARY KEY);
15
SET typeAn SET type allows the creator of a table to limit the values of
a field to any combinations of the values in a set. This concept is goes against Edgar Codd’s definition of an atomic values for domain values.
Like enumerated type, all values need to be known as table create time and the values are stored as bitmask values. Each value is assigned a specific bit.
EXAMPLE:CREATE TABLE toy ( type SET (‘block’, ‘peg’, ‘arch’)
PRIMARY KEY);
16
Summary
In this module you learned:• CREATE TABLE AS • DROP• Common variable types in MySQL
Relational Data Model