Data and Tables Bok, Jong Soon Jongsoon.bok@gmail.com www.javaexpert.co.kr
Learning MySQL Language Structure Comments and portability Case-sensitivity Escape characters Naming limitations Quoting Time zones Character sets and collations
Comments -- : Single line comment /* ~ */ : Multi line comment
Case-sensitivity Traditionally, SQL reserved words are written in uppercase, such as SELECT, FROM, NULL, and AS. SQL reserved words are case-insensitive. Case-sensitivity for names is determined by whether or not the file system is case-sensitive for Databases, Tables, Views, Aliases, Triggers. Windows OS is case-insensitive.
is the backslash (\). Escape Escape Characters Meant \n New line \t Tab \b Back space \r Carriage return \\ Back slash \ Single quote mark \ Double quote mark \_ Wildcard character for one character \% Wildcard character for one or more characters \0 and \Z print ASCII 0 (NUL) and ASCII 26 (Ctrl-Z) In Windows, Ctrl-Z is a special character marking an end of a file, and on Mac OS X and Unix, Ctrl-Z is the special key sequence that will suspend the current foreground process.
Naming Limitations and Quoting Identifiers are all limited to 64 characters except aliases. Aliases are limited to 255 characters. Characters may be one or more bytes. Identifiers are stored using the utf8 character set. Like numbers, the reserved words TRUE, FALSE, and NULL do not need to be quoted. As with other reserved words, TRUE, FALSE, and NULL are case-insensitive.
Dot Notation MySQL has a special dot notation that can be used to specify a database when referring to a table. Simply place a dot character (.) between the database and table name. SELECT user, host, password FROM mysql.user;
Time Zones When mysqld starts, it determines the time zone of the operating system and sets the system_time_zone system variable accordingly. By default, mysqld sets the value of time_zone to SYSTEM. Fields with a TIMESTAMP value are converted to UTC and stored. DATE, TIME, and DATETIME fields are not converted and stored as UTC. http://dev.mysql.com/doc/refman/6.0/en/time-zone-support.html
Character sets and collations MySQL supports many different character sets and collations. A character set, or charset, is the set of available characters. A collation specifies the lexical sort order. In English the lexical sort order begins with a, b, c, d. In Spanish the lexical sort order begins with a, b, c, ch, d. In Greek the lexical sort order begins with α, β, χ, δ.
Defining Your Data
Defining Your Data (Cont.) Is there a better way to organize this information? What would you do?
Defining Your Data (Cont.) Well, how about a database? Right? Exactly right. A database is just what we need.
Defining Your Data (Cont.)
Look at your data in Categories Cut each note into pieces: Cut up another sticky note with the categories :
Look at your data in Categories (Cont.)
What s in a database? A database is a container that holds tables and other SQL structures related to those tables.
What s in a database? (Cont.)
What s in a database? (Cont.) The information inside the database is organized into tables.
Tables up close
Tables up close (Cont.) A column is a piece of data stored by your table. A row is a single set of columns that describe attributes of a single thing. Columns and rows together make up a table.
Connecting to the Server To connect to the Server with terminal. shell>mysql h host u user p Enter password : ******
Connecting to the Server (Cont.) To connect to the Server with MySQL Workbench.
Connecting to the Server (Cont.) To connect to the Server with SQLGate2010.
Create Database CREATE DATABASE database_name
Create Database (Cont.) mysql>show databases;
Select a Database mysql>use database_name;
Select a Database (Cont.) shell>mysql u root p gregs_list
Create Table
Create Table (Cont.) CREATE TABLE table_name ( col_name data_type[(length)] [NOT NULL NULL] [DEFAULT dafalut_value] [AUTO_INCREMENT] [UNIQUE [KEY] [PRIMARY] KEY] [CONSTRAINT] PRIMARY KEY [CONSTRAINT] UNIQUE [INDEX KEY] [CONSTRAINT] FOREIGN KEY CHECK (expr) )
Create Table (Cont.)
Create Table (Cont.)
Create Table (Cont.) See produced output. Verify your table
Take a meeting with some data types
Which Data-type?
MySQL Data Type In order to store, retrieve, or process data. MySQL includes many of the ISO SQL:2003 standard data types, and adds in more data types. The ISO SQL:2003 standard defines seven categories of data types: Character String Types National Character String Types Binary Large Object String Types Numeric Types Boolean Types Datetime Types Interval Types
String Data Types Data Type Name SQL Standard? Fixed/Variable Length Range Attributes CHAR Yes Fixed 0~255 A,B,C,S,D,U VARCHAR Yes Variable 0~255 A,B,C,S,D,U TINYTEXT No Variable 255 Bytes A,B,C,S,U TEXT No Variable 65,535 (64KB) MEDIUMTEXT No Variable 16,777,215 (16MB) LONGTEXT No Variable 4,294,967,295 (4GB) A,B,C,S,U A,B,C,S,U A,B,C,S,NN,N,U A:ASCII, B:BINARY, C:CHARACTER, S:SETCOLLATION, D:DEFAULT, U:UNICODE, NN:NOT NULL, N:NULL
CHAR vs. VARCHAR Value CHAR(4) Storage Required VARCHAR(4) Storage Required 4 Bytes 1 Bytes ab ab 4 Bytes ab 3 Bytes abcd abcd 4 Bytes abcd 5 Bytes abcdefgh abcd 4 Bytes abcd 5 Bytes
Binary Data Types Data Type Name SQL Standard? Fixed/Variable Length Range Attributes BINARY No Fixed 0~255 Bytes D, NN, N VARBINARY No Variable 0~65,532 Bytes D, NN, N TINYBLOB No Variable 255 Bytes NN, N BLOB No Variable 65,535 (64KB) MEDIUMBLOB No Variable 16,777,215 (16MB) LONGBLOB No Variable 4,294,967,295 (4GB) NN, N NN, N NN,N D:DEFAULT, NN:NOT NULL, N:NULL
Numeric Data Types Data Type Name SQL Standard? Fixed/Variable Length Range Attributes NUMERIC Yes Fixed A,D,NN,NS,U,Z BECIMAL Yes Fixed D,NN,NS,U,Z TINYINT No Fixed 1 Bytes* A,D,NN,N,SDV,S,U,Z SMALLINT Yes Fixed 2 Bytes** A,D,NN,N,SDV,S,U,Z MEDIUMINT No Fixed 3 Bytes*** A,D,NN,N,SDV,S,U,Z INT Yes Fixed 4 Bytes**** A,D,NN,N,SDV,S,U,Z *-128~127(0~255) **-32768~32767(0~65535) ***-8388608~8388607(0~16777215) ****-2147483648~2147483647(0~4294967295) A:AUTO_INCREMENT, D:DEFAULT, NN:NOT NULL, N:NULL, NS:NULL SIGNED, U:UNSIGNED, Z:ZEROFILL, SDV:SERIAL DEFAULT VALUE
Numeric Data Types (Cont.) Data Type Name SQL Standard? Fixed/Variable Length Range Attributes BIGINT Yes Fixed 8 Bytes* A,D,NN,N,SDV,S,U,Z FLOAT Yes Fixed 4 Bytes A,D,NN,NS,U,Z DOUBLE Yes Fixed 8 Bytes A,D,NN,NS,U,Z BIT No Variable 1~64bit D,NN,N REAL Yes REAL is an alias for DOUBLE or FLOAT SERIAL No SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE KEY. *-9223372036854775808~ 9223372036854775807(0~18446744073709551615) A:AUTO_INCREMENT, D:DEFAULT, NN:NOT NULL, N:NULL, NS:NULL SIGNED, U:UNSIGNED, Z:ZEROFILL, SDV:SERIAL DEFAULT VALUE
Floating-Point Data Types The FLOAT and DOUBLE data types are used to represent approximate numeric data values. For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses. A precision from 0 to 23 results in a four-byte single-precision FLOAT column. A precision from 24 to 53 results in an eight-byte double precision DOUBLE column.
Floating-Point Data Types(Cont.) MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE (M,D). (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.
DECIMAL & NUMERIC The DECIMAL and NUMERIC data types are used to store exact numeric data values. In MySQL, NUMERIC is implemented as DECIMAL. These types are used to store values for which it is important to preserve exact precision, for example with monetary data. MySQL 5.1 stores DECIMAL and NUMERIC values in binary format. Before MySQL 5.0.3, they were stored as strings.
DECIMAL & NUMERIC (Cont.) Salary DECIMAL(5,2) 5 is the precision and 2 is the scale. The precision represents the number of significant digits The scale represents the number of digits that can be stored following the decimal point. In this case, the range of values that can be stored in the salary column is from -999.99 to 999.99. The default value of is precision 10. The maximum number of digits for DECIMAL or NUMERIC is 65
Boolean Data Types The ISO SQL:2003 standard defines a boolean data type of BOOLEAN. MySQL supports the standard and adds a nonstandard abbreviation of BOOL. However, MySQL implements BOOLEAN as an alias for TINYINT(1).
Data Type Name Datetime Data Types Supported Range (Guaranteed to Work) DATE 1000-01-01 ~ 9999-12-31 DATETIME 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 TIMESTAMP* 1970-01-01 00:00:01 ~ 2038-01-19 03:14:07 Size Zero Value 3 Bytes 0000-00-01 8 Bytes 0000-00-00 00:00:00 4 Bytes 0000-00-00 00:00:00 TIME -838:59:59 ~ 838:59:58 3 Bytes 00:00:00 YEAR(2) 70 ~ 69 (1970 ~ 2069) 1 Bytes 00 YEAR(4) 1901 ~ 2155 1 Bytes 0000 * TIMESTAMP values are stored as the number of seconds since the epoch ('1970-01-01 00:00:00' UTC).
Create Table (Cont.)
Create Table (Cont.)
Alter Table ALTER TABLE table_name ADD COLUMN col_name column_definition [FIRST AFTER col_name] ADD [CONSTRAINT] PRIMARY KEY ADD [CONSTRAINT] UNIQUE [ INDEX KEY ] ADD [CONSTRAINT] FOREIGN KEY ALTER [COLUMN] col_name {SET DEFAULT value DROP DEFAULT} CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST AFTER col_name] MODIFY [COLUMN] col_name column_definition [FIRST AFTER col_name] DROP [COLUMN] col_name DROP PRIMARY KEY DROP FOREIGN KEY RENAME [TO] new_table_name
DROP Table, Database DROP TABLE table_name DROP DATABASE database_name
CREATE TABLE, USE DATABASE, CREATE TABLE, DESC, DROP TABLE, CHAR, VARCHAR, BLOB, DATE, DATETIME, DEC, INT 1. I ve got your number. 2. I can dispose of your unwanted tables. 3. T or F questions are my favorite. 4. I keep track of your mom s birthday. 5. I got the whole table in my hands. 6. Numbers are cool, but I hate fractions. 7. I like long, wordy explanations. 8. This is the place to store everything. 9. The table wouldn t exist without me. 10. I know exactly when your dental appointment is next week. 11. Accountants like me. 12. I can give you a peek at your table format. 13. Without us, you couldn t even create a table.
Insert Data into Table
INSERT INSERT [INTO] table_name [(col_name, )] { VALUES VALUE } ( { expr DEFAULT }, ), ( ),
Insert Data into Table (Cont.)
Create the INSERT statement
Create the INSERT statement (Cont.)
Variations on an INSERT statement There re three variations of INSERT statements you should know about. Changing the order of columns Omitting column names Leaving some columns out
Variations on an INSERT statement (Cont.) Changing the order of columns Can change the order of column names, as long as the matching values for each column come in that same order.
Variations on an INSERT statement (Cont.) Omitting column names Can leave out the list of column names, but the values must be all there, and all in the same order that you added the columns in.
Variations on an INSERT statement (Cont.) Leaving some columns out Can insert a few columns and leave some out.
Columns without values
Columns without values (Cont.)
Peek at your table with the SELECT statement
Controlling your inner NULL
NOT NULL appears in DESC
NOT NULL appears in DESC (Cont.)
Fill in the blanks with DEFAULT
Fill in the blanks with DEFAULT (Cont.)
Tablecross
Review
CREATE TABLE, USE DATABASE, CREATE TABLE, DESC, DROP TABLE, CHAR, VARCHAR, BLOB, DATE, DATETIME, DEC, INT 1. I ve got your number. DEC, INT 2. I can dispose of your unwanted tables. DROP TABLE 3. T or F questions are my favorite. CHAR(1) 4. I keep track of your mom s birthday. 5. I got the whole table in my hands. CREATE DATABASE 6. Numbers are cool, but I hate fractions. INT 7. I like long, wordy explanations. BLOB 8. This is the place to store everything. 9. The table wouldn t exist without me. CREATE DATABASE 10. I know exactly when your dental appointment is next week. 11. Accountants like me. DEC 12. I can give you a peek at your table format. DESC 13. Without us, you couldn t even create a table. CREATE DATABASE CREATE TABLE DROP TABLE DATE CREATE TABLE DATETIME