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

Similar documents
Chapter 3 Introduction to relational databases and MySQL

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

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

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

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

Chapter 8 How to work with data types

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

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

Data and Tables. Bok, Jong Soon

Overview of MySQL Structure and Syntax [2]

Create Basic Databases and Integrate with a Website Lesson 1

Data Definition Language with mysql. By Kautsar

Basis Data Terapan. Yoannita

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

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

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

Simple Quesries in SQL & Table Creation and Data Manipulation

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

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)

*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.

Programming and Database Fundamentals for Data Scientists

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

Basic SQL. Basic SQL. Basic SQL

MySQL: an application

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

Deepak Bhinde PGT Comp. Sc.

MySQL Creating a Database Lecture 3

Advanced SQL. Nov 21, CS445 Pacific University 1

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

Unit 1 - Chapter 4,5

DATABASE MANAGEMENT SYSTEMS

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

Acknowledgments About the Authors

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

SQL: Data Definition Language

EE 495M - Lecture 3. Overview

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

MTAT Introduction to Databases

Vidya Pratishthan s College Of Engineering, Baramati. Laboratory Manual Database Management System Laboratory

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

Downloaded from

DB Programming. Database Systems

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

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

How to use SQL to create a database

MySQL Introduction. By Prof. B.A.Khivsara

MySQL Introduction. By Prof. B.A.Khivsara

Standard Query Language: Current standard Logical Schema Design: Schema Definition with SQL (DDL)

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

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

Exact Numeric Data Types

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

The Top 20 Design Tips

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Model Question Paper. Credits: 4 Marks: 140

GridDB Advanced Edition SQL reference

Full file at

Lecture 5. Monday, September 15, 2014

Data Definition Language (DDL)

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Lab # 4. Data Definition Language (DDL)

MTAT Introduction to Databases

UNIT V ESTABLISHING A DATABASE CONNECTION AND WORKING WITH DATABASE

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

Chapter 9: MySQL for Server-Side Data Storage

Lecture 07. Spring 2018 Borough of Manhattan Community College

CSC Web Programming. Introduction to SQL

SAS/ACCESS Supplement for MySQL. SAS/ACCESS for Relational Databases


SQL Data Definition Language

SQL Introduction. CS 377: Database Systems

Lab 4: Tables and Constraints

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Full file at

Database Management Systems by Hanh Pham GOALS

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

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Module 3 MySQL Database. Database Management System

SQL Structured Query Language Introduction

4) PHP and MySQL. Emmanuel Benoist. Spring Term Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1

Creating and Managing Tables Schedule: Timing Topic

Functional Logic Programming with Databases

SQL DATA DEFINITION LANGUAGE


MySQL and MariaDB. March, Introduction 3

SQL Commands & Mongo DB New Syllabus

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 1. Introduction to SQL Server Panayiotis Andreou

SQL. Char (30) can store ram, ramji007 or 80- b

Introduction to IBM DB2

Transcription:

Lab # 2 Hands-On DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia Part A: Demo by Instructor in Lab a. Data type of MySQL b. CREATE table c. ALTER table (ADD, CHANGE, MODIFY and DROP columns) d. RENAME table e. DROP table Part B: Hands-on by Students in Lab Part A: Demo by Instructor in Lab a. Data type of MySQL Properly defining the fields in a table is important to the overall optimization of your database. You should use only the type and size of field; Don't define a field as 30 or so characters wide if you know you're only going to use 2 or 3 characters. MySQL uses many different data types broken into three categories: numeric, date and time, and string types. Numeric Types are: TINYINT: A very small integer that can be signed or unsigned. If signed, the allowable range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify a width of up to 4 digits. SMALLINT: A small integer that can be signed or unsigned. If signed, the allowable range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You can specify a width of up to 5 digits. MEDIUMINT: A medium-sized integer that can be signed or unsigned. If signed, the allowable range is from -8388608 to 8388607. If unsigned, the allowable range is from 0 to 16777215. You can specify a width of up to 9 digits. INT: A normal-sized integer that can be signed or unsigned. If signed, the allowable range is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295. You can specify a width of up to 11 digits. Page 1 of 5

