3 [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... 4 [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] 5 (or) 6 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name 7 SET col_name={expr | DEFAULT}, ... 8 [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] 9 (or) 10 INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE][INTO] tbl_name 11 [(col_name [, col_name] ...)] 12 (SELECT col_name [, col_name] ... from existing_table_name) 13 [ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ...] 14 15 -DELAYED: 16 The server puts the row or rows to be inserted into a queue, and the client issuing the INSERT DELAYED statement can then continue 17 to work on other operations. Records in the queue are added to the table when it is not used by any other thread. 18 19 -LOW_PRIORITY and HIGH_PRIORITY: 20 In LOW_PRIORITY, execution of INSERT command is delayed until no other clients are performing read operation on the same table. 21 HIGH_PRIORITY delays low-priority-updates and also causes concurrent inserts not to be used. 22 23 -IGNORE: 24 Ignores and discards errors caused during INSERT statement and all errors are treated as warnings instead. 25 26 -ON DUPLICATE KEY UPDATE: 27 If a duplicate value occurs for Unique / primary key column during insertion, it will update the new value instead of throwing 28 constraint violation error. 29 -------------------------------------------------------------------------------------- -------------------------------------------- 30 31 Replace Command(can insert a new record as well as can update the existing record):- 32 REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... 33 (or) 34 REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name SET col_name={expr | DEFAULT}, ... 35 -------------------------------------------------------------------------------------- -------------------------------------------- 36 37 Delete Command:- 38 DELETE [LOW_PRIORITY][IGNORE] FROM table_name 39 [WHERE where_condition] 40 [LIMIT row_count] 41 -------------------------------------------------------------------------------------- -------------------------------------------- 42 43 Truncate Command:- 44 TRUNCATE TABLE <table_name>; 45 -------------------------------------------------------------------------------------- -------------------------------------------- 46 47 Update Command:- 48 UPDATE [LOW_PRIORITY] [IGNORE] table_name 49 SET col_name1={expr1} [, col_name2={expr2}] ... 50 [WHERE where_condition] 51 [ORDER BY ...] [LIMIT row_count]; 52 53 Example- 54 UPDATE HOTEL_ROOM_DETAILS SET ROOM_PRICE = ROOM_PRICE + 1000 55 WHERE HOTEL_ID=1 LIMIT 1; 56 57 -------------------------------------------------------------------------------------- -------------------------------------------- 58 Select Command:- 59 SELECT [ALL|DISTINCT|DISTINCTROW] [HIGH_PRIORITY] [STRAIGHT_JOIN] select_expr [, select_expr ...] 60 FROM table_references 61 [WHERE where_condition] 62 [GROUP BY {col_name|expr|position} [ASC|DESC], ... [WITH ROLLUP]] 63 [HAVING where_condition] 64 [ORDER BY {col_name|expr|position} [ASC|DESC], ...] 65 [LIMIT {[offset_n,] row_count} | {row_count [OFFSET offset_n]}]; 66 67 brief descriptions for the options of SELECT clause: 68 -SELECT - Used to retrieve rows from the table. 69 ALL - Used to include all rows retrieved from the table into the result set. By default, ALL is the selected option when 70 DISTINCT and DISTINCTROW options are not specified 71 DISTINCT/DISTINCTROW - Only distinct rows are retrieved into the result set. If more than one column is mentioned in the 72 SELECT clause, then distinct operation is applied to combination of the column values 73 -HIGH_PRIORITY - This option makes the SELECT command to have higher priority over any other operation that is already occurring 74 on the table 75 -STRAIGHT_JOIN - When more than one table is mentioned in the FROM clause for join operations, it forces the tables to be used in 76 the same order as it is mentioned, preventing order change for internal optimization 77 -FROM - Used to mention table references from which records are fetched 78 -WHERE - Used to filter records retrieved from the table based on certain condition(s) 79 -GROUP BY - Used to group particular set of records and apply aggregate functions on them 80 -WITH ROLLUP - Used to perform super aggregation of the GROUP BY result set 81 -HAVING - Filters the GROUP BY result set using condition(s) containing appropriate aggregate functions 82 -ORDER BY - Used to sort the result set based on values present in particular column(s) 83 -LIMIT - Restricts the number of rows returned by the SELECT statement 84 -offset_n - Represents the number of records to be skipped for retrieval 85 86 Clauses, if used, must be specified in the following order in SELECT command: 87 SELECT 88 FROM 89 WHERE 90 GROUP BY 91 HAVING 92 ORDER BY 93 LIMIT 94 95 The SELECT command is parsed in the following order: 96 FROM 97 WHERE 98 GROUP BY 99 HAVING 100 SELECT 101 ORDER BY 102 LIMIT 103 At each step of parsing, the result set obtained from previous step is given as input to the next step for parsing 104 -------------------------------------------------------------------------------------- -------------------------------------------- 105 View:- 106 CREATE [OR REPLACE] VIEW view_name [(col list)] 107 AS select_command 108 [WITH CHECK OPTION] 109 -REPLACE: This option, if used, drops an existing view with the same name and creates a new view 110 -WITH CHECK OPTION: This option ensures that any updates made to the table through the view are in accordance to the WHERE 111 condition provided in the SELECT command during view creation. 112 113 Note- 114 -The SELECT command in the view statement cannot have a sub query 115 -VIEWS created using aggregated columns cannot be updated and also for updating any view there should be a one to one relationship 116 between the records of the view and the base table 117 -ALTER VIEW is used to alter a view definition and DROP VIEW is used for deleting a view 118 -VIEWs are accessible as long as the base table(s) used for creating the view are available 119 -------------------------------------------------------------------------------------- -------------------------------------------- 120 121 122 123