Chapter 8: Working With Databases & Tables o Working with Databases & Tables DDL Component of SQL Databases CREATE DATABASE class; o Represented as directories in MySQL s data storage area o Can t have 2 with same name o Non-alpha must be in quotes using backtick (`) CREATE DATABASE `123`; o IF NOT EXISTS CREATE DATABASE IF NOT EXISTS db1; o Make database current USE class; o To use table from another database prefix with database name SELECT name FROM Assign_1.player; o DROP DATABASE class; ALL contents of database will be deleted DROP DATABASE IF EXISTS class; Tables CREATE TABLE class (field1, field2, o FIELD TYPES o FIELD CONSTRAINTS o PRIMARY KEY o FOREIGN KEY o TABLE TYPE o FIELD TYPES Chapter 5 covers in great detail Summary of the most common on table 8-1 page 178 o FIELD LEVEL CONSTRAINTS NULL / NOTNULL Can field be left empty or must it be filled in AUTO_INCREMENT & TIMESTAMP field types ignore this o They never allow a NULL value o DEFAULT Will become the field value if nothing is entered
o AUTO_INCREMENT Only for INT field types Automatically increments previous value by 1 o TABLE LEVEL CONSTRAINTS follow the field definitions in the CREATE TABLE command INDEXES List of sorted field values used to simplify the task of locating specific records Typically used to facilitate the performance of queries Without an index, each row of a table must be scanned o More impact as table size increases Disadvantages o Takes up additional space Database density o Dual updates for INSERT / UPDATE / DELETE Typically recommended for fields that frequently appear in: WHERE / ORDER BY / GROUP BY clauses Can be single or multiple field Can have more than one per table May be named o If not field name is used May be created without CREATE TABLE command CREATE INDEX name ON player (PlayerNick); o Name as index for PlayerNick field in player table DROP INDEX used to get rid of DROP INDEX name ON player; BLOB & TEXT fields o Mandatory to supply number of characters to be indexed CREATE INDEX synopsis ON books (synopsis(50)); o Specify 50 characters of synopsis to be indexed
INDEX VARIANTS UNIQUE o No duplicate data allowed o May apply to field as well as index o NULL is allowed unless specified with NOTNULL qualifier FULLTEXT o Designed for full-text searching on MyISAM table types o Uses MATCH() & AGAINST() functions to return a similarity score which is used to organize the response o See discussion on pages 183-185 Search Engines o Best to apply these AFTER table has been populated Faster! o Must be rebuilt if changes made to the table/database DROP / ADD REPAIR TABLE class QUICK; PRIMARY KEY Unique identifier for table Design rules of Database Theory Typically for fields where values will rarely change Allows no NULL or duplicate values May be single or multi-field Applied to field name define din table definition Automatically index in MySQL FOREIGN KEY Fundamental basis of Relational Model Fields with the same meaning in multiple tables and serve as a point of commonality Referential integrity Many DBMS do this automatically MySQL only acts this way using INNODB table type o All tables MUST BE INNODB table type
o Fields used in the relationship MUST BE indexed Not automatic o Done for performance to avoid all the resources required to perform all the integrity checks FOREIGN KEY (TeamID) REFERENCES team (TeamID) To remove ALTER TABLE player DROP FOREIGN KEY TeamID; o DROP table will do this as well To find SHOW CREATE TABLE player; Automatic maintenance o Without specifying in the Foreign key clause all foreign key values may conflict with the rules of referential integrity o ON DELETE / ON UPDATE Used to avoid orphan records as a result on the primary key value being deleted / changed. o Qualifiers found in table 8-2 on page 193 o CASCADE Changes made to primary key value will be reflected in foreign key values o SET NULL Changes in primary key values will be set to NULL in corresponding foreign key values o RESTRICT Change / removal of primary key will be halted FOREIGN KEY (TeamID) REFERENCES team (TeamID) ON DELETE CASCADE TABLE TYPE Your option in MySQL MyISAM MySQL default
o Other Table Issues Portable among OS platforms Fixed & variable length tables Supports table sup to 4GB in size Optimized for speed & performance ISAM Still maintained primarily for compatibility with legacy systems Don t offer optimization Not easily portable Can t compress indexes HEAP In-memory tables that used hash-indexes o See page 181 Need to be aware of max_heap_table_size to avoid using up all system RAM Optimized for temporary tables Data stored only for life of server o Lost if server crashes Not used much BerkeleyDB Developed for transaction-safe environment o Commit o Rollback o Concurrency Large table sizes Cannot compress indexes Table path hardwired into table file when created Supplanted by InnoDB InnoDB Fully ACID compliant o Atomicity o Consistency o Isolation o Durability Row and table level locks Oracle like Comparable to MyISAM type with transactional & referential integrity capability MERGE Virtual table created by combining MyISAM tables Uses indexes of component tables Union compatible
o Copying Tables Combine CREATE TABLE and SELECT USE Assign_1; SELECT * FROM team; CREATE TABLE myteams SELECT * FROM team; SELECT * FROM myteams; Use selected fields or WHERE to create a subset CREATE TABLE mee SELECT * FROM team WHERE 0 = 1; CREATE TABLE newteam LIKE team; o Creates empty table Temporary table Exist for length of MySQL session CREATE TEMPORARY TABLE o Modifying Tables ALTER TABLE Maintain existing table fields or indexes ADD o Deleting Tables DROP TABLE IF EXISTS DROP CHANGE / MODIFY o With CHANGE specify old and new field name Can stay the same o With MODIFY just change type without changing field name