BIGINT: A large integer that can be signed or unsigned. If signed, the allowable range is from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is from 0 to 18446744073709551615. You can specify a width of up to 20 digits. FLOAT(M,D): A floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). Decimal precision can go to 24 places for a FLOAT. DOUBLE(M,D): A double precision floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). Decimal precision can go to 53 places for a DOUBLE. DECIMAL(M,D): An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. Date and Time Types are: DATE: A date in YYYY-MM-DD format. For example, February 29th, 2016 would be stored as 2016-02-29. DATETIME: A date and time combination in YYYY-MM-DD HH:MM:SS format. For example, 2:30 in the afternoon on February 29th, 2016 would be stored as 2016-02-29 14:30:00. TIMESTAMP: A timestamp, this looks like the previous DATETIME format, only without the hyphens between numbers; 4:30 in the afternoon on February 29th, 2016 would be stored as 20160229143000 ( YYYYMMDDHHMMSS). TIME: Stores the time in HH:MM:SS format. YEAR(M) - Stores a year in 2-digit or 4- digit format. If the length is specified as 2 (for example YEAR(2)), YEAR can be 16, If the length is specified as 4, YEAR can be 2016. The default length is 4. String Types are: CHAR(M): A fixed-length string between 1 and 255 characters in length (for example CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is not required, but the default is 1. VARCHAR(M): A variable-length string between 1 and 255 characters in length; for example VARCHAR(25). You must define a length when creating a VARCHAR field. TINYBLOB or TINYTEXT: A BLOB or TEXT column with a maximum length of 255 characters. You do not specify a length with TINYBLOB or TINYTEXT. Page 2 of 5

MEDIUMBLOB or MEDIUMTEXT: A BLOB or TEXT column with a maximum length of 16777215 characters. You do not specify a length with MEDIUMBLOB or MEDIUMTEXT. BLOB or TEXT: A field with a maximum length of 65535 characters. BLOBs are "Binary Large Objects" and are used to store large amounts of binary data, such as images or other types of files. LONGBLOB or LONGTEXT: A BLOB or TEXT column with a maximum length of 4294967295 characters. You do not specify a length with LONGBLOB or LONGTEXT. ENUM: An enumeration, which is a fancy term for list. When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL). For example, if you wanted your field to contain "A" or "B" or "C", you would define your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that field. b. CREATE table: Here is generic SQL syntax to create a MySQL table: CREATE TABLE table_name (column_name column_type); CREATE TABLE users ( username VARCHAR (30), password VARCHAR (20) ); Now, we will create following table in mydatabase. CREATE TABLE faculty_tbl ( id INT NOT NULL AUTO_INCREMENT, teacher_name VARCHAR(50) NOT NULL, teacher_salary float(10,2) NOT NULL, Joining_date DATE, PRIMARY KEY (id) ); Page 3 of 5

Explanation of some above mentioned items: NOT NULL: Field Attribute NOT NULL is being used because we do not want this field to be NULL. So if user will try to create a record with NULL value, then MySQL will raise an error. AUTO_INCREMENT: Field Attribute AUTO_INCREMENT tells MySQL to go ahead and add the next available number to the id field. PRIMARY KEY: Keyword PRIMARY KEY is used to define a column as primary key. You can use multiple columns separated by comma to define a primary key. See the description of the table. DESC faculty_tbl Or SHOW COLUMNS FROM faculty_tbl c. ALTER table: To add a new column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype Add new Column: ADD degree VARCHAR(30) NULL ADD DateOfBirth year Change existing Column s datatype: MODIFY COLUMN DateOfBirth date Change existing Column s Name and datatype: CHANGE COLUMN DateOfBirth YearOfBirth year Drop existing Column from table: DROP COLUMN degree Page 4 of 5

d. RENAME table Rename table syntax: RENAME TABLE tbl_name TO new_tbl_name RENAME TABLE faculty_tbl TO UT_FacultyTable e. DROP table Drop table form database. Drop table syntax: DROP TABLE UT_FacultyTable Part B: Hands-on by Students in Lab a. Create a table EmployeeTable in UT_Database database with following structure: i. EmpNo NOT NULL number, Primary Key, Auto Generated ii. EmpName VarChar(40) NOT NULL iii. Job NOT NULL Char(10) iv. DeptNo NOT NULL number v. PHONE_NO number vi. YearOfEnrollment year vii. Percentage float (two number, two decimal points) b. Add two more columns in EmployeeTable i. EmailAddress NOT NULL VARCHAR (20) ii. PostalAddress VARCHAR(100) c. Change column Job as Position datatype with VARCHAR(15) d. Change column PhoneNo with datatype VARCHAR (10) e. Delete column DeptNo from the table f. Finally, rename table with name UT_EmployeeTable Page 5 of 5