my sql

50
PRESENTATION BY Sundar.R

Upload: sundar

Post on 20-Jan-2015

981 views

Category:

Education


3 download

DESCRIPTION

May be useful for u. All the best..

TRANSCRIPT

Page 1: MY SQL

PRESENTATION

BY

Sundar.R

Page 2: MY SQL

The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables.

CREATE TABLE - creates a new database table

ALTER TABLE - alters (changes) a database table

DROP TABLE - deletes a database

Page 3: MY SQL

SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records.

SELECT - extracts data from a database table

UPDATE - updates data in a database table

DELETE - deletes data from a database table

INSERT INTO - inserts new data into a database table

Page 4: MY SQL

To create a database: SYNTAX CREATE DATABASE database_name ; EXAMPLE CREATE DATABASE s2i; To activate the created database: SYNTAX USE DATABASE_NAME;

Page 5: MY SQL

SYNTAX

CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,.......) ;

Page 6: MY SQL

Data Type Description

integer(size)int(size)smallint(size)tinyint(size)

Hold integers only. The maximum number of digits are specified in parenthesis.

decimal(size,d)numeric(size,d)

Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specified in "d".

char(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis.

varchar(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis.

date(yyyymmdd)

Holds a date

Page 7: MY SQL

The INSERT INTO statement is used to insert new rows into a table.

SYNTAX INSERT INTO table_name VALUES

(value1, value2,....) ;

INSERT INTO table_name (column1, column2,...)VALUES (value1, value2,....) ;

Page 8: MY SQL

The UPDATE statement is used to modify the data in a table.

SYNTAX UPDATE table_name SET column1=

new_value WHEREOld_column1=old_value;

Page 9: MY SQL

The ALTER TABLE statement is used to add or drop columns in an existing table.

SYNTAX

ALTER TABLE table_name ADD column_name data_type;

Page 10: MY SQL

The DELETE statement is used to delete rows in a table.

Syntax

DELETE FROM table_name WHERE column_name = some_value;

Page 11: MY SQL

Delete All Rows

DELETE FROM table_name; OR DELETE * FROM table_name; OR TRUNCATE TABLE table_name;

Page 12: MY SQL

To delete a table DROP TABLE table_name; To delete a database DROP DATABASE database_name; To delete the column in table ALTER TABLE table_name DROP

COLUMN column_name;

Page 13: MY SQL

The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set).

Select a table fully Syntax SELECT * FROM Persons;

Page 14: MY SQL

SELECT A COLUMN Select column_name from table_name;

SELECT A ROW Select*from table_name where

column_name=‘value’;

Page 15: MY SQL

All values stored in mysql is in array formate. $var1=mysql_query(“select*from

table_name”,$connection_name); While($var=mysql_fetch_array($var1)) { Echo “<br/>”; Echo $var[‘column_name’]; }

Page 16: MY SQL

The LIKE & UNLIKE condition is used to specify a search for a pattern in a column.

Syntax SELECT column FROM table_name

WHERE column_name LIKE pattern; SELECT column FROM table_name

WHERE column_name UNLIKE pattern;

Page 17: MY SQL

EXAMPLES

SELECT * FROM table_name WHERE column_name LIKE 'O%‘;

SELECT * FROM table_name WHERE column_name LIKE '%a‘;

SELECT * FROM table_name WHERE column_name LIKE '%la%‘;

Page 18: MY SQL

The AND & OR operators are used to filter records based on more than one condition.

The AND operator displays a record if both the first condition and the second condition is true.

The OR operator displays a record if either the first condition or the second condition is true.

Page 19: MY SQL

SELECT * FROM table_name WHERE column1=‘value' AND column2=‘value‘;

SELECT * FROM table_name WHERE column1=‘value1' OR column1=‘value2‘;

SELECT * FROM table_name WHERE column1=‘value' AND (column2=‘value1' OR

column2=‘value2‘);

Page 20: MY SQL

The ORDER BY keyword is used to sort the result-set by a specified column.

The ORDER BY keyword sort the records in ascending order by default.

If you want to sort the records in a descending order, you can use the DESC keyword.

Page 21: MY SQL

SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC;

SELECT * FROM table_name ORDER BY column_name;

SELECT * FROM table_name ORDER BY column_name DESC;

Page 22: MY SQL

Auto-increment allows a unique number to be generated when a new record is inserted into a table.

CREATE TABLE Persons ( Id int (5) AUTO_INCREMENT)

ALTER TABLE Persons AUTO_INCREMENT=100 ;

Page 23: MY SQL

The TOP clause is used to specify the number of records to return.

The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.

Note: Not all database systems support the TOP clause.

Page 24: MY SQL

SELECT column_name(s) FROM table_name LIMIT number;

SELECT TOP 2 * FROM table_name;

SELECT TOP 50 PERCENT * FROM table_name;

Page 25: MY SQL

With SQL, aliases can be used for column names and table names.

SELECT column AS column_alias FROM table; EXAMPLES: SELECT LastName AS Family,

FirstName AS NameFROM Persons; SELECT column FROM table AS table_alias; EXAMPLE: SELECT LastName, FROM Persons

AS Employees;

Page 26: MY SQL

The IN operator allows you to specify multiple values in a WHERE clause.

SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);

SELECT * FROM table_name WHERE column IN (‘value1',‘value2');

Page 27: MY SQL

The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.

SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;

SELECT column_name(s) FROM table_name WHERE column_name NOTBETWEEN value1 AND value2;

Page 28: MY SQL

To select only DIFFERENT values from the column named

SYNTAX SELECT DISTINCT Company FROM

