May 26, 2005MySQL Tutorial - 11 MySQL Tutorial 1 – How to Use MySQL CSCI 2140 TA: Jiye Li May 26, 2005

Slides:



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

MySQL. To start go to Login details: login: labuser password:macimd15 – There.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Information Systems Today (©2006 Prentice Hall) MySQL 1CS3754 Class Note #8, Is an open-source relational database management system 2.Is fast and.
MySQL. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn The main subsystems in MySQL architecture The different storage.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Visual Programing SQL Overview Section 1.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
What is MySQL? MySQL is a relational database management system (RDBMS) based on SQL (Structured Query Language). First released in January, Many.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
CSE 4701 Chapter 10-1 Chapter 10 6e: Oracle Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
3 A Guide to MySQL.
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
SQL and SQL*Plus Interaction
Insert, Update and the rest…
ORACLE SQL Developer & SQLPLUS Statements
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Data Definition Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

May 26, 2005MySQL Tutorial - 11 MySQL Tutorial 1 – How to Use MySQL CSCI 2140 TA: Jiye Li May 26,

May 26, 2005MySQL Tutorial - 12 Agenda Getting familiar with our network system Introducing MySQL SQL basics create, add, select, modify, delete Exercises (see handout)

May 26, 2005MySQL Tutorial - 13 How to get a UNIX account Sun workstation or ssh program (putty) Log into torch.cs.dal.ca Username: discover Passwd: query Enter your student number Ask Help Desk

May 26, 2005MySQL Tutorial - 14 How to get a Windows account Username: the same as torch account Password: student ID number. Don’t forget ‘B’ is capitalized.

May 26, 2005MySQL Tutorial - 15 What is MySQL? MySQL is a database management system (DBMS) for relational databases Online Manual MySQL is installed on torch Each user ID stands for a database Create tables under this database

May 26, 2005MySQL Tutorial - 16 How to Login to MySQL Go on torch.cs.dal.ca Run mysql to login torch: ~$ mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is to server version: Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> Student ID Lower case

May 26, 2005MySQL Tutorial - 17 How to Logout Use the command exit or quit torch: ~$ mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is to server version: Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> exit Bye torch: ~$

May 26, 2005MySQL Tutorial - 18 Use your database After login MySQL, use your own database before creating tables torch: ~$ mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is to server version: Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use jiye; Database changed mysql> Your user ID

May 26, 2005MySQL Tutorial - 19 SQL Basics Suppose we would like to create a few tables, such as employee table, employer table and payment table, representing a database about employee information.

May 26, 2005MySQL Tutorial How to write commands in MySQL How to create a table Primary Keys and Foreign Keys How to add records How to select records How to modify records How to delete records

May 26, 2005MySQL Tutorial How to create tables Use create create table (column_name data_type [not null] …, column_name data_type [not null], primary key (column_name)); To show the structure of the table describe ;

May 26, 2005MySQL Tutorial Example mysql> create table employee ( empno smallint(4) not null auto_increment, name char (8) not null, job char (4), salary int (8) not null, deptno int (4) not null, primary key (empno) );

May 26, 2005MySQL Tutorial Primary Key Primary Key is a column or set of columns Uniquely identifies the rest of the data in any given row. For Example: in the employee table, employee number is the primary key.

May 26, 2005MySQL Tutorial Foreign Key A foreign key is a column in a table This column is a primary key of another table Any data in a foreign key column must have corresponding data in the other table constraints.html

May 26, 2005MySQL Tutorial Foreign Key The goal of using foreign keys is that, tables can be related without repeating data Note that foreign keys in SQL are used to check and enforce referential integrity, not to join tables. If you want to get results from multiple tables from a SELECT statement, you do this by performing a join between them: SELECT * FROM t1, t2 WHERE t1.id = t2.id;

May 26, 2005MySQL Tutorial employer Example – Create table with foreign keys create table employee2 ( empno smallint(4) not null, salary float, primary key (empno) ) type = innodb; create table employer ( id smallint(4), employee_no smallint(4), index employ_ind (employee_no), foreign key(employee_no) references employee2(empno) on delete cascade) type=innodb; empno (PK) salary idemployee_no(FK) employee2

May 26, 2005MySQL Tutorial The Syntax of a Foreign Key Constraint Definition [CONSTRAINT [symbol]] FOREIGN KEY (index_col_name,...) REFERENCES table_name (index_col_name,...) [ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}] [ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}] Both tables have to be InnoDB type InnoDB provides MySQL with a transaction-safe storage engine with commit, rollback, and crash recovery capabilities. overview.html

May 26, 2005MySQL Tutorial The Syntax of a Foreign Key Constraint Definition InnoDB rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table without a matching candidate key value in the parent table. CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. SET NULL: Delete or update the row from the parent table and set the foreign key column(s) in the child table to NULL. This is only valid if the foreign key columns do not have the NOT NULL qualifier specified. NO ACTION: NO ACTION means no action in the sense that an attempt to delete or update a primary key value will not be allowed to proceed if there is a related foreign key value in the referenced table. RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as omitting the ON DELETE or ON UPDATE clause. (Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION and RESTRICT are the same.) SET DEFAULT: This action is recognized by the parser, but InnoDB rejects table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.

May 26, 2005MySQL Tutorial MySQL Table Types If we want to use Foreign Key InnoDB tables Otherwise Default table type, MyISAM In SQL queries you can freely mix InnoDB type tables with other table types of MySQL, even within the same query.

May 26, 2005MySQL Tutorial How to add records Use insert insert into values (column_value, …, column_value);

May 26, 2005MySQL Tutorial Example insert into employee values (1000,'Wilson','Clrk',1700,10); insert into employee values (1001,'Smith','Slsm',2500,40); insert into employee values (1003,'Reed','Anlt',3500,30); insert into employee values (1005,'Watson','Mngr',4500,30); insert into employee values (1009,'Allen','Mngr',3800,40); insert into employee values (1010,'Turner','Clrk',1800,50); insert into employee values (2000,'Chen','Mngr',2900,10); insert into employee values (2100,'Ramirez','Mngr',3650,50); insert into employee values (2130,'McDonnel','Clrk',1625,60); insert into employee values (2140,'Simpson','Drvr',825,60);

May 26, 2005MySQL Tutorial Example On Slide 17 insert into employee2 values (100, ); insert into employee2 values (200, ); insert into employee2 values (300, 98.17); insert into employer values (51, 100); insert into employer values (52, 100); insert into employer values (53, 200); insert into employer values (54, 300);

May 26, 2005MySQL Tutorial How to Select Records select * from ; select * from where = ; select * from where = order by ; select from ; select from

May 26, 2005MySQL Tutorial Example select * from employee; select * from employee where empno = 1000; select * from employee where job = 'Clrk' order by salary; select name, empno from employee; select job from employee; select distinct job from employee;

May 26, 2005MySQL Tutorial Example On Slide 17 select empno from employee2; select empno from employee2 where salary >=50 and salary < 150; select * from employee2, employer; select id, empno from employer m, employee2 n where m.employee_no = n.empno;

May 26, 2005MySQL Tutorial How to Modify Records Use update to modify attribute values of (some) tuples in a table update set =, …, = [where ];

May 26, 2005MySQL Tutorial Example update employee set job = ‘Drvr’, deptno = 20, salary = salary where name = 'Reed’; update employee set salary = salary * 1.15 where deptno in (10, 40);

May 26, 2005MySQL Tutorial How to Delete Records Use delete delete from [where ];

May 26, 2005MySQL Tutorial Example delete from employee where salary < 2000; delete from employee; Note: this command will delete all the records in the “employee” table.

May 26, 2005MySQL Tutorial Tip1: How to Load Command Execute a SQL script file. Take a file name as an argument. Save SQL commands into a file Execute the commands: mysql> source ;

May 26, 2005MySQL Tutorial Example In file “temp.sql” select * from employee; select name from employee where salary < 3000; mysql> source temp.sql;

May 26, 2005MySQL Tutorial Tip2: How to Save Results Save results tee Set outfile. Append everything into given outfile. All the information displayed on screen is stored in.

May 26, 2005MySQL Tutorial Example mysql>tee result; mysql> select * from employee; mysql> notee; mysql> exit torch: ~$ cat result mysql> select * from employee; EMPNO NAME JOB SALARY DEPTNO Wilson Clrk Smith Slsm ………… 10 rows selected. mysql> notee; torch: ~$

May 26, 2005MySQL Tutorial Appendix: MySQL Data Types TypeSizeDescription tinyint[Length]1 byteRange of –128 to 127 float4 bytesA small number with a floating decimal point Date3 bytesIn the format of YYYY-MM-DD Time3 bytesIn the format of HH:MM:SS varchar[Length]String length + 1 byte A fixed-length field from 0 to 255 characters long

May 26, 2005MySQL Tutorial Appendix: MySQL under Unix A few commands use username; show tables; show columns from employee; help; exit; SQL commands (select, insert, delete,…)

May 26, 2005MySQL Tutorial Appendix: MySQL Control Center Graphical user interface (GUI) to the MySQL database server Supports interactive use, including syntax highlighting and tab completion Download from sqlcc.html

May 26, 2005MySQL Tutorial Appendix: MySQL Control Center Student ID