Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.

Similar presentations


Presentation on theme: "1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL."— Presentation transcript:

1 1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL

2 2 SQL Data Types Numeric  INTEGER, INT, SMALLINT MySQL: TINYINT (1 BYTE), SMALLINT (2 BYTE), MEDIUMINT (3 BYTE), INT or INTEGER (4 BYTE), BIGINT (8 BYTE), both SIGNED and UNSIGNED  FLOAT, REAL, DOUBLE PRECISION MySQL: FLOAT (single precision), DOUBLE, REAL, DOUBLE PRECISION (double precision)  DECIMAL, DEC, NUMERIC(I, J) I is number of digits, J is number following decimal point MySQL: DECIMAL, DEC, NUMERIC, FIXED

3 3 SQL Data Types Character String  CHAR(n) or CHARACTER(n)  VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n) MySQL:  CHAR(n), VARCHAR(n) - n <= 255  BINARY, VARBINARY - same except binary  xBLOB, xTEXT (x = TINY, (none), MEDIUM, LONG - controls maximum size)  ENUM - string object with restricted set of values, internally stored as integers

4 4 SQL Data Types Boolean  Three valued (True, False, Unknown)  Used to support logic with NULLs  MySQL: Boolean is a synonym for a one-bit TINYINT Full SQL Standard Booleans to be implemented

5 5 SQL Data Types Date and Time Types  DATE: YYYY-MM-DD (Standard and MySQL)  TIME: HH:MM:SS (Standard and MySQL)  DATETIME: YYYY-MM-DD HH:MM:SS (MySQL)  TIMESTAMP: YYYY-MM-DD HH:MM:SS NNNNNN (Includes fractional seconds) (Standard) (MySQL - no fractional seconds)

6 6 CREATE SCHEMA CREATE DATABASE Standard: CREATE SCHEMA schema_name AUTHORIZATION user_name;  Create the named schema administered by the named user MySQL: CREATE DATABASE [IF_NOT_EXISTS] database_name;  Create named database. User must have requisite privileges  CREATE SCHEMA is being added in MySQL 5.0

7 7 Basic CREATE TABLE CREATE TABLE table_name … CREATE TABLE schema_name.table_name …  Create the named table MySQL:  CREATE [TEMPORARY] TABLE table_name … table_name includes database_name.table_name

8 8 Basic CREATE TABLE CREATE TABLE table_name (column_def, … ); Where column_def is: column_name column_type [NOT NULL | NULL] [DEFAULT value] [CHECK expr]  NULL is default  DEFAULT value is default for type if not specified An insert which doesn’t specify a value for a column will: 1. Use the DEFAULT value if that’s specified 2. Use NULL is that’s allowed 3. Use the default for type if that’s allowed (switch/option) 4. Return an error CHECK expr causes the expression to be checked when the column is changed (not in MySQL)

9 9 Some More CREAT TABLE CREATE TABLE table_name ( column_def, …, column_def, create_def, …, create_def)  Technically: A column_def is a create_def

10 10 Create Definitions Constraints  CONSTRAINT constraint_name PRIMARY KEY (col_name, … col_name)  CONSTRAINT constraint_name UNIQUE (col, … col) Alternate Key  CONSTRAINT constraint_name FOREIGN KEY (col, … col) REFERNCES table(col, … col) [ ON DELETE … ] [ ON UPDATE …]

11 11 Create Definitions Others  INDEX name (col, … col) Create an (non-unique) Index  CHECK(expr) Check that expr holds when table is modified MySQL: Parsed but ignored (ouch!) And many, many others …  Includes DB Engine specific options for all DBMSs

12 12 DROP DROP TABLE DROP SCHEMA | DATABASE  Optional IF EXISTS Delete whatever

13 13 ALTER TABLE ALTER TABLE table_name alter_spec, … alter_spec  ADD COLUMN, CONSTRAINT, INDEX Use the definition as in CREATE TABLE  DROP COLUMN, CONSTRAINT, INDEX name  ALTER COLUMN name SET DEFAULT value  ALTER COLUMN name DROP DEFAULT  RENAME new_table_name And a host of others

14 14 Guidance Create a shell script that looks something like DROP TABLE table_name IF EXISTS; CREATE TABLE table_name … LOAD DATA INFILE …  Load test data May include DROP DATABASE, CREATE DATABASE if you have the privileges Edit and run the shell script when you need to recreate the database Reserve ALTER, … for fixing databases when they reach the point where the above won’t work

15 15 Guidance for Creating Databases


Download ppt "1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL."

Similar presentations


Ads by Google