mysql
DESCRIPTION
mysqlTRANSCRIPT
![Page 1: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/1.jpg)
MYSQLMYSQL
DEFINITIONDEFINITION MySQL, pronounced either "My S-Q-L" or "My
Sequel," is an open source relational database management system. It is based on the structure query language (SQL), which is used for adding, removing, and modifying information in the database. Standard SQL commands, such as ADD, DROP, INSERT, and UPDATE can be used with MySQL.
![Page 2: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/2.jpg)
BASIC MySQL COMMANDBASIC MySQL COMMAND
* CREATE TABLE syntax* DROP TABLE syntax* DELETE syntax * SELECT syntax * JOIN syntax * INSERT syntax * REPLACE syntax * UPDATE syntax
![Page 3: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/3.jpg)
BASIC QURIESBASIC QURIES
CREATE TABLECREATE TABLE This command is used to create structure
of the table. SyntaxSyntax: Create table <tablename>(list of col
Definition1,....); Example:Example: Create table emp1 (Emp ID (number(3)
primary key, Name(varchar(20), Age(number(), DOB(date));
![Page 4: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/4.jpg)
DROP TABLEDROP TABLE
Syntax:Syntax: Drop table [if exists] tbl_name
Explanation:Explanation: DROP TABLE removes one or more tables. All table
data and the table definition are removed.You can use the keywords IF EXISTS to prevent an error from
occurring for tables that don't exist.
![Page 5: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/5.jpg)
DELETEDELETE
Syntax: Delete from <tablename>;
Example: Delete from emp1;
Explanation: This command is used to delete the rows and
column.
![Page 6: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/6.jpg)
SELECT
Syntax:Syntax: Select * from <tablename>;
Example:Example: Select * from emp1;
Explanation: This command is used to describe the
structure of the table.
![Page 7: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/7.jpg)
INSERT VALUEINSERT VALUE
Syntax: Insert into <tablename> values (list of
values);
Example: Insert into emp1 values (11, Anu, 20,30-aug-
1989);
Explanation: This command is used to insert values into
the structure of the table.
![Page 8: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/8.jpg)
REPLACEREPLACE
Syntax:Syntax: REPLACE [INTO] tbl_name [(col_name,...)]
VALUES (expression,...)
Explanation:Explanation: REPLACE works exactly like INSERT, except that
if an old record in the table has the same value as a new record on a unique index, the old record is deleted before the new record is inserted.
![Page 9: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/9.jpg)
UPDATEUPDATE
Syntax:Syntax: UPDATE [table] SET [column]=[value] WHERE [criteria]
UPDATE Used_Vehicles SET mileage=66000 WHERE vehicle_id=1; UPDATE [table] SET [column]=[value] WHERE [criteria]
Example:
UPDATE Used_Vehicles SET mileage=66000 WHERE vehicle_id=1;
Explanation: UPDATE updates columns in existing table rows with new values.
The SET clause indicates which columns to modify and the values they should be given. The WHERE clause, if given, specifies which rows should be updated. Otherwise all rows are updated.
![Page 10: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/10.jpg)
ADVANCED COMMANDSADVANCED COMMANDS
AS ALTER and ADD UNION JOIN TEMPORARY Table TRUNCATE Table
![Page 11: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/11.jpg)
ASAS
Syntax: SELECT <columns>FROM
<existing_table_name>AS <new_table_name>
Example: SELECT t1.name -> FROM artists -> AS t1;
Explanation: It is used to create a shorthand reference to elements with long names to make the SQL statements shorter and reduce the chance of typos in the longer names.
![Page 12: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/12.jpg)
ALTERING THE DATABASE ALTERING THE DATABASE STRUCTURE AND ADDING DATASTRUCTURE AND ADDING DATA
Syntax:ALATER TABLE tablenameADD clm_name type
Example:Example: ALTER TABLE cds -> ADD producerID INT(3);
![Page 13: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/13.jpg)
UNION JOINSUNION JOINSSyntax: Select <fields>from <table> where <condition> unionSELECT <fields> FROM <table>WHERE <condition>
Example: SELECT artist FROM artists WHERE (artists.name LIKE 'P
%') UNION SELECT artists.name FROM artists WHERE
(artists.name LIKE 'G%');
Explanation: Union Joins allow the results of two queries to be
combined into one outputted result set. This is done by having the 2 (or more) queries glued together by the UNION operator.
![Page 14: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/14.jpg)
CREATING THE TEMPORARY CREATING THE TEMPORARY TABLETABLE
Definition: The syntax for creating temporary tables is almost identical that
used for creating a normal table. Except that there is an extra TEMPORARY clause.
Syntax: CREATE TEMPORARY TABLE <table> (field definition)
CREATE TEMPORARY TABLE <newtable>SELECT * FROM <oldtable>
![Page 15: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/15.jpg)
TRUNCATE TABLETRUNCATE TABLE
Syntax: TRUNCATE TABLE <table_name>
Example: TRUNCATE TABLE emp1;
![Page 16: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/16.jpg)
FUNCTIONS IN MYSQLFUNCTIONS IN MYSQL
Aggregate functionsNumeric functions String functions
![Page 17: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/17.jpg)
AGGREGATE FUNCTIONSAGGREGATE FUNCTIONS
Syntax: SELECT COUNT(*) FROM table_name ;
Use: Mysql COUNT function is useful in counting the number
of records.
![Page 18: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/18.jpg)
MAX AND MIN FUNCTIONSMAX AND MIN FUNCTIONS
Syntax:
SELECT MAX(Col_name) FROM table_name;
Use:
MySQL MAX function is used to find out the record with maximum value among a record set.
![Page 19: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/19.jpg)
MIN( )MIN( )
Syntax:
SELECT MIN(Col_name) FROM table_name;
Use:
MySQL MIN function is used to find out the record with minimum value among a record set.
![Page 20: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/20.jpg)
AVG( )
Syntax: SELECT AVG(Col_name) FROM table_name;
Use: MySQL AVG function is used to find out the
average of a field in various records.
![Page 21: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/21.jpg)
SUM( )SUM( )
Syntax: SELECT SUM(Col_name) FROM table_name;
Use:
MySQL SUM function is used to find out the sum of a field in various records.
![Page 22: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/22.jpg)
RAND( )RAND( )
Syntax:
SELECT RAND( );
Use:
MySQL has a RAND function that can be invoked to produce random numbers between 0 and 1
![Page 23: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/23.jpg)
NUMERIC FUNCTIONSNUMERIC FUNCTIONS
Syntax:
ABS(X);Use:
The ABS() function returns the absolute value
of X.
![Page 24: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/24.jpg)
BIT_COUNT( )BIT_COUNT( )
Synatx:Synatx:
BIT_COUNT(numeric_value)Use: Use:
The BIT_COUNT() function returns the
number of bits that are active in numeric_value.
![Page 25: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/25.jpg)
CEIL( ) / CEILING( )CEIL( ) / CEILING( )
Syntax: CEIL(X)
CEILING(X)Use: These function return the smallest integer value
that is not smaller than X.
![Page 26: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/26.jpg)
FLOOR( )FLOOR( )
Syntax: FLOOR(X)
Use:
This function returns the largest integer value that is not greater than X.
![Page 27: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/27.jpg)
GREATEST( )GREATEST( )
Syntax: GREATEST(n1,n2,n3,..........)
Use: The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3,
a nd so on).
![Page 28: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/28.jpg)
LEAST( )LEAST( )
Syntax:
LEAST(N1,N2,N3,N4,......)
Use: Its purpose is to return the least-valued item
from the value list (N1, N2, N3, and so on).
![Page 29: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/29.jpg)
PI( )PI( )
Syntax: PI()
Use:
This function simply returns the value of pi. MySQL internally stores the full double-precision
value of pi.
![Page 30: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/30.jpg)
POW( ) / POWER( )POW( ) / POWER( )
Syntax:
POW(X,Y)POWER(X,Y)
Use:
These two functions return the value of X raised to the power of Y.
![Page 31: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/31.jpg)
ROUND( )ROUND( )
Syntax :
ROUND(X)ROUND(X,D)
Use:
This function returns X rounded to the nearest integer. If a second argument, D, is supplied,
then the function returns X rounded to D decimal places.
![Page 32: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/32.jpg)
SIN( )SIN( )
Syntax: SIN(X)
Use: This function returns the sine of X
![Page 33: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/33.jpg)
SQRT( )
Syntax: SQRT(X)
Use:
This function returns the non-negative square root of X.
![Page 34: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/34.jpg)
TRUNCATE( )TRUNCATE( )
Syntax: TRUNCATE(X,D)
Use: This function is used to return the value of X
truncated to D number of decimal places.
![Page 35: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/35.jpg)
STRING FUNTIONSSTRING FUNTIONS
Syntax: ASCII(str)
Use: Returns the numeric value of the leftmost
character of the string str.
![Page 36: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/36.jpg)
BIN( )
Syntax: BIN(N)
Use:
Returns a string representation of the binary value of N,
![Page 37: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/37.jpg)
BIT LENGTH( )
Syntax:
BIT_LENGTH(str)Use:
Returns the length of the string str in bits.
Example:
SELECT BIT_LENGTH('text');BIT_LENGTH('text')
32
![Page 38: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/38.jpg)
CHAR( )Syntax:
CHAR(N,... [USING charset_name])
Use: CHAR() interprets each argument N as an
integer and returns a string consisting Example:
SELECT CHAR(77,121,83,81,'76');
MySQL
![Page 39: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/39.jpg)
CHAR LENGTHCHAR LENGTH
Syntax: CHAR_LENGTH(str)
Use: Returns the length of the string str, measured in
characters. A multi-byte character counts as a single character
![Page 40: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/40.jpg)
CONCAT( )CONCAT( )
Syntax: CONCAT(str1,str2,...)
Use: MySQL CONCAT function is used to concatenate
two strings to form a single string.
![Page 41: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/41.jpg)
FIELD( )
Syntax:
FIELD(str,str1,str2,str3,...)Use:
Returns the index (position starting with 1) of str in the str1, str2, str3, ... list. Returns 0 if str is
not found.
![Page 42: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/42.jpg)
FIND_IN_SET( )FIND_IN_SET( )
Syntax: FIND_IN_SET(str,strlist)
Use: Returns a value in the range of 1 to N if the string
str is in the string list strlist consisting of N substrings.
![Page 43: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/43.jpg)
INSERT( )INSERT( )
Syntax:
INSERT(str,pos,len,newstr)Use:
Returns the string str, with the substring beginning at position pos and len characters
long replaced by the string newstr.
![Page 44: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/44.jpg)
LCASE( ) / LOWER( )LCASE( ) / LOWER( )
Syntax:
LOWER(str)Use:
Returns the string str with all characters changed
to lowercase according to the current character set mapping.
![Page 45: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/45.jpg)
To export a database, use the mysqldump utility normally located in your mysql/bin directory . For example, to export all the tables and data for a database named guestdb.
Syntax:Syntax:mysqldump guestdb > guestdb.txt
Exporting a DatabaseExporting a Database
![Page 46: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/46.jpg)
This will create a text file containing all the commands necessary to recreate all the tables and data found in guestdb. However, what if I want to export only one table? To do this the command is modified as follows assuming guestTbl is the table to be exported.
Syntax:mysqldump guestdb guestTbl > guestdb.txt
![Page 47: Mysql](https://reader036.vdocuments.net/reader036/viewer/2022070317/556209d1d8b42a00138b47d0/html5/thumbnails/47.jpg)
With the data in a text file, its time to import the data back into MySQL. This can be done by passing the commands contained in the text file into the MySQL client.
For example:mysql -p --user=username < guestdb.txt
This passes all the commands in the file into the mysql client just like you were typing them in.
Importing the DatabaseImporting the Database