You are on page 1of 15
MODULE 3 Structured Query Language (MySQL) ae an ogee ‘tern ne cue snd SQ cmd e300, ONS, NEE, Dk Grote cate eave mes Sk cane fom sane My he one Mel Mo ng, SGC" seinen fr Sted Qs aaa. eS " peta. pening ys. Toon ne totbsed entire, toe atthe prom sys root te) Uae othe ye monte. command end wth org Your myst comecerie Sever e327 mys. conmanty Saver Typernep or Warne. pee dear cate gut stan To wich nv MSL crmand om a cent pep nyeal>command-heywordfparametr()k et} apt: ya sest databases [nt=) Frequently-used Commands 1. CREATE database dbaama: 2. show databases: 5. ariege [olumns] FROM fablename Pruileges: SELECT 2 INSERT © UPDATE 2 DELETE © ALTER 2 CREATE © DROP INDEX Seindimg ane seo) medina) as ‘Sewn ca Syoer ree 1s 3) ews °F Fateypean) 12 ae ogee samnqaoansone piety ° Punters) 68595 vas Pepa.) et vee Attributes primary kay ‘auto_increment not null unsigned The maximum range wth unsigned int is 4294967295. When we Jwtite an unsigned to any column, that means you cannot insert negative rumors, 6. show tables; 7. describe fablename: ‘desc tablengmer ©. INSERT tablename (column2. column...) values (valted. vale...) INSERT fabloname set columni=valvet, colmn?=value?. 9. SELECT columa(s) from tablename/<) WHERE condition [group by gcouptunel Thaving Zefation| [order by column [limit criteria; 10, UPDATE (zblename set columnt= values, column2=valuez, WHERE condition; 11, DELETE from tablename WHERE condition; {ALTER ae amame andra, coma} i chute ae ‘Slonntamar 2 pmay ey (eaheaan Change commana stmnanatiw noe Totes cp cua: ‘bop pear ye cpa rerame waiamas ‘SQL Data Manipulation Language (OM) and ‘ata Definition Language (001) Commands ‘DDL commands i + UPDATE -Isuset metre eeng recone, + DRTE —-isusuco cae eas recs rom ata ‘Thea. 00. cnmands pats database ables 0b ota or clea ta dees Inge (ks), pers ss amar bes, an ips as aongales. ‘OL commands ist: mine nde ted ser ew yen tn end ee + LIER ‘Greater tha Less than ‘Greater than or equa MySQL Like Clause The folmg are same examples shovmng deren LIE operators wth "and" wwieards: TOKE Operator Description ‘WHERE column_narie URE SiG” | ids any vals that Sart wth WHERE column-name LIKE Ya" Finds any values that end ith" Less than or equal ‘WHERE columnar UKE Wh nds any values at have" ay postion ] J 1 1 1 | | | } Mote: In some versions of SQL the <> operator may be writen as = BETWEEN | Column between the lower iit and upper Tt ‘WHERE clumn_narie UKE o8) | Fs any vals that have "in te second RE Pattarn match Hike (ejuante peston TOT ERE [Batter math ot Be aay ‘WERE column nario ORE'0 9 56” | nds any vos that car with" and are at [Rots _[ Fann natch got ef 23 vans_| least 3 characters in gt aT es WE cea URE | day es at ta WY a ed TS NULL Address is nul TS NOT NULL [Column isnot nll MySQL Between Clause Coe example: ‘SELECT * FROM erployee_tt WHERE salary BETWEEN 15000 AND 30000; MySQL NOT LIKE Clause Bam: The fellowing MySQL statement excludes those rows from the table employee tl, having the Ist character of emp_name"T. code: SELECT emp_name, adress FROM employee_tbl WHERE emp_name NOT LIKE Ts; MySQL IN Clause code example 1 clause used ta replace many OR conditions waecr™ FROM cmployoatb WHERE Sry=7000 OR saary=7500 OR slry=8000; Using IN clause a5 folows: From employe tb) WHERE salary IN (7000, 7500, 8900) MySQLNOT IN Clause ame Ifyou want to get the on from the table employee, which contain address at in “sagan” oc "cebu", the folowing sal statement can be used ode: SELECT amp nae, aaa FROM employee th WHERE oacrese NOTIN (Bago, "Cebu Frequently-used Intrinsic Aggregate Functions avg(column) average of the column count{column) frequently count of the column — if DISTINCT is added in front of the column name, count is unique min(column) minimum value in the specified column max{column) maximum value in the specified column sum(column) summation of the value for the specified column std(column) standard deviation of the specified column My SQL / SQL Server / MS Access: [ALTER TABLE tabl ALTER COLUMN column_name datatype SQL ALTER TABLE Example Look atthe "Sales" table: ‘Sales_1d | Lasthame Firstwame | Address Salesarea 1 Cruz ‘Jason Dallas Teas 2 Sarin Sofia Los Angeles | Califor 3 Nols Zenia Houston Teas Query: ‘To ADO 2 columns HireDate and. Salary to table Sales, ‘We use the fllowing SQL statement: ‘ALTER TABLE Sales ADD (tHieDate dete, Salary decimal(6,2)); The "Sales" table will now tke this: ‘Sales_Id | LastName | FirstName | Address | SalesArea | HireData | Salary 1 cuz Jason | Dallas. Texas 2 Sarin Sofie Los angeles | California z ‘Nicolas | Zenia | Houston | Texas Change Data Type Example Supe ewer to cege the dase otek naned “Seay he ‘Sse ‘eb We use the folowing SQL statement: ALTER TABLE Sales MODIFY Salary foat.2); “ALTER TABLE Sales [ALTER COLUMN Salary lt{8,2) After running the above query, the Salary Column is chenged from decimal to fat datetype. ALTER DROP COLUMN Example To dla the cok named “HrDta nthe “Sle” able, We use the folowing SL statement ALTER TABLE Saks [DROP COLLIN HireDate Sales Td[ LastName FirstName [Adress —[ Saleshvea [Stary T(Gue Jason | Dall ~ [Teas Tn fla [os ancora Sea “ie [ston [Twas ‘The DROP DATABASE Statement ‘The DROP DATABASE cused to remove en objet from the databace DROP DATABASE database name [DROP TABLE Statement The DROP TABLE statements used to remave a table. NOTE: Be very careful while using the DROP command becouse after a table is deleted ‘then llth data avaiable in tha table wl ako be lost freer, and we cannot get iad DROP TABLE tale name The DELETE Statement ‘The DELETE statement Is used to delete rous ina table SQL DELETE Syntax: DELETE FROM table_name WHERE some_column=some_value Note: The WHERE clause In the DELETE syntax identifies which records) that should be deleted. Ifyou omit the WHERE clause, all records wll be deletes! SQL Delete All Rows Deleting all rows in a table without deleting the table. DELETE FROM table_name DELETE = FROM table name Note: Be very careful when deleting records. You cannot undo this statement! ‘The UPDATE Query SQL UPDATE Example “TheSQL UPDATE Query is Sd to Ue the exiting eer na abl. The "Sales able: ‘SQL UPDATE Syntax ‘Sales_Jd | LastName FirstName [Address | SalesArea 1 cuz “i Dallas Texas 2 ain Sofa Los angeles | Calforis 3 ‘Neos esis Houston Teas 4 Valdez ‘Asha ‘Sen Antonio | Texas “The WHERE couce possible to use wen the UPDATE query to pct the selected rows [5 Falcon uence yeu om the WHERE cles, athe ons wil be update. Now we want to update te salesperson "Flcon, Laurence" inthe "Sle" table, Wie use the following SQL statement UPDATE Sales SET Address=Denver, iy="Colrat’ WHERE Lastiame='Felcn’ AND FistName="Lawence “The 'Seles" table wil now look ke this ‘Sales_Jd| LastName FirstName [Address | SalesArea 1 cuz 1 Dallas Tera 2 Jerin Sofa Los angeles | Calton 3 ‘Neos esi Houston Texas 4 Valdez ‘Aisha ‘Sen Antonio | Texas 5 Falcon Laurence | Derwer Colored SQL FUNCTIONS MySQL AVG() Function ‘AVG() function returns the average of the values in a spectied column, SQL AVG() Synti SELECT AVG{colurn_name) FROM table_name; SQL AVG() Example The COUNT() Function ‘COUNTI) function used to coun the number rons a table st match @ speed cndion, SQL COUNT(column_name) Syntax: The coUNT(elumn_ name incon retuns acount of numberof non NULL vals f | hen exreson. ete: NUL values wil ot be ote othe spc column. SELECT CoUNTeskinn_ name) FROM table rome; ‘SQL COUNT(*) Syntax: The COUNT(*) function allows you to count al rows or nly rows that match a spec ‘onditon SELECT COUNT(*) FROM table nam; ‘SQL COUNT(*) Example ‘SELECT COUNT(") AS NumberOfOrders FROM Orders SQL COUNT(DISTINCT column_name) Example We use the folowing SOL statement to count the numberof unique customers inthe "orders" table. SELECT COUNTIDISTINCT Customer) AS NumberOfOrders FROM Orders; ‘The MAX() Function MAX() function returns the largest value of an expression ofthe selected column, SQL MAX() Syntax SELECT MAx(column_name) FROM table_name The MIN() Function MIN() function returns the smallest value of an expression ofthe selected column, Min() Funetion Syntaxt SELECT MIN(column_name) FROM table_name; ‘The SUMQ Function 'SUM() function calculates the sum value of an expression. ‘SQL SUM() Syntax: SELECT SUM(column_name) FROM table_name;

You might also like