Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.

Similar presentations


Presentation on theme: "CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND."— Presentation transcript:

1 CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND

2 Introduction  SQL, Structured Query Language, was developed in 1970s in an IBM Laboratory.  SQL sometimes also referred to as SEQUEL is a 4 th generation non- procedural language.  SQL, being non-procedural, describes WHAT all data is to be retrieved or inserted or modified or deleted, rather than specifying code describing HOW to perform the entire operation

3  SQL, enables the following: (i) Creating / modifying a database’s structure (ii) Changing security settings for system (iii) Permitting users for working on databases or tables (iv) Querying database (v) Inserting / Modifying/Deleting the database contents.

4  SQL Elements in MySQL The MySQL implementation of SQL has certain elements that play an important role in defining /querying a database. These basic elements are: (i) Literals (ii) Data types (iii) Nulls (iv) Comments  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’.

5  data types: To identify the type of data and associated operations for handling it.  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. Nulls can appear in columns of any data type provided they are not restricted by NOT NULL or PRIMARY KEY integrity constraints.  Comment:  /*…. */ => (Multi line comment)  -- =>(single line comment)  # =>single line comment from the appearance)

6  Numeric Data Types: MySQL uses all the standard ANSI SQL numeric data types.  The following list shows the common 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.  TINYINT: up to 4 digit number without decimal.  MEDIUMINT: up to 9 digit number without decimal.  BIGINT: up to 11 digit number without decimal.  DOUBLE(M.D): A Double precision floating numbers up to M digit with D decimal places.default to 16,4

7 Date & Time Data Types  DATE : A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. example: December 30 th,1973 would be stored as 1973-12-30.  DATETIME:A date and time format like YYYY- MM-DD HH:MM:SS. Example: 1973-12-30 15:3:00  TIME :Stores time in HH:MM:SS format.  YEAR(M): Store a year in 2 or 4 digits format. Example:  YEAR(2), year can be 1970 to 2069(70 to 69).

8 String /text Types:  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 not case sensitive search. Difference between Char and Varchar data types  The difference between CHAR and VARCHAR is that of fixed length and variable length.  The CHAR data types specifies a fixed length character string and VARCHAR data types specifies a variable length string.

9 SQL Server Data Types  int, integer: 4 byte integer  smallint: 2 byte integer  tinyint: 1 byte integer  float: 4 or 8 byte floating point number  real: 4 byte floating point number  double precision: 8 byte floating point number  numeric, decimal(precision, scale): exact numeric, 2 to 17 bytes. Only difference is that only numeric types with a scale of 0 can be used of the IDENTITY column.  Destroy a table: drop table table_name

10 SQL COMMAND SYNTAX  The SQL provides a predefined set of commands that help us work on relational databases.  Keywords are words that have a special meaning in SQL. They are understood to be instructions.  Commands, or statements, are instructions given by you to a SQL database.  Arguments complete or modify the meaning of a clause.

11 SPECIAL OPERATORS  BETWEEN - define range limits  IS NULL - check if attribute value is null  LIKE - check for similar character strings  IN - check if attribute value matches a value within a (sub)set of listed values  EXISTS - check whether attribute has a value

12 Basic MySQL Operations  Create database  use database  see data base  Create table  Insert records  Load data  Retrieve records  Update records  Delete records  Modify table  Join table  Drop table  Optimize table  Count, Like, Order by, Group by  More advanced ones (sub-queries, stored procedures, triggers, views …)

13  Some Data Management commands in MySQL  Creating a Database: Syntax to Create database: Create database databasename ;  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 ; Database changed  What tables are currently stored in the MyDB database? mysql> show tables; Empty set (0.00 sec)

14 What are the current databases at the server? mysql> show databases; +--------------+ | Database | +--------------+ | mysql | | test | +--------------+ mysql is a database (stores users’ password) used by system.

15  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.  Syntax;  CREATE TABLE (columnname datatypes[size], columnname data types[size],….); Ex. mysql> CREATE TABLE student (Stname char(30), Stcity char(20), age Integer );

16 Making Simple Queries  Inserting a Record in a table mysql> INSERT INTO Student VALUES (‘Amit’, ‘ Suratgarh’, 16);  Deleting a Table and database mysql> DROP TABLE Student ; mysql> DROP DATABASE School ;  Viewing Table Structure mysql> DESCRIBE Student ;  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 FROM WHERE ORDER BY [ASC | DESC] ;

17  SELECT Statement: for queries on single or multiple tables  Clauses of the SELECT statement: –SELECT: List the columns (and expressions) that should be returned from the query –FROM: Indicate the table(s) or view(s) from which data will be obtained –WHERE: Indicate the conditions under which a row will be included in the result –GROUP BY: Indicate columns to group the results –HAVING: Indicate the conditions under which a group will be included –ORDER BY: Sorts the result according to specified columns

18 18

19

20

21

22  Selecting From All the Rows-ALL Keyword: The result retains the duplicates output rows. It is just the same as when you specify neither DISTINCT nor ALL.  EXAMPLE:  SELECT ALL city FROM Student;

23

24

25

26

27

28

29

30  Sorting by Column Alias  If you have defined column alias in your query, then you can use this column alias even for sorting records. This is because, once a column alias is declared, it can be used elsewhere in the statement.

31


Download ppt "CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND."

Similar presentations


Ads by Google