Mysql database
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Mysql database

on

  • 167 views

 

Statistics

Views

Total Views
167
Views on SlideShare
167
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Mysql database Presentation Transcript

  • 1. MYSQL DATABASE● BY THIS YOU CAN EASILY LEARN THEDATABASE.● NOTES- BY MAYANK TIWARI
  • 2. MYSQL DATABASE1. INTRODUCTION OF MYSQL DATABASE2. TYPES OF DATABASE3. ADVANTAGE OF MYSQL DATABASE4. MYSQL QUERIES5. MYSQL CLAUSE6. MYSQL OPERATORS7. MYSQL JOINS8. MYSQL KEYS9. MYSQL DATATYPES
  • 3. INTRODUCTION OF MYSQLDATABASE1. Database is the collection of data, information & records in aorganized way.2. All database used SQL that is structural query language. Ifyou want to deal with the database you used the querylanguage.(query is the instruction to the database given bydeveloper or query is just the syntax you written in alanguage.)(structural query means you write the syntax stepby step or a structural foam.)3. Database is just like pointer who store the value,change thevalue,get the value,post the value4. We use data base for storing the DATAINFORMATION into it.5. MYSQL database is the collection of data, information &records in the form of tables,with related data.
  • 4. TYPES OF DATABASERDBMS :-1. MYSQL DATABASE.2. MS SQL SERVER.3. DB2.4. ORACLE SERVER.5. MS ACCESS.Note - {Syntax of Queries in php :- join->clause->conditional operatores->group by ->order by.}Note – All the sql quary syntax are written on that page.http://www.w3schools.com/sql/sql_quickref.asp
  • 5. ADVANTAGE OF MYSQLDATABASE1. MYSQL is released under an open-source license. Soyou have nothing to pay to use it.2. MYSQL is a very powerful program in its own right. Ithandles a large subset of the functionality of the mostexpensive and powerful database packages.3. MYSQL uses a standard form of the well-known SQLdata language.4. MYSQL works on many operating systems and withmany languages including PHP, PERL, C, C++, JAVAetc.
  • 6. 5. MYSQL works very quickly and works well even withlarge data sets.6. MYSQL is very friendly to PHP, the most appreciatedlanguage for web development.7. MYSQL supports large databases, up to 50 millionrows or more in a table. The default file size limit for atable is 4GB, but you can increase this (if youroperating system can handle it) to a theoretical limitof 8 million terabytes (TB).8. MYSQL is customizable. The open source GPLlicense allows programmers to modify the MYSQLsoftware to fit their own specific environments.maryof the current situation
  • 7. MYSQL QUERIESWhat is DDL, DML and DCL?1. Data Definition Language deals with databaseschemas and descriptions of how the data shouldreside in the database, therefore languagestatements like CREATE TABLE or ALTER TABLEbelong to DDL.2. DML deals with data manipulation, and thereforeincludes most common SQL statements suchSELECT, INSERT, etc.3. Data Control Language includes commands such asGRANT, and mostly concerns with rights,permissions and other controls of the databasesystem.
  • 8. DDLData Definition Language (DDL) statements areused to define the database structure orschema.CREATE - to create objects in the databaseALTER - alters the structure of the databaseDROP - delete objects from the databaseTRUNCATE - remove all records from a table, including all spaces allocatedfor the records are removedCOMMENT - add comments to the data dictionaryRENAME - rename an object
  • 9. DMLData Manipulation Language (DML) statementsare used for managing data within schemaobjects.SELECT - retrieve data from the a databaseINSERT - insert data into a tableUPDATE - updates existing data within a tableDELETE - deletes all records from a table, the space for the records remainMERGE - UPSERT operation (insert or update)CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path todata LOCKTABLE - control concurrency
  • 10. DCLData Control Language (DCL) statements.GRANT - gives users access privileges to databaseREVOKE - withdraw access privileges given with the GRANT command
  • 11. MYSQL QUERIES1. Create database :- To create database & table. Syntax : {createdatabase database name;}{CREATE TABLE authors (id INT, name VARCHAR(20), emailVARCHAR(20));}2. Show :- To show the database & table you have. Syntax : {showdatabases ;}{show table;}3. Use :- To use data base. syntax : {use data base name;}4. select * :- This is used to show the detail information of table.syntax : {select*from table name}5. Like :- It is used to get the data from the table which is most
  • 12. 5. Alter & Modify :- To change the data type of table. syntax : {altertable table name modify column name data type with length;}6. Drop :- It is used to delete database,table,column,index,view.Syntax :{ drop name of thing which you want to drop given name;}7. Update :- It is used to update the table data. syntax : {updatetable name set column name=row name(cell value) where columnname=row name(cell value)}You can chose the multiple value also toupdate.8. Delete :- It is used to delete the data from table. syntax : {deletefrom table name where column name = row name;}9. Describe :- It is used to describe the the table structure. syntax :{describe or desc table name;}
  • 13. 10. Insert into :- It is used to insert data in table. syntax : {insertinto (all column name use , bet ween it) values( all values we want toinsert);}You can chose the multiple value also to insert in singlestatement.11. Specification :- If we want a specific data from the table :- syntax: {select*from table name where column name= row name (cellvalue);}12. Sorting of table content :- It is used to sort the content of table inascending or descending order. Syntax :{select column name(by *you can select all column of table) from table name order by columnname asc or desc;}13. Group By :- This statement is used with aggregate function.where wefetch the values of a column in group.14. Having :- The HAVING clause was added to SQL because the WHEREkeyword could not be used with aggregate functions.10. SQL Having9. SQL Group By10. SQL Having9. SQL Group By
  • 14. MYSQL CLAUSE1. where :- Use to go on the table row position. Or cell position.2. From :- It is used to difine table name by using from.3. To :- It is also for our convienace.4. Add :- To add the column in the table.5. Top :- The TOP clause is used to specify the number of records toreturn.The TOP clause can be very useful on large tables withthousands of records. Returning a large number of records canimpact on performance.Note: Not all database systems support the TOP clause.SQL Server Syntax{SELECT TOP number|percent column_name(s) FROM table_name}{SELECT TOP 2 * FROM table name;}6. Having :- This clause is used with group by statement. Its also aconditional operator.
  • 15. MYSQL OPERATORS1. Conditional operators(AND,OR,NOT) :- That are use to show thedata which is dependent on condition. Syntax :{update table nameset column name=value where column name=value and columnname =value;} same for or.2. Union operator :- It is used to combine all the tables in database& represented all tables data in a single table foam. Syntax :{ select column name(for all column use * at the place of columnname) from table name union select*from table name;} its notshowing the repeated data. But for all the repeated data use unionall at the place of union.3. Between :- The BETWEEN operator is used in a WHERE clause toselect a range of data between two values.4. In :- The IN operator allows you to specify multiple values in aWHERE clause.syntax :{ SELECT column_name(s)FROM table_name WHEREcolumn_name IN (value1,value2,...)}5. Like :- The LIKE operator is used in a WHERE clause to search fora specified pattern in a column.
  • 16. MYSQL KEYS1. KEYS ARE DIFINE IN the tables toFETCH THE UNIQE DATA.2. You make so many keys in a tableaccording to your requirement.
  • 17. TYPES OF KEYS1. SUPER KEY2. CANDIDATE KEY3. PRIMARY KEY4. UNIQE KEY5. SIMPLE KEY6. SECONDRY KEY OR ALTERNATIVE KAY7. COMPOUND KEY8. COMPOSITE KEY9. FOREIGN KEY
  • 18. 1. SUPER KEY :-2. CANDIDATE KEY :-3. SIMPLE KEY :-1. SUPER KEY :-An attribute or a combination of attributethat is used to identify the records uniquely is known as Super Key.A table can have many Super Keys,.any combination which can identify the records uniquely will be aSuper Key.2. CANDIDATE KEY :-A candidate key is a field orcombination of fields that can act as a primary key field for thattable to uniquely identify each record in that table.A candidate key is a sub set of a Super Keys.A candidate key is asub set of a Super Keys.In order to be eligible for a candidate key it must pass certaincriteria.It must contain unique valuesIt must not contain null valuesIt contains the minimum number of fields to ensure uniquenessIt must uniquely identify each record in the tableOnce your candidate keys have been identified you can now selectone to be your primary key
  • 19. 4. SECONDRY OR ALTERNATIVE KAY :-5. COMPOUND KEY :-6. COMPOSITE KEY :-3. SIMPLE KEY :-A simple key consists of a single field touniquely identify a record.primary key is also known as simple key.4. SECONDRY KEY OR ALTERNATIVE KAY :-The attributes that are not even the Super Key but can be still usedfor identification of records (not unique) are known as SecondaryKey.Alternate Key can be any of the Candidate Keys except for thePrimary Key. It is a subset of candidate key.5. COMPOUND KEY :-A Combination of more than onecolumn identifying records of a table uniquely, all the columns thattake part in the combination process are Simple Key’s.6. COMPOSITE KEY :- If we use multiple attributes to createa Primary Key then that Primary Key is called Composite Key. In thisall the other columns that take part in the combination process arenot simple keys.
  • 20. PRIMARY KEY1. The PRIMARY KEY constraint uniquely identifieseach record in a database table.2. Primary keys must contain unique values.3. A primary key column cannot contain NULLvalues.4. Each table should have a primary key, and eachtable can have only ONE primary key.NOTE - YOU CAN REMOVE KEY BY ALTER & DROPAND ADD BY ALTER.& CONSTRAINT IS ALSOTHERE.
  • 21. UNIQUE KEY1. The UNIQUE constraint uniquely identifies each record in adatabase table.2. The UNIQUE and PRIMARY KEY constraints both provide aguarantee for uniqueness for a column or set of columns.3. A PRIMARY KEY constraint automatically has a UNIQUEconstraint defined on it.4. Note - that you can have many UNIQUE constraints pertable, but only one PRIMARY KEY constraint per table.
  • 22. FOREIGN KEY1. A FOREIGN KEY in one table points to a PRIMARYKEY in another table.CREATE TABLE SECOND TABLE NAME(COLUMN 1 int NOTNULL,COLUMN 2 int NOT NULL,COLUMN 3 int,PRIMARY KEY(SECOND TABLE COLUMN 1),FOREIGN KEY (COLUMN 1 INTABLE FIRST) REFERENCES FIRST TABLE NAME (COLUMN 1IN))NOTE – FOREIGN KEY STABILISED THE RELATION BETWEENTWO TABLES WHICH CONTAINS THE AT LEAST ONE SAMECOLUMN.WE CAN ADD & DELETE THE FOREIGN KEY BY ALTER & DROPQUIREY.
  • 23. MYSQL JOINS1. JOINS ARE USE TO JOIN THE TABLES. & FETCH THE DATA OFDIFFERENT TABLES WHICH ARE RELATED BY AT LEAST ONESIMILAR COLUMN BY SINGLE TABLE.In simple words joins arejoin the tables.2. The JOIN keyword is used in an SQL statement to query data fromtwo or more tables, based on a relationship between certain columnsin these tables.3. Tables in a database are often related to each other with keys.4. A primary key is a column (or a combination of columns) with aunique value for each row. Each primary key value must be uniquewithin the table. The purpose is to bind data together, across tables,without repeating all of the data in every table.5. SQL joins are used to query data from two or more tables, based ona relationship between certain columns in these tables.
  • 24. Types of joins1. Inner join2. Outer join3. Full join2. Outer join :-1. Left outer join2. Right outer join
  • 25. 1. Inner join :-The INNER JOIN keywordreturns rows when there is at least onematch in both tables.SQL INNER JOIN SyntaxSELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name=table_name2.column_name2. Left outer join :-The LEFT JOIN keywordreturns all rows from the left table(table_name1), even if there are nomatches in the right table (table_name2).
  • 26. SQL LEFT JOIN SyntaxSELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON table_name1.column_name=table_name2.column_namePS: In some databases LEFT JOIN is called LEFT OUTER JOIN.3. Right outer join :-The RIGHT JOIN keyword returnsall the rows from the right table (table_name2), even if there areno matches in the left table (table_name1).SQL RIGHT JOIN SyntaxSELECT column_name(s)FROM table_name1RIGHT JOIN table_name2ON table_name1.column_name=table_name2.column_namePS: In some databases RIGHT JOIN is called RIGHT OUTERJOIN.
  • 27. 4. Full join :-The FULL JOIN keyword return rows when thereis a match in one of the tables. It shows all the row of right & lefttable whether it is matched or not matched with each other. Mysqlhas the lack support to full join. So we cant use the below syntax inmysql. We use the last syntax in mysql to use full join.Syntax: The basic syntax of FULL JOIN is as follows:SELECT table1.column1, table2.column2...FROM table1FULL JOIN table2ON table1.common_filed = table2.common_field;Note - If your Database does not support FULL JOIN like MySQL does notsupport FULL JOIN, then you can use UNION ALL clause to combile two JOINSas follows:SQL> SELECT ID, NAME, AMOUNT, DATEFROM CUSTOMERSLEFT JOIN ORDERSON CUSTOMERS.ID = ORDERS.CUSTOMER_IDUNION ALLSELECT ID, NAME, AMOUNT, DATEFROM CUSTOMERSRIGHT JOIN ORDERSON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
  • 28. MYSQL DATATYPESMYSQL has many different data types, separated into three categories:numeric, date and time, and string types.1. Numeric Data Types :-1. INT - A normal-sized integer that can be signed or unsigned. If signed,the allowable range is from -2147483648 to 2147483647. If unsigned, theallowable range is from 0 to 4294967295. You can specify a width of up to11 digits.2. TINYINT - A very small integer that can be signed or unsigned. If signed,the allowable range is from -128 to 127. If unsigned, the allowable range isfrom 0 to 255. You can specify a width of up to 4 digits.3. SMALLINT - A small integer that can be signed or unsigned. If signed, theallowable range is from -32768 to 32767. If unsigned, the allowable rangeis from 0 to 65535. You can specify a width of up to 5 digits.4. MEDIUMINT - A medium-sized integer that can be signed or unsigned. Ifsigned, the allowable range is from -8388608 to 8388607. If unsigned, theallowable range is from 0 to 16777215. You can specify a width of up to 9digits.
  • 29. 2. Date and Time Types :-1. DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example, May 15, 1985 would be stored as 1985-05-15.2. DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SSformat, between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. Forexample, 4:30 in the afternoon on December 25th, 1980 would be stored as1980-12-25 16:30:00.3. TIMESTAMP - A timestamp between midnight, January 1, 1970 andsometime in 2037. This looks like the previous DATETIME format, onlywithout the hyphens between numbers; 3:30 in the afternoon on December30th, 1973 would be stored as 19731230153000 ( YYYYMMDDHHMMSS ).4. TIME - Stores the time in HH:MM:SS format.5. YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length isspecified as 2 (for example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69).If the length is specified as 4, YEAR can be 1901 to 2155. The default lengthis 4.
  • 30. 3. String Types :-1. CHAR(M) - A fixed-length string between 1 and 255 characters in length(for example CHAR(5)), right-padded with spaces to the specified lengthwhen stored. Defining a length is not required, but the default is 1.2. VARCHAR(M) - A variable-length string between 1 and 255 characters inlength; for example VARCHAR(25). You must define a length when creating aVARCHAR field.3. BLOB or TEXT - A field with a maximum length of 65535 characters.BLOBs are "Binary Large Objects" and are used to store large amounts ofbinary data, such as images or other types of files. Fields defined as TEXTalso hold large amounts of data; the difference between the two is that sortsand comparisons on stored data are case sensitive on BLOBs and are notcase sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.
  • 31. 4. TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum lengthof 255 characters. You do not specify a length with TINYBLOB or TINYTEXT.5. MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximumlength of 16777215 characters. You do not specify a length withMEDIUMBLOB or MEDIUMTEXT.6. LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximumlength of 4294967295 characters. You do not specify a length with LONGBLOBor LONGTEXT.7. ENUM - An enumeration, which is a fancy term for list. When defining anENUM, you are creating a list of items from which the value must be selected(or it can be NULL). For example, if you wanted your field to contain "A" or"B" or "C", you would define your ENUM as ENUM (A, B, C) and only thosevalues (or NULL) could ever populate that field.
  • 32. MYSQL FUNCTIONS-: AGGREGATE FUNCTIONS :-SQL aggregate functions return asingle value, calculated from valuesin a column.1. SQL avg()2. SQL count()3. SQL first()4. SQL last()5. SQL max()6. SQL min()7. SQL sum()-:SCALAR FUNCTIONS :-SQL scalar functions return a singlevalue, based on the input value.1. SQL ucase()2. SQL lcase()3. SQL mid()4. SQL len()5. SQL round()6. SQL now()7. SQL format()
  • 33. AGGREGATE FUNCTIONSAVG() - Returns the average valueCOUNT() - Returns the number of rowsFIRST() - Returns the first valueLAST() - Returns the last valueMAX() - Returns the largest valueMIN() - Returns the smallest valueSUM() - Returns the sum of column values
  • 34. SCALAR FUNCTIONSUCASE() - Converts a field to upper caseLCASE() - Converts a field to lower caseMID() - Extract characters from a text fieldLEN() - Returns the length of a text fieldROUND() - Rounds a numeric field to the numberof decimals specifiedNOW() - Returns the current system date andtimeFORMAT() - Formats how a field is to bedisplayed
  • 35. NORMALIZATIONNormalization is the by which we refine the data or fillter the datawith uniqueness.Normalization is the process where a database is designed in a waythat removes redundancies, and increases the clarity in organizingdata in a database.Minimize data redundancy i.e. no unnecessarily duplication of data.To make database structure flexible i.e. it should be possible to addnew data values and rows without reorganizing the databasestructure.Data should be consistent throughout the database i.e. it should notsuffer from following anomalies.Insert Anomaly - Due to lack of data i.e., all the data available for insertionsuch that null values in keys should be avoided. This kind of anomaly canseriously damage a database
  • 36. Update Anomaly - It is due to data redundancy i.e. multipleoccurrences of same values in a column. This can lead toinefficiency.Deletion Anomaly - It leads to loss of data for rows that are notstored else where. It could result in loss of vital data.Complex queries required by the user should be easy to handle.On decomposition of a relation into smaller relations with fewerattributes on normalization the resulting relations whenever joinedmust result in the same relation without any extra rows. The joinoperations can be performed in any order. This is known asLossless Join decomposition.The resulting relations (tables) obtained on normalization shouldpossess the properties such as each row must be identified by aunique key, no repeating groups, homogenous columns, eachcolumn is assigned a unique name etc.
  • 37. TYPES OF NORMAL FORM1. 1st Normal Form or 1NF2. 2nd Normal Form or 2NF3. 3nd Normal Form or 3NF4. BCNF (Boyce & Codd)5. 4NF6. 5NF7. 6NF