Вы находитесь на странице: 1из 25

Inserting, Updating and Deleting Records in MySQL SELECT Statement

@Instructor by Ngo Trung Kien Date: Thursday, September 09, 2010 RDBMS and Data Management

Saturday, April 14, 2012

MySQL- DML & DCL

Inserting Records in MySQL

Inserting

Inserting Multiple Rows

INSERT INTO table_name (List field,,) VALUES(Value1,Value2,,);

INSERT INTO table_name (List field,,) VALUES (Value1,Value2,,), (Value1,Value2,,), (Value1,Value2,,),. . . ;

Inserting SET
INSERT INTO table_name SET Field_1= value_1, Field_2=value_2,,;

Saturday, April 14, 2012

MySQL- DML & DCL

Updating, DELETE Data in MySQL


UPDATE table_name SET field = value [,field = value] [WHERE conditions];

DELETE FROM <table_name> WHERE <conditions>;

Saturday, April 14, 2012

MySQL- DML & DCL

Variable in T-SQL
Syntax Local DECLARE{ @local_name [as] Data_type, @local_name [as] Data_type,...} local_name: Name of variable data_type: is a data type of system or user define Example DECLARE @student_id char[10] DECLARE @proId int Global: that are defined and maintained by the sytems. @@VERSION

Saturday, April 14, 2012

MySQL- DML & DCL

Comments in SQL

The standard SQL comment is two hyphens (--). However, some databases use other forms of comments as shown in the table below.

Saturday, April 14, 2012

MySQL- DML & DCL

Introduction

The SELECT statement is discussed in order to retrieve and access data from database That data that is retrieved is not only useful for the purpose of reading but also for modification by operator as UPDATE, DELETE, In a table data can be viewed using the SELECT statement will display the required information in table The SELECT statement retrieves rows and columns from one or more table The Ouput of the SELECT statement is another table called result set The columns appear in the same sequence as the order of expression in the SELECT statement.

Saturday, April 14, 2012

MySQL- DML & DCL

Classifying SELECT statement

The SELECT statement also joins two table, or retrieves a subset of columns from one or more tables The SELECT statement also defines the colums to be used for a query The SELECT statement can consist of a series of expressions seperates by commas

Saturday, April 14, 2012

MySQL- DML & DCL

Syntax SELECT statement


Syntax: SELECT <collection> [FROM <table name>] [WHERE <expression>] [GROUP BY <expression> HAVING <Condition>] [ORDER BY <Field_name> ASC| DESC] [LIMIT <[Offset,] Limit>] Which:

collection: list of column name in any table (include between columns , if retrieves than one) or any (function, variable,..) table name: List of tabl Expression: Operators and comparison

Saturday, April 14, 2012

MySQL- DML & DCL

Full SELECT statement

Saturday, April 14, 2012

MySQL- DML & DCL

Example SELECT statement


SELECT * FROM tblCategories SELECT cat_id ,cat_name ,cat_img FROM tblCategories SELECT c.cat_id as ID ,c.cat_name as category_name FROM tblCategories as c WHERE c.cat_id=PA001 SELECT LEFT(C0909K,3)

Saturday, April 14, 2012

MySQL- DML & DCL

10

SELECT INTO

INTO clause creates a new table and insert rows, colums listed in the SELECT INTO clause inserts existing rows into the new table
SELECT <list colums> INTO <new table> FROM <table selected>

SELECT * INTO backup_tblCategories FROM tblCategories


MySQL- DML & DCL
11

Saturday, April 14, 2012

SELECT DISTINCT

DISTINCT keyword elimitnates rows that are repeateing from the result set of a SELECT statement Syntax:
SELECT DISTINCT <column eliminate> FROM <table name>

Ex: SELECT DISTINCT cat_name FROM tblCategories

Saturday, April 14, 2012

MySQL- DML & DCL

12

SELECT WHERE

WHERE used to conditionally select or limit records retrieved by the query Syntax: SELECT <collection> FROM <table name> WHERE <expression> Where: collection: list of column name in any table (include between columns , if retrieves than one) or any (function, variable,..) table name: List of table Expression: Operators
MySQL- DML & DCL
13

Saturday, April 14, 2012

SELECT WHERE- Expression

The different operators that can be used with the WHERE <Expression>
Operator
=,>=,<,>,<= < >, != AND, OR, NOT LIKE BETWEEN Not equal to WHERE price>=10.5 AND price<=20.5 OR price=21.0 Search for pattern %[_^string]% WHERE price BETWEEN 10.5 AND 20.5

Description
Compare operators

IN,NOT IN, ALL, ANY Hellochao IN (C, Hello, chao)

Saturday, April 14, 2012

MySQL- DML & DCL

14

Options and Attributes

Saturday, April 14, 2012

MySQL- DML & DCL

15

SELECT ORDER BY, GROUP BY

ORDER BY: Sort query result by one or more columns follow ASC, DESC GROUP BY: clause partition the result set into one or more subset, can be it uses with HAVING Syntax:

SELECT <collection> FROM <table name> WHERE <expression> ORDER BY <column ASC|DESC> GROUP BY <column> [HAVING <expression>]

Saturday, April 14, 2012

MySQL- DML & DCL

16

Example ORDER, GROUP BY

SELECT * FROM tblCategories as c WHERE 1=1 ORDER BY c.cat_name ASC SELECT ord_id , cus_name , sum(pro_price*ord_quantity) as Totals FROM tblOrderDetails WHERE 1=1 GROUP BY cus_id

Saturday, April 14, 2012

MySQL- DML & DCL

17

SELECT -The LIMIT Clause


The LIMIT clause is used most effectively in a SELECT statement when it is used with an ORDER BY clause. The LIMIT clause takes two arguments, as the following syntax shows:

LIMIT [<offset>,] <row count>


SELECT * FROM tblCategories as c LIMIT 10 SELECT * FROM tblCategories as c LIMIT 3,5 SELECT title, rating, rental_rate FROM film ORDER BY title ASC LIMIT 5;
Saturday, April 14, 2012

MySQL- DML & DCL

18

Join multi table by key

In a normalized database, groups of data are stored in individual tables, and relationships are established between those tables to link related data As a result, often when creating SELECT, UPDATE, or DELETE statements, you want to be able to access data in different tables to carry out an operation affected by those relationships The real power of a relational database emerges from the ability to combine multiple entities in a single operation
MySQL- DML & DCL
19

Saturday, April 14, 2012

Using WHERE

Syntax: SELECT <collection columns> FROM <table_1, table_2> WHERE <table_1.c_key=table_2.c_key> c_key: is primary key or foreign key Table_1, table_2: Related 1-1, 1-N or M-N

Saturday, April 14, 2012

MySQL- DML & DCL

20

Example using WHERE

SELECT C.ContactName ,C.City ,O.OrderDate O.RequiredDate FROM Customers as C, Orders as O WHERE C.CustomerID=O.CustomerID

Saturday, April 14, 2012

MySQL- DML & DCL

21

Using Join

SQL supports the following JOIN syntaxes for the table references part of SELECT statements and multiple-table DELETE and UPDATE statements: Table references type:

INNER JOIN LEFT JOIN RIGHT JOIN

Saturday, April 14, 2012

MySQL- DML & DCL

22

Using Join[1]

Syntax

SELECT <collection columns> FROM <table_1> INNER|LEFT|RIGHT JOIN <table_2> ON <table_1.c_key=table_2.c_key>

c_key: is primary key or foreign key Table_1, table_2: Related 1-1, 1-N or M-N

Saturday, April 14, 2012

MySQL- DML & DCL

23

Example using Join

SELECT C.ContactName ,C.City ,O.OrderDate O.RequiredDate FROM Customers as C INNER JOIN Orders as O ON C.CustomerID=O.CustomerID

Saturday, April 14, 2012

MySQL- DML & DCL

24

Relation JOIN Statement

Saturday, April 14, 2012

MySQL- DML & DCL

25

Вам также может понравиться