PRESENTATION
BY
Sundar.R
 The Data Definition Language (DDL) part of SQL
permits database tables to be created or deleted.
We can also define inde...
 SQL (Structured Query Language) is a syntax
for executing queries. But the SQL language
also includes a syntax to update...
 To create a database:
 SYNTAX
 CREATE DATABASE database_name ;
 EXAMPLE
 CREATE DATABASE s2i;
 To activate the crea...
 SYNTAX
 CREATE TABLE table_name(column_name1
data_type,column_name2 data_type,.......) ;
Data Type Description
integer(size)
int(size)
smallint(size)
tinyint(size)
Hold integers only. The maximum
number of digit...
 The INSERT INTO statement is used to insert
new rows into a table.
 SYNTAX
 INSERT INTO table_name VALUES (value1,
val...
 The UPDATE statement is used to modify the
data in a table.
 SYNTAX
 UPDATE table_name SET column1=
new_value WHERE
Ol...
 The ALTER TABLE statement is used to add or
drop columns in an existing table.
 SYNTAX
 ALTER TABLE table_name ADD col...
 The DELETE statement is used to delete rows
in a table.
 Syntax
 DELETE FROM table_name WHERE
column_name = some_value;
 Delete All Rows
 DELETE FROM table_name;
 OR
 DELETE * FROM table_name;
 OR
 TRUNCATE TABLE table_name;
 To delete a table
 DROP TABLE table_name;
 To delete a database
 DROP DATABASE database_name;
 To delete the column ...
 The SELECT statement is used to select data
from a table. The tabular result is stored in a
result table (called the res...
 SELECT A COLUMN
 Select column_name from table_name;
 SELECT A ROW
 Select*from table_name where
column_name=‘value’;
 All values stored in mysql is in array formate.
 $var1=mysql_query(“select*from table_name”,
$connection_name);
 While...
 The LIKE & UNLIKE condition is used to
specify a search for a pattern in a column.
 Syntax
 SELECT column FROM table_n...
 EXAMPLES
 SELECT * FROM table_name WHERE column_name
LIKE 'O%‘;
 SELECT * FROM table_name WHERE column_name
LIKE '%a‘;...
 The AND & OR operators are used to filter
records based on more than one condition.
 The AND operator displays a record...
 SELECT * FROM table_name WHERE
column1=‘value' AND column2=‘value‘;
 SELECT * FROM table_name WHERE
column1=‘value1' OR...
 The ORDER BY keyword is used to sort the
result-set by a specified column.
 The ORDER BY keyword sort the records in
as...
 SELECT column_name(s) FROM table_name
ORDER BY column_name(s) ASC|DESC;
 SELECT * FROM table_name ORDER BY
column_name;...
 Auto-increment allows a unique number to be
generated when a new record is inserted into a
table.
 CREATE TABLE Persons...
 The TOP clause is used to specify the number
of records to return.
 The TOP clause can be very useful on large
tables w...
 SELECT column_name(s) FROM table_name LIMIT
number;
 SELECT TOP 2 * FROM table_name;
 SELECT TOP 50 PERCENT * FROM tab...
 With SQL, aliases can be used for column names
and table names.
 SELECT column AS column_alias FROM table;
 EXAMPLES: ...
 The IN operator allows you to specify multiple
values in a WHERE clause.
 SELECT column_name(s) FROM table_name WHERE
c...
 The BETWEEN operator selects a range of data
between two values. The values can be numbers,
text, or dates.
 SELECT col...
 To select only DIFFERENT values from the
column named
 SYNTAX
 SELECT DISTINCT Company FROM Orders;
 Constraints are used to limit the type of data
that can go into a table.
 Constraints can be specified when a table is
...
 The NOT NULL constraint enforces a column
to NOT accept NULL values.
 CREATE TABLE Persons ( P_Id int NOT NULL,
LastNam...
 The UNIQUE constraint uniquely identifies each
record in a database table.
 CREATE TABLE Persons ( P_Id int NOT NULL,
L...
 The PRIMARY KEY constraint uniquely
identifies each record in a database
table.
 Primary keys must contain unique value...
 A FOREIGN KEY in one table points to a
PRIMARY KEY in another table.
 CREATE TABLE Orders ( O_Id int NOT NULL,
OrderNo ...
 The CHECK constraint is used to limit the value
range that can be placed in a column.
 CREATE TABLE Persons ( P_Id int ...
 The DEFAULT constraint is used to insert a
default value into a column.
 CREATE TABLE Persons ( P_Id int NOT NULL,
City...
 An index can be created in a table to find
data more quickly and efficiently.
 CREATE INDEX index_name ON table_name
(c...
 The JOIN keyword is used in an SQL statement to
query data from two or more tables, based on a
relationship between cert...
 The INNER JOIN keyword return rows when
there is at least one match in both tables.
 SELECT column_name(s) FROM table_n...
 The LEFT JOIN keyword returns all rows from
the left table (table_name1), even if there
are no matches in the right tabl...
 The RIGHT JOIN keyword Return all rows
from the right table (table_name2), even if
there are no matches in the left tabl...
 The FULL JOIN keyword return rows when
there is a match in one of the tables.
 SELECT column_name(s) FROM table_name1
F...
SQL aggregate functions return a single value,
calculated from values in a column.
 AVG() - Returns the average value
 C...
SQL scalar functions return a single value, based
on the input value.
 UCASE() - Converts a field to upper case
 LCASE()...
 GROUP BY... was added to SQL because
aggregate functions (like SUM) return the
aggregate of all column values every time...
 SELECT column, SUM(column) FROM table GROUP
BY column;
 EXAMPLE: SELECT Company, SUM(Amount) FROM
Sales;
 SELECT Compa...
Statement Syntax
AND / OR SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLE (add
column)*...
CREATE TABLE CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
.......
)
CREATE UNIQUE INDEX CREAT...
GROUP BY SELECT
column_name1,SUM(column_name2)
FROM table_name
GROUP BY column_name1
HAVING SELECT
column_name1,SUM(column...
ORDER BY SELECT column_name(s)
FROM table_name
ORDER BY column_name
[ASC|DESC]
SELECT SELECT column_name(s)
FROM table_nam...
SELECT INTO
(used to create backup copies of tables)
SELECT *
INTO new_table_name
FROM original_table_name
or
SELECT colum...
Upcoming SlideShare
Loading in...5
×

Mysql 120831075600-phpapp01

331

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
331
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mysql 120831075600-phpapp01

  1. 1. PRESENTATION BY Sundar.R
  2. 2.  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
  3. 3.  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
  4. 4.  To create a database:  SYNTAX  CREATE DATABASE database_name ;  EXAMPLE  CREATE DATABASE s2i;  To activate the created database:  SYNTAX  USE DATABASE_NAME;
  5. 5.  SYNTAX  CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,.......) ;
  6. 6. 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(yyyymmd d) Holds a date
  7. 7.  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,....) ;
  8. 8.  The UPDATE statement is used to modify the data in a table.  SYNTAX  UPDATE table_name SET column1= new_value WHERE Old_column1=old_value;
  9. 9.  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;
  10. 10.  The DELETE statement is used to delete rows in a table.  Syntax  DELETE FROM table_name WHERE column_name = some_value;
  11. 11.  Delete All Rows  DELETE FROM table_name;  OR  DELETE * FROM table_name;  OR  TRUNCATE TABLE table_name;
  12. 12.  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;
  13. 13.  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;
  14. 14.  SELECT A COLUMN  Select column_name from table_name;  SELECT A ROW  Select*from table_name where column_name=‘value’;
  15. 15.  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’];  }
  16. 16.  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;
  17. 17.  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%‘;
  18. 18.  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.
  19. 19.  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‘);
  20. 20.  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.
  21. 21.  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;
  22. 22.  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 ;
  23. 23.  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.
  24. 24.  SELECT column_name(s) FROM table_name LIMIT number;  SELECT TOP 2 * FROM table_name;  SELECT TOP 50 PERCENT * FROM table_name;
  25. 25.  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;
  26. 26.  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');
  27. 27.  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;
  28. 28.  To select only DIFFERENT values from the column named  SYNTAX  SELECT DISTINCT Company FROM Orders;
  29. 29.  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
  30. 30.  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) )
  31. 31.  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);
  32. 32.  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)
  33. 33.  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)
  34. 34.  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)
  35. 35.  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'
  36. 36.  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;
  37. 37.  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
  38. 38.  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.col umn_name
  39. 39.  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.col umn_name
  40. 40.  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.col umn_name
  41. 41.  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.col umn_name
  42. 42. 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
  43. 43. 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
  44. 44.  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.
  45. 45.  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;
  46. 46. Statement Syntax AND / OR SELECT column_name(s) FROM table_name WHERE condition AND|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_alias FROM table_name AS (alias for table) SELECT column_name FROM table_name AS table_alias BETWEEN SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2 CREATE DATABASE CREATE DATABASE database_name CREATE INDEX CREATE INDEX index_name ON table_name (column_name)
  47. 47. CREATE TABLE CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, ....... ) CREATE UNIQUE INDEX CREATE UNIQUE INDEX index_name ON table_name (column_name) CREATE VIEW CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition DELETE FROM DELETE FROM table_name (Note: Deletes the entire table!!) or DELETE FROM table_name WHERE condition DROP DATABASE DROP DATABASE database_name DROP INDEX DROP INDEX table_name.index_name DROP TABLE DROP TABLE table_name
  48. 48. GROUP BY SELECT column_name1,SUM(column_name2) FROM table_name GROUP BY column_name1 HAVING SELECT column_name1,SUM(column_name2) FROM table_name GROUP BY column_name1 HAVING SUM(column_name2) condition value IN SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,..) INSERT INTO INSERT INTO table_name VALUES (value1, value2,....) LIKE SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern
  49. 49. ORDER BY SELECT column_name(s) FROM table_name ORDER 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_name FROM original_table_name or SELECT column_name(s) INTO new_table_name FROM original_table_name
  50. 50. SELECT INTO (used to create backup copies of tables) SELECT * INTO new_table_name FROM original_table_name or SELECT column_name(s) INTO new_table_name FROM original_table_name TRUNCATE TABLE (deletes only the data inside the table) TRUNCATE TABLE table_name UPDATE UPDATE table_name SET column_name=new_value [, column_name=new_value] WHERE column_name=some_value WHERE SELECT column_name(s) FROM table_name WHERE condition
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×