Data and Tables. Bok, Jong Soon

Similar documents
TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Basic SQL. Basic SQL. Basic SQL

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Chapter 8 How to work with data types

Overview of MySQL Structure and Syntax [2]

Basis Data Terapan. Yoannita

GridDB Advanced Edition SQL reference

MySQL: an application

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

Careerarm.com. 1. What is MySQL? MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)

Tool/Web URL Features phpmyadmin. More on phpmyadmin under User Intefaces. MySQL Query Browser

GridDB Advanced Edition SQL reference

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Data Definition Language with mysql. By Kautsar

*this is a guide that was created from lecture videos and is used to help you gain an understanding of what is mysql used for.

Like all programming models, MySQL identifiers follow certain rules and conventions.

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Exact Numeric Data Types

Unit 1 - Chapter 4,5

General References on SQL (structured query language) SQL tutorial.

CIS 363 MySQL. Chapter 4 MySQL Connectors Chapter 5 Data Types Chapter 6 Identifiers

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

Programming and Database Fundamentals for Data Scientists

Chapter 3 Introduction to relational databases and MySQL

Lab # 4. Data Definition Language (DDL)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

SQL. Often times, in order for us to build the most functional website we can, we depend on a database to store information.

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

Oracle SQL Developer. Supplementary Information for MySQL Migrations Release 2.1 E

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

The type of all data used in a C (or C++) program must be specified

MTAT Introduction to Databases

Simple Quesries in SQL & Table Creation and Data Manipulation

SQL DDL II. CS121: Relational Databases Fall 2017 Lecture 8

SQL Data Definition Language

The type of all data used in a C++ program must be specified

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Model Question Paper. Credits: 4 Marks: 140

MySQL by Examples for Beginners

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Visual C# Instructor s Manual Table of Contents

Lecture 5. Monday, September 15, 2014

THE UNIVERSITY OF AUCKLAND

Using IBM-Informix datatypes with IDS 10 and web application server Keshava Murthy, IBM Informix Development

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Acknowledgments About the Authors

Data types String data types Numeric data types Date, time, and timestamp data types XML data type Large object data types ROWID data type

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lab 4: Tables and Constraints

Advanced SQL. Nov 21, CS445 Pacific University 1

Full file at

MySQL Introduction. By Prof. B.A.Khivsara

1) Introduction to SQL

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Unit 3. Constants and Expressions

MySQL Creating a Database Lecture 3

CMP-3440 Database Systems

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Introduction to MySQL /MariaDB and SQL Basics. Read Chapter 3!

CSC Web Programming. Introduction to SQL

SQLITE - INSTALLATION

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

MTAT Introduction to Databases

Lecture 07. Spring 2018 Borough of Manhattan Community College

Tables From Existing Tables

Create Basic Databases and Integrate with a Website Lesson 1

Programming in C++ 4. The lexical basis of C++

How to use SQL to create a database

SQL: Data Definition Language

SQL functions fit into two broad categories: Data definition language Data manipulation language

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Java Notes. 10th ICSE. Saravanan Ganesh

Database Programming with PL/SQL

The Top 20 Design Tips

CIS 363 MySQL. Chapter 10 SQL Expressions Chapter 11 Updating Data


Percona XtraDB: Compressed Columns with Dictionaries an alternative to InnoDB table compression. Yura Sorokin, Senior Software Engineer at Percona

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Database Management Systems,

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

SQL DATA MANIPULATION. Prepared By: Vipul Vekariya M.E( Computer) Gardi College Of Eng. & Tech. Rajkot.

Relational Database Management Systems for Epidemiologists: SQL Part I

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Database Applications and Web-Enabled Databases. University of California, Berkeley School of Information IS 257: Database Management

Lecture 1. Types, Expressions, & Variables

SQL Introduction. CS 377: Database Systems

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Transcription:

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