Orders;

Page 29: MY SQL

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 CHECK PRIMARY KEY FOREIGN KEY DEFAULT

Page 30: MY SQL

The NOT NULL constraint enforces a column to NOT accept NULL values.

CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

Page 31: MY SQL

The UNIQUE constraint uniquely identifies each record in a database table.

CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE (P_Id) );

Alter table table_name ADD UNIQUE (ID);

Page 32: MY SQL

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

CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, PRIMARY KEY (P_Id) )

ALTER TABLE Persons ADD PRIMARY KEY (P_Id)

Page 33: MY SQL

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 (P_Id), FOREIGN KEY (O_Id) REFERENCES Persons(P_Id) )

ALTER TABLE Orders ADD FOREIGN KEY (O_Id) REFERENCES Persons(P_Id)

Page 34: MY SQL

The CHECK constraint is used to limit the value range that can be placed in a column.

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

ALTER TABLE Persons ADD CHECK (P_Id>0)

Page 35: MY SQL

The DEFAULT constraint is used to insert a default value into a column.

CREATE TABLE Persons ( P_Id int NOT NULL, City varchar(255) DEFAULT 'Sandnes' )

ALTER TABLE Persons WHERE City SET DEFAULT 'SANDNES'

Page 36: MY SQL

An index can be created in a table to find data more quickly and efficiently.

CREATE INDEX index_name ON table_name (column_name);

CREATE UNIQUE INDEX index_name ON table_name (column_name);

Drop INDEX index_name FROM TABLE_NAME;

Page 37: MY SQL

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.

Different SQL JOINS INNER JOIN OUTER JOIN FULL JOIN

Page 38: MY SQL

The INNER JOIN keyword return rows when there is at least one match in both tables.

SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name

Page 39: MY SQL

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).

SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

Page 40: MY SQL

The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).

SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

Page 41: MY SQL

The FULL JOIN keyword return rows when there is a match in one of the tables.

SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name

Page 42: MY SQL

SQL aggregate functions return a single value, calculated from values in a column.

AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum

Page 43: MY SQL

SQL scalar functions return a single value, based

on the input value. UCASE() - Converts a field to upper case LCASE() - Converts a field to lower case MID() - Extract characters from a text field LEN() - Returns the length of a text field ROUND() - Rounds a numeric field to the

number of decimals specified NOW() - Returns the current system date and time FORMAT() - Formats how a field is to be displayed

Page 44: MY SQL

GROUP BY... was added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called, and without the GROUP BY function it was impossible to find the sum for each individual group of column values.

Page 45: MY SQL

SELECT column, SUM(column) FROM table GROUP

BY column; EXAMPLE: SELECT Company, SUM(Amount)

FROM Sales; SELECT Company, SUM(Amount) FROM Sales GROUP BY Company; SELECT column, SUM(column) FROM table

GROUP BY column HAVING SUM(column) condition

value;

Page 46: MY SQL

Statement Syntax

AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition

ALTER TABLE (add column)*//

ALTER TABLE table_name ADD column_name datatype

ALTER TABLE (drop column)

ALTER TABLE table_name DROP COLUMN column_name

AS (alias for column) SELECT column_name AS column_aliasFROM table_name

AS (alias for table) SELECT column_nameFROM table_name  AS table_alias

BETWEEN SELECT column_name(s)FROM table_nameWHERE column_nameBETWEEN value1 AND value2

CREATE DATABASE CREATE DATABASE database_name

CREATE INDEX CREATE INDEX index_nameON table_name (column_name)

Page 47: MY SQL

CREATE TABLE CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,.......)

CREATE UNIQUE INDEX CREATE UNIQUE INDEX index_nameON table_name (column_name)

CREATE VIEW CREATE VIEW view_name ASSELECT column_name(s)FROM table_nameWHERE condition

DELETE FROM DELETE FROM table_name (Note: Deletes the entire table!!) orDELETE FROM table_nameWHERE condition

DROP DATABASE DROP DATABASE database_name

DROP INDEX DROP INDEX table_name.index_name

DROP TABLE DROP TABLE table_name

Page 48: MY SQL

GROUP BY SELECT column_name1,SUM(column_name2)FROM table_nameGROUP BY column_name1

HAVING SELECT column_name1,SUM(column_name2)FROM table_nameGROUP BY column_name1HAVING SUM(column_name2) condition value

IN SELECT column_name(s)FROM table_nameWHERE column_nameIN (value1,value2,..)

INSERT INTO INSERT INTO table_nameVALUES (value1, value2,....)

LIKE SELECT column_name(s)FROM table_nameWHERE column_nameLIKE pattern

Page 49: MY SQL

ORDER BY SELECT column_name(s)FROM table_nameORDER BY column_name [ASC|DESC]

SELECT SELECT column_name(s)FROM table_name

SELECT * SELECT *FROM table_name

SELECT DISTINCT SELECT DISTINCT column_name(s)FROM table_name

SELECT INTO(used to create backup copies of tables)

SELECT *INTO new_table_nameFROM original_table_name orSELECT column_name(s)INTO new_table_nameFROM original_table_name

Page 50: MY SQL

SELECT INTO(used to create backup copies of tables)

SELECT *INTO new_table_nameFROM original_table_name orSELECT column_name(s)INTO new_table_nameFROM original_table_name

TRUNCATE TABLE(deletes only the data inside the table)

TRUNCATE TABLE table_name

UPDATE UPDATE table_nameSET column_name=new_value[, column_name=new_value]WHERE column_name=some_value

WHERE SELECT column_name(s)FROM table_nameWHERE condition