Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.

Slides:



Advertisements
Similar presentations
Session 2Introduction to Database Technology Data Types and Table Creation.
Advertisements

Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
Database Chapters.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structured Query Language - SQL Carol Wolf Computer Science.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Oracle Data Definition Language (DDL)
Slide 1 of 77 Database, SQL, MySQL, CENG 449 Lecture 7.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Basis Data Terapan Yoannita. SQL Server Data Types Character strings: Data typeDescriptionStorage char(n)Fixed-length character string. Maximum 8,000.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Fox MIS Spring 2011 Database Week 5 SQL basics SELECT, INSERT, UPDATE, DELETE.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Fox MIS Spring 2011 Database Week 6 Mid-term Review.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
Visual Programing SQL Overview Section 1.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
Sql DDL queries CS 260 Database Systems.
CHAPTER 9 SQL อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Fundamentals of DBMS Notes-1.
From: SQL From:
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Data Definition and Data Types
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics SQL – Putting Information Into a Database
STRUCTURED QUERY LANGUAGE
MIS2502: Data Analytics SQL – Putting Information Into a Database
Creating Tables & Inserting Values Using SQL
SQL DATA CONSTRAINTS.
MIS2502: Data Analytics SQL – Putting Information Into a Database
Oracle Data Definition Language (DDL)
MIS2502: Data Analytics SQL – Putting Information Into a Database
Lesson Plan Instructional Objective Learning Objective
MIS2502: Data Analytics SQL – Putting Information Into a Database
Chapter 4 Introduction to MySQL.
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
Database Instructor: Bei Kang.
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
SQL NOT NULL Constraint
SQL (Structured Query Language)
Presentation transcript:

Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise

CREATE DB and TABLE Create a database: CREATE DATABASE database_name Example: CREATE DATABASE my_db Create a table in a database: CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type,.... ) Example: CREATE TABLE Persons ( P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )

SQL Constraints Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). –NOT NULL –UNIQUE –PRIMARY KEY –FOREIGN KEY –CHECK –DEFAULT

CHECK The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id>0) )

DEFAULT The DEFAULT constraint is used to insert a default value into a column. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' )

PRIMARY KEY The PRIMARY KEY constraint uniquely identifies each record in a database table. –Primary keys must contain unique values. –A primary key column cannot contain NULL values. –Each table can have only ONE primary key. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) ) ALTER TABLE Persons ADD PRIMARY KEY (P_Id) –Creates primary key constraint for P_id column –Note: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created). ALTER TABLE Persons DROP PRIMARY KEY –Drops a PRIMARY KEY constraint

FOREIGN KEY A FOREIGN KEY in one table points to a PRIMARY KEY in another table. CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) –Create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already created ALTER TABLE Orders DROP FOREIGN KEY P_Id –Drops a FOREIGN KEY constraint

ALTER TABLE The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. ALTER TABLE table_name ADD column_name datatype constraint(optional) ALTER TABLE table_name CHANGE OLD_COLUMN_NAME NEW_COLUMN_NAME datatype constraint(optional) –Old column name and new column name can be the same. ALTER TABLE table_name DROP COLUMN column_name ALTER TABLE Persons ADD DateOfBirth date ALTER TABLE Persons DROP COLUMN DateOfBirth ALTER TABLE Persons CHANGE DateOfBirth DateOfBirth year

Exercise Create database MISxxxearth Create table ‘MySpring2011’ which has your course enrollment information Spring 2011 –Use { } instead of ( ) –Course ID, Course No, Course Name, Credit, Instructor Name, Day, Time –Use as many constraints as you can –Each constraint is supposed to be correct one in a logical sense Values in some columns are supposed to be unique There should be a primary key Credit should be greater than zero Add one more column ‘Department’ –Department is supposed to be ‘MIS’ by default Insert your course information in the table Useful command: DESCRIBE table_name

ERD – Example

One Possible ERD

Exercise Create database MISxxxsaturn. Create tables for our invoice ERD example –Sample answer is in the previous slide –Decide which data type is assigned for each column –Put necessary constraints into columns –Make necessary primary and foreign keys Insert data of three invoices (next three slides) into corresponding tables Add yourself as a customer in the customer table Add a person next to you as a seller in the seller table Add your favorite book information in the product table

First Invoice

Second Invoice /10/2008 Jason Mraz 72 Spring Street New York, NY Jason Mraz 72 Spring Street New York, NY The Big Short Economics Databases R Amazing IS TOTAL Tax Grand Total The Big Short Economics Databases R Amazing IS TOTAL Tax Grand Total

Third Invoice /4/2008 6/12/2008 Sunny California 610 W. Ash St San Diego, CA Sunny California 610 W. Ash St San Diego, CA To the End of the Land Novel Databases R Amazing IS TOTAL Tax Grand Total To the End of the Land Novel Databases R Amazing IS TOTAL Tax Grand Total

MySQL Data Types (Text Types) Data typeDescription CHAR(size)Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Can store up to 255 characters VARCHAR(size)Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be converted to a TEXT type TINYTEXTHolds a string with a maximum length of 255 characters TEXTHolds a string with a maximum length of 65,535 characters BLOBFor BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data MEDIUMTEXTHolds a string with a maximum length of 16,777,215 characters MEDIUMBLOBFor BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data LONGTEXTHolds a string with a maximum length of 4,294,967,295 characters LONGBLOBFor BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data ENUM(x,y,z,et c.) Let you enter a list of possible values. You can list up to values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. Note: The values are sorted in the order you enter them. SETSimilar to ENUM except that SET may contain up to 64 list items and can store more than one choice

MySQL Data Types (Number Types) Data typeDescription TINYINT(size)-128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis SMALLINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis MEDIUMINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis INT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis BIGINT(size) to normal. 0 to UNSIGNED*. The maximum number of digits may be specified in parenthesis FLOAT(size,d)A small number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DOUBLE(size,d)A large number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter DECIMAL(size,d)A DOUBLE stored as a string, allowing for a fixed decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

MySQL Data Types (Data Types) Data typeDescription DATE()A date. Format: YYYY-MM-DD Note: The supported range is from ' ' to ' ' DATETIME()*A date and time combination. Format: YYYY-MM-DD HH:MM:SS Note: The supported range is from ' :00:00' to ' :59:59' TIMESTAMP()*A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch (' :00:00' UTC). Format: YYYY-MM-DD HH:MM:SS Note: The supported range is from ' :00:01' UTC to ' :14:07' UTC TIME()A time. Format: HH:MM:SS Note: The supported range is from '-838:59:59' to '838:59:59' YEAR()A year in two-digit or four-digit format. Note: Values allowed in four-digit format: 1901 to Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069