Deepak Bhinde PGT Comp. Sc.

Similar documents
Chapter 9: Working with MySQL

Chapter 14: MySQL Revision Tour. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh (Raj.)

Visit for more.

Chapter 12 DBMS Concepts

Simple Quesries in SQL & Table Creation and Data Manipulation

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

SQL Structured Query Language Introduction

Downloaded from

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

Chapter 3 Introduction to relational databases and MySQL

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

Visit for more.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

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

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

Tables From Existing Tables

Relational Database Management Systems for Epidemiologists: SQL Part I

CSC Web Programming. Introduction to SQL

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2


Table Joins and Indexes in SQL

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Principles of Data Management

Chapter 17: Table & Integrity Contraints. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Downloaded from

COMP 244 DATABASE CONCEPTS & APPLICATIONS

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Chapter-14 SQL COMMANDS

Structure Query Language (SQL)

CSIE30600 Database Systems Basic SQL 2. Outline

Chapter 16: Advanced MySQL- Grouping Records and Joining Tables. Informatics Practices Class XII. By- Rajesh Kumar Mishra

INDIAN SCHOOL MUSCAT FINAL TERM EXAMINATION INFORMATICS PRACTICES

Visit for more.

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

THE INDIAN COMMUNITY SCHOOL, KUWAIT

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Intro to Database Commands

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

SQL Commands & Mongo DB New Syllabus

RESTRICTING AND SORTING DATA

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Database Systems Laboratory 2 SQL Fundamentals

QQ Group

Unit 1 - Chapter 4,5

Basic SQL. Basic SQL. Basic SQL

Chapter 3: Introduction to SQL

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Exact Numeric Data Types

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

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Unit 4. Scalar Functions and Arithmetic

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

CMP-3440 Database Systems

Slides by: Ms. Shree Jaswal

Chapter 3: Introduction to SQL

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

NCSS: Databases and SQL

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

Database Management Systems,

Database Programming with PL/SQL

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

user specifies what is wanted, not how to find it

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

Kendriya Vidyalaya Sangathan (Chandigarh Region) Blue Print of Question Paper Class: XI Subject: Informatics Practices Session

1) Introduction to SQL

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Downloaded from

Database design process

1 SQL Structured Query Language

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

The SQL data-definition language (DDL) allows defining :

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Chapter 3: SQL. Chapter 3: SQL

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

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Chapter 3 How to retrieve data from a single table

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

12. MS Access Tables, Relationships, and Queries

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

SQL Functions (Single-Row, Aggregate)

Overview Relational data model

Relational Database Language

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

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

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

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

SQL. - single row functions - Database Design ( 데이터베이스설계 ) JUNG, Ki-Hyun ( 정기현 )

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

Database Programming with SQL

COMP102: Introduction to Databases, 4

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Transcription:

Deepak Bhinde PGT Comp. Sc.

SQL Elements in MySQL Literals: Literals refers to the fixed data value. It may be Numeric or Character. Numeric literals may be integer or real numbers and Character literals must be closed in single quotes like Hello. Null values: If a column in a row has no value then it is said to NULL. The Null should not be used as a Zero value. Comment: /*. */ (Multi line comment) -- (single line comment) # (single line comment from the appearance) Data types Numeric type Date & Time type String or Text type

Data Types in SQL Numeric Data Types: INTEGER or INT up to 11 digit number without decimal. SMALLINT up to 5 digit number without decimal. FLOAT (M,D) Real numbers up to M digit with D decimal places. Ex. Float (10,2) DECIMAL(M,D) or NUMERIC(M,D) Unpacked floating point up to M length and D decimal places. Date & Time Data Types: DATE - A date in YYYY-MM-DD format. DATETIME A date and time format like YYYY-MM-DD HH:MM:SS TIME - Stores time in HH:MM:SS format. String or Text Data Type: CHAR(M) - A fixed length string up to 255 characters. (default is 1) VARCHAR(M) A variable length string up to 255 characters. BLOB Used to store image data or characters up to 65535. TEXT Same as BLOB but offers case insensitive search.

S ome Data Management commands in MySQL Creating a Database. The following command will create School database in MySQL. Ex. mysql> CREATE DATABASE School; Opening a database To open an existing database you can use the following command. Ex. mysql> USE school ; Creating a table in the database To create a table we can use Create Table. command. The following command will create Student table with Stname and Stcity columns. Ex. mysql> CREATE TABLE student (Stname char(30), Stcity char(20), age Integer ); Inserting a Record in a table mysql> INSERT INTO Student VALUES ( Amit, Suratgarh, 16); Getting listings of database and tables mysql> SHOW DATABASES; mysql> SHOW TABLES; Deleting a Table and database mysql> DROP TABLE Student ; mysql> DROP DATABASE School ; Viewing Table Structure mysql> DESCRIBE Student ;

Making Simple Queries The SELECT command of SQL, empower you to make a request (queries) to retrieve records from the database. The syntax of SQL is given below- SELECT < * column name(s)> FROM <table(s)> WHERE <condition> ORDER BY <column name> [ASC DESC] ; Consider the table Student as StID Name Fname DOB Class S1 Harivansh Rai 1948-11-10 Allahabad 12 S2 Sharukh Firoz 1970-05-10 Delhi 11 S3 Irphan Akbar 1970-10-05 Jaipur 11 S4 Salman Salim Javed 1972-04-10 10 S5 Abhishek 1975-03-12 10

Making Simple Queries Cont.. Selecting all columns If you want to view all columns of the student table, then you should give the following commandmysql> SELECT * FROM Student ; MySQL will display the all records with all columns in the Student table. * Is used to represent all columns. StID Name Fname DOB Class S1 Harivansh Rai 1948-11-10 Allahabad 12 S2 Sharukh Firoz 1970-05-10 Delhi 11 S3 Irphan Akbar 1970-10-05 Jaipur 11 S4 Salman Salim Javed 1972-04-10 10 S5 Abhishek 1975-03-12 10

Making Simple Queries Cont.. Selecting columns If you want to view only Name and columns of the student table mysql> SELECT Name, FROM Student ; Name Sharukh Irphan Salman Abhishek Allahabad Delhi Jaipur But if you give mysql> SELECT, Name FROM Student ; Allahabad Delhi Jaipur Name Sharukh Irphan Salman Abhishek

Making Simple Queries Cont.. Eliminating Duplicate values in a column - DISTINCT mysql> SELECT FROM Student ; Allahabad Delhi Jaipur is repeated mysql> SELECT DISTINCT FROM Student ; Allahabad Delhi Jaipur Only Unique Cities are displayed

Making Simple Queries Cont.. Doing simple calculations We can also perform simple calculations with SQL Select command. SQL provide a dummy table named DUAL, which can be used for this purpose. mysql> SELECT 4*3 FROM DUAL ; We can also extend this idea with a columns of the existing table. mysql> SELECT Name, Sal *12 FROM EMP ; Using Column Aliases We can give a different name to a column or expression (Alias) in the output of a query. Alias for Sal*12 mysql> SELECT Name, Sal*12 AS Annual Salary FROM EMP; mysql> SELECT Name, DOB AS Date of Birth FROM Student; mysql> SELECT 22/7 AS PI FROM Dual; When Alias name is a single word then is not rquired. In MySQL FROM DUAL is optional i.e Select 4*3 ; is valid

Making Simple Queries Cont.. Displaying Text in the Query output We can also display any text in the query out put. mysql> SELECT Mr., Name, FROM Student ; mysql> SELECT Name, Rs, Sal from Emp; Mysql> SELECT Name, Rs, Sal*12 AS Annual Salary FROM Emp; Handling NULL Values MySQL stores a special value called NULL for empty cells, which is displayed when data is not available or missing. We can substitute this NULL by a text or message to get more readable out put. IFNULL(<column name>, <text>) can be used for this purpose. mysql> SELECT Name, IFNULL(DOB, Not Available ) FROM Student; mysql> SELECT Name, IFNULL(Class, Not allotted ) FROM Student;

Selecting Specific Rows WHERE clause WHERE <Condition> We can select specific records by specifying condition with WHERE clause. mysql> SELECT * FROM Student WHERE = ; StID Name Fname DOB Class S4 Salman Salim Javed 1972-04-10 10 S5 Abhishek 1975-03-12 10 mysql> SELECT Name, Fname, from Student WHERE Class >10; Condition Name Fname Class Harivansh Rai Allahabad 12 Sharukh Firoz Delhi 11 Irphan Akbar Jaipur 11 We can operators to make a complex conditions

Selecting Specific Rows WHERE clause Relational Operators We can use the following Relational operators in condition. =, >, <, >=, <=, <>, IS, LIKE, IN, BETWEEN Logical Operators We can use the following Logical Operators to connect two conditions. OR ( ), AND (&&), NOT (!) mysql> SELECT Name, from Student WHERE <> AND Class>10; mysql> SELECT * FROM Emp WHERE Sal >10000 OR Job = Manager ; mysql> SELECT * FROM Student WHERE NOT Grade= A ;

Selecting Specific Rows WHERE clause Specifying Range of Values BETWEEN Operator mysql> SELECT * FROM Emp WHERE Sal BETWEEN 5000 AND 10000 ; The same query can also be written as - mysql> SELECT * FROM Emp WHERE Sal >= 5000 AND Sal<=10000 ; Other Logical operators also can be applied- mysql> SELECT * FROM Emp WHERE NOT Sal BETWEEN 5000 AND 10000 ; Specifying List IN Operator mysql> SELECT * FROM Emp WHERE Sal IN (5000, 10000) ; The same query can also be written as - mysql> SELECT * FROM Emp WHERE Sal = 5000 OR Sal =10000 ; mysql> SELECT * FROM Student WHERE IN (, Delhi, Kanpur ) ;

Selecting Specific Rows WHERE clause Pattern Matching LIKE Operator A string pattern can be used in SQL using the following wild card % - Represents any substring _ - Any Character Example. A% A _B% _ represents any string starting with A character. - represents any 3 character string ending with A. - represents any string having second character B represents any 3 letter string. A pattern is case sensitive and can be used with LIKE operator. mysql> SELECT * FROM Student WHERE Name LIKE A% ; mysql> SELECT * FROM Student WHERE Name LIKE %Singh% ; mysql> SELECT Name, FROM Student WHERE Class>=9 AND Name LIKE %Kumar% ;

Selecting Specific Rows WHERE clause Searching NULL Values IS Operator mysql> SELECT * FROM Student WHERE IS NULL ; The NOT Operator can also be applied - mysql> SELECT * FROM Student WHERE IS NOT NULL; Ordering Query Result ORDER BY Clause A query result can be orders in ascending (A-Z) or descending (Z-A) order as per any column. Default is Ascending mysql> SELECT * FROM Student ORDER BY ; To get descending order use DESC key word. mysql> SELECT * FROM Student ORDER BY DESC; mysql> SELECT Name, Fname, FROM Student Where Name LIKE R% ORDER BY Class;