other ddl commands - cs 3200 · if a modification to a table would lose tuples the alter command...

16
OTHER DDL Commands Topic 2 Lesson 7 – More commands for table structure modification

Upload: others

Post on 03-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

OTHER DDL Commands

Topic 2Lesson 7 – More commands for table structure modification

Page 2: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

2

Chapter 5, 8, 11 Murach’s MySQL

Page 3: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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

Page 4: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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

Page 5: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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;

Page 6: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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

}

Page 7: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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.

Page 8: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

8

DATA TYPES in MySQL

MySQL supports:• Character• Numeric• Date and time• Large Object (LOB)• Spatial• JSON

Relational Data Model

Page 9: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

9

Character DATA TYPES in MySQL

• CharacterType BytesCHAR(M) Mx4 VARCHAR(M) L+1

M = length + 1

Relational Data Model

Page 10: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

10

INTEGER types and their sizes

Type BytesBIGINT 8INT 4 MEDIUMINT 3SMALLINT 2

TINYINT 1

Page 11: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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.

Page 12: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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

Page 13: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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

Page 14: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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);

Page 15: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

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);

Page 16: OTHER DDL Commands - CS 3200 · If a modification to a table would lose tuples the ALTER command will fail. 8 DATA TYPES in MySQL MySQL supports:

16

Summary

In this module you learned:• CREATE TABLE AS • DROP• Common variable types in MySQL

Relational Data Model