chapter:14 simple queries in sql prepared by prepared by : vinay alexander ( विनय...

31
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER VINAY ALEXANDER ( ( वववव वववव ववववववववववव ववववववववववव ) ) PGT(CS) ,KV JHAGRAKHAND PGT(CS) ,KV JHAGRAKHAND

Upload: brenda-shelton

Post on 16-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

  • CHAPTER:14Simple Queries in SQL Prepared By :VINAY ALEXANDER ( )PGT(CS) ,KV JHAGRAKHAND

  • IntroductionSQL, Structured Query Language, was developed in 1970s in an IBM Laboratory. SQL sometimes also referred to as SEQUEL is a 4th 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

  • SQL, enables the following:(i) Creating / modifying a databases 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.

  • SQL Elements in MySQLThe 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) CommentsLiterals: 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.

  • 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)

  • Numeric Data Types: MySQL uses all the standard ANSI SQL numeric data types. The following list shows the common numeric data typesINTEGER 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

  • Date & Time Data TypesDATE : A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. example: December 30th ,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:00TIME :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).

  • 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 typesThe 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.

  • SQL Server Data Typesint, integer: 4 byte integersmallint: 2 byte integertinyint: 1 byte integerfloat: 4 or 8 byte floating point numberreal: 4 byte floating point numberdouble 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

  • SQL COMMAND SYNTAXThe 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.

  • SPECIAL OPERATORSBETWEEN - define range limitsIS NULL - check if attribute value is nullLIKE - check for similar character stringsIN - check if attribute value matches a value within a (sub)set of listed valuesEXISTS - check whether attribute has a value

  • Basic MySQL OperationsCreate database use database see data baseCreate tableInsert recordsLoad dataRetrieve recordsUpdate recordsDelete recordsModify tableJoin tableDrop tableOptimize tableCount, Like, Order by, Group byMore advanced ones (sub-queries, stored procedures, triggers, views )

  • Some Data Management commands in MySQLCreating 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 changedWhat tables are currently stored in the MyDB database? mysql> show tables;Empty set (0.00 sec)

  • What are the current databases at the server?mysql> show databases; +--------------+| Database |+--------------+ | mysql | | test |+--------------+

    mysql is a database (stores users password) used by system.

  • 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 );

  • Making Simple QueriesInserting a Record in a tablemysql> INSERT INTO Student VALUES (Amit, Suratgarh, 16);Deleting a Table and databasemysql> DROP TABLE Student ;mysql> DROP DATABASE School ;Viewing Table Structuremysql> 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 < *| column name(s)> FROM WHERE ORDER BY [ASC | DESC] ;

  • SELECT Statement: for queries on single or multiple tablesClauses of the SELECT statement:SELECT: List the columns (and expressions) that should be returned from the queryFROM: Indicate the table(s) or view(s) from which data will be obtainedWHERE: Indicate the conditions under which a row will be included in the resultGROUP BY: Indicate columns to group the results HAVING: Indicate the conditions under which a group will be includedORDER BY: Sorts the result according to specified columns

  • *

  • 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;

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

  • *