creating and managing tables

18
Creating and Managing Tables

Upload: byron

Post on 08-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Creating and Managing Tables. Objectives. At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe the datatypes that can be used when specifying column definition Alter table definitions Drop, rename and truncate tables. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Creating and Managing Tables

Creating and Managing Tables

Page 2: Creating and Managing Tables

Objectives

At the end of this lesson, you will be able to:

Describe the main database objectsCreate tablesDescribe the datatypes that can be used when specifying column definitionAlter table definitionsDrop, rename and truncate tables

Page 3: Creating and Managing Tables

Naming Conventions

Can be 1–64 characters longMust contain only A–Z, a–z, 0–9, _ , and $Must not duplicate the name of another object owned by the same userMust not be a MySQL reserved wordMust begin with a letter or digit

Required

Page 4: Creating and Managing Tables

Naming Conventions

Field names should be descriptive.Use the underscore (_) to separate words.Use entirely lowercase words (this is a personal preference rather than a rule).Use plural table names (to indicate multiple values stored) and singular column names.End primary and foreign key columns with id (or ID)List the primary key first in a table, followed by foreign keys.

Suggested

Page 5: Creating and Managing Tables

The CREATE TABLE Statement

You must have :CREATE TABLE privilegeA storage area

You specify:Table nameColumn name, column datatype, and column size

CREATE TABLE [schema.]table (column datatype [DEFAULT expr]);

Page 6: Creating and Managing Tables

The DEFAULT Option

Specify a default value for a column during an insert.

Legal values are literal value, expression, or MySQL function.Illegal values are another column’s name or pseudocolumn.The default datatype must match the column datatype.

… hire_date DATE DEFAULT CURDATE(), …

Page 7: Creating and Managing Tables

Creating TablesCreating the table

Confirm table creation

MySQL> CREATE TABLE dept -> (dept_nbr INTEGER(2), -> dept_name VARCHAR(14), -> location VARCHAR(13));Table created.

MySQL> DESCRIBE dept

Name Null? Type --------------------------- -------- --------- dept_nbr INTEGER(2) dept_name VARCHAR(14) locaton VARCHAR(13)

Page 8: Creating and Managing Tables

Querying the Data Dictionary

Show tables owned by the user.

MySQL> SHOW TABLES;

Page 9: Creating and Managing Tables

DatatypesNumeric Types

Type Size Description

TINYINT [Length] 1 byte Range of -128 to 127 or 0 to 255 unsigned.

SMALLINT [Length] 2 bytes Range of -32, 768 to 32,767 or 0 to 65535 unsigned.

MEDIUMINT [Length] 3 bytes Range of -8, 388, 608 to 8, 388, 607 or 0 to 16, 777, 215 unsigned

INT [Length] 4 bytes Range of -2, 147, 483, 648 to 2, 147, 483, 647 or 0 to 4, 294, 967, 295 unsigned

BIGINT [Length] 8 bytes Range of -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 774, 807 or 0 to 18, 446, 744, 073, 709, 551, 615 unsigned

FLOAT [Length, Decimals] 4 bytes A small number with a floating decimal point.

DOUBLE [Length, Decimals] 8 bytes A large number with a floating decimal point.

DECIMAL [Length, Decimals] Length + 1 or Length + 2 bytes

A DOUBLE with a fixed decimal point.

Bold item most frequently used in Business Environments

Page 10: Creating and Managing Tables

DatatypesText TypesType Size Description

CHAR [Length] Length bytes A fixed-length field from 0 to 255 characters long.

VARCHAR [Length] String length + 1 or 2 bytes

A fixed-length field from 0 to 255 characters long. (65,535 characters long as of MyMySQL 5.0.3)

TINYTEXT String length + 1 bytes A string with a maximum length of 255 characters.

TEXT String length _ 2 bytes A string with a maximum length of 65,535 characters.

MEDIUMTEXT String length + 3 bytes A string with a maximum length of 16,777,215 characters.

LONGTEXT String length + 4 bytes A string with a maximum length of 4,294,967,295 characters.

BINARY [Length] Length bytes Similar to CHAR but stores binary data.

VARBINARY [Length] Data length + 1 bytes Similar to VARCHAR but stores binary data.

TINYBLOB Data length + 1 bytes Stores binary data with a maximum length of 255 bytes.

BLOB Data length + 2 bytes Stores binary data with a maximum length of 65,535 bytes.

MEDIUM BLOB Data length + 3 bytes Stores binary data with a maximum length 16,777,215 bytes.

LONGBLOB Data length + 4 bytes Stores binary data with a maximum length of 4,294,967,295 bytes.

ENUM 1 or 2 bytes Short for enumeration, which means that each column can have one of several possible values.

SER 1, 2, 3, 4, or 8 bytes Like ENUM except that each column can have more than one of several possible values.

Bold item most frequently used in Business Environments

Page 11: Creating and Managing Tables

DatatypesDate and Time Types

Type Size Description

DATE 3 bytes In the format of YYYY-MM-DD.

DATETIME 8 bytes In the format of YYYY-MM-DD HH:MM:SS

TIMESTAMP 4 bytes In the format of YYYYMMDDHHMMSS; acceptable range ends in the year 2037.

TIME 3 bytes In the format of HH:MM:SS.

YEAR 1 byte In the format of YYYY, with a range from 1901 to 2155.

Bold item most frequently used in Business Environments

Page 12: Creating and Managing Tables

The ALTER TABLE Statement

Use the ALTER TABLE statement to:Add a new columnModify an existing columnDefine a default value for the new column

ALTER TABLE tableADD (column datatype [DEFAULT expr]

[, column datatype]...);

ALTER TABLE tableMODIFY (column datatype [DEFAULT expr]

[, column datatype]...);

Page 13: Creating and Managing Tables

Adding a Column

Department30employee_nbr name annual_salary hire_date

7698 Blake 34200 2001-05-01 7654 Martin 15000 2001-09-28 7499 Allen 19200 2001-02-20 7844 Turner 18000 2001-09-08...

“…add a newcolumn intoDepartment30 table…”

Department30

job

New column

employee_nbr name annual_salary hire_date

7698 Blake 34200 2001-05-01 7654 Martin 15000 2001-09-28 7499 Allen 19200 2001-02-20 7844 Turner 18000 2001-09-08...

job

Page 14: Creating and Managing Tables

Adding a ColumnYou use the ADD clause to add columns.

The new column becomes the last column.

MySQL>ALTER TABLE dept30 ->ADD (job VARCHAR(9));Table altered.

employee_nbr name annual_salary hire_date job------------ ----- ------------- --------- --- 7698 Blake 34200 2001-05-01 7654 Martin 15000 2001-09-28 7499 Allen 19200 2001-02-20 7844 Turner 18000 2001-09-08...6 rows selected.

Page 15: Creating and Managing Tables

Modifying a ColumnYou can change a column’s datatype, size, and default value.

A change to the default value affects only subsequent insertions to the table.

ALTER TABLE dept30MODIFY (name VARCHAR(15));Table altered.

Page 16: Creating and Managing Tables

Dropping a Table

All data in the table is deleted.Any pending transactions are committed.All indexes are dropped.You cannot roll back this statement.

MySQL> DROP TABLE dept30;Table dropped.

Page 17: Creating and Managing Tables

Dropping a Column

All data in the column is deleted.Any pending transactions are committed.You cannot roll back this statement.

MySQL> ALTER TABLE tablename DROP COLUMN (columname) [CASCADE CONSTRAINTS]

Page 18: Creating and Managing Tables

Changing the Name of an Object

To change the name of a table, view, sequence, or synonym, you execute the RENAME statement.

You must be the owner of the object.

MySQL> RENAME dept TO department;Table renamed.