0% found this document useful (0 votes)
21 views

Mysql Introduction 1

The document provides information about MySQL commands for data definition, data manipulation, transaction control, and functions. It includes examples of using: - CREATE, ALTER, DROP commands to define and modify databases and tables - INSERT, SELECT, UPDATE, DELETE commands to manipulate data in tables - COMMIT, ROLLBACK commands to control transactions - Aggregate functions like COUNT, MIN, MAX to perform calculations on table columns

Uploaded by

mohd naved
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Mysql Introduction 1

The document provides information about MySQL commands for data definition, data manipulation, transaction control, and functions. It includes examples of using: - CREATE, ALTER, DROP commands to define and modify databases and tables - INSERT, SELECT, UPDATE, DELETE commands to manipulate data in tables - COMMIT, ROLLBACK commands to control transactions - Aggregate functions like COUNT, MIN, MAX to perform calculations on table columns

Uploaded by

mohd naved
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

MYSQL INTRODUCTION

DATA DEFINITION LANGUAGE (DDL) CREATE, ALTER, DROP

CREATE DATABASE EMP;

 CREATION OF A TABLE

CREATE TABLE EMPLOYEE


( ECODE INTEGER NOT NULL UNIQUE,
ENAME CHAR(20) NOT NULL,
SEX CHAR(1)
);

CREATE TABLE SALARY


(
ECODE INTEGER FOREIGN KEY (ECODE) REFERENCES EMPLOYEE,
SALAMT INTEGER CHECK (SALAMT>10000)
);

 MODIFICATION IN A TABLE

ALTER TABLE SALARY ADD(ACCNO INTEGER);

ALTER TABLE EMPLOYEE MODIFY ENAME CHAR(30);

ALTER TABLE EMPLOYEE CHANGE ENAME EMPNAME CHAR(30);

ALTER TABLE EMPLOYEE DROP COLUMN SEX;

 REMOVING TABLE
DROP TABLE EMPLOYEE;

 REMOVING OF DATABASE WITH THEIR TABLES


DROP DATABASE EMP;

1
DATA MANIPULATION LANGUAGE (DML)
INSERT, SELECT, UPDATE, DELETE

 INSERTION OF ROWS IN A TABLE

INSERT INTO EMPLOYEE VALUES (1001,‘RAVI KUMAR’,‘M’);

INSERT INTO EMPLOYEE (ECODE, EMPNAME) VALUES (1002,‘KAMAL KUMAR’);

* Date format for insertion - ‘2010-04-20’. FOUR DIGIT YEAR-TWO DIGIT MONTH-TWO DIGIT DAY.

 SEARCHING OF ROWS IN A TABLE

SELECT * FROM EMPLOYEE;

SELECT ECODE, EMPNAME FROM EMPLOYEE;

SELECT * FROM SALARY WHERE SALAMT>20000;

SELECT EMPNAME FROM SALARY WHERE SALAMT>20000;

 MODIFICATION OF ROWS IN A TABLE

UPDATE EMPLOYEE SET ECODE=2001 WHERE EMPNAME=’RAVI KUMAR’;

UPDATE EMPLOYEE SET ECODE=2002 WHERE ECODE=1002;

UPDATE SALARY SET SALARY=25000 WHERE (ECODE >2000 AND ECODE<=3000);

 DELETION OF ROWS IN A TABLE

DELETE FROM EMPLOYEE;

DELETE FROM EMPLOYEE WHERE ECODE=2001;

DELETE FROM SALARY WHERE SALAMT >50000;

2
TRANSACTION CONTROL LANGUAGE (TCL)
COMMIT, SAVEPOINT, ROLLBACK, SET TRANSACTION

 SAVING DATA PERMANENTLY;

COMMIT;

 MAKING OF POINT FOR ROLLBACK

CREATE SAVEPOINT S1;

 ROLLBACK TO SAVEPOINT

ROLLBACK SAVEPOINT S1;

 OTHER USEFUL COMMANDS

USE EMP -- Make current database

SHOW TABLES -- To see all the tables from the current database.

DESCRIBE SALARY -- To display the structure of table i.e. columns names their datatype,
OR width, constraints
DESC SALARY

3
MORE OPTION FOR DML COMMANDS

SELECT COMMAND
* Means all columns otherwise you specify the column names
SELECT * FROM EMPLOYEE;

SELECT ECODE, EMPNAME FROM EMPLOYEE;

 CONDITIONS WITH WHERE


SELECT * FROM SALARY WHERE SALAMT>20000;

SELECT EMPNAME FROM SALARY WHERE SALAMT>20000;

 ALL
SELECT ALL SALAMT FROM SALARY;

 DISTINCT
SELECT DISTINCT SALAMT FROM SALARY;

 CALCULATOR
SELECT 5+68;
SELECT 5+68 FROM DUAL;
SELECT ECODE, SALAMT*12 FROM SALARY;

 CURRENT DATE
SELECT CURDATE();

 DISPLAY DATA WITH OTHER HEADING


SELECT SALAMT*12 AS “ANNUAL SALARY” FROM SALARY
SELECT 22/7 AS PI;

 HANDLING NULLS
SELECT NAME, BIRTH, DEATH FROM ABC;
SELECT NAME, BIRTH, IFNULL(DEATH, ”ALIVE”) FROM ABC;

 PUTTING TEXT IN SELECT QUERY


SELECT EMPNAME, ‘GETS THE SALARY PER MONTH’ SALAMT FROM SALARY;

 USE OF RELATIONAL OPERATORS: = , > , < , >= , <= , <>


SELECT * FROM EMPLOYEE WHERE ECODE<>2001;

 USE OF LOGICAL OPERATORS: OR / || , AND / && , NOT / !


SELECT * FROM EMPLOYEE WHERE ECODE=2001 OR EMPNAME=’RAVI KUMAR’;
SELECT * FROM EMPLOYEE WHERE ECODE=2001 AND EMPNAME=’RAVI KUMAR’;
SELECT * FROM EMPLOYEE WHERE (NOT ECODE=2001);

4
 BETWEEN
SELECT ECODE, EMPNAME FROM SALARY WHERE SALAMT BETWEEN 20000 AND 50000;
SELECT ECODE, EMPNAME FROM SALARY WHERE SALAMT NOT BETWEEN 20000 AND 50000;

 IN
SELECT * FROM SALARY WHERE SALAMT IN (10000,19270,24500,70000);

 LIKE
SELECT * FROM EMPLOYEE WHERE EMPNAME LIKE ‘A%’; -- Starts with A.
SELECT * FROM EMPLOYEE WHERE EMPNAME LIKE ‘----‘; -- Exact four characters.
SELECT * FROM EMPLOYEE WHERE EMPNAME LIKE ‘---&‘; -- At least three characters.

 NULL
SELECT * FROM EMPLOYEE WHERE EMPNAME IS NULL;

 ORDER BY
SELECT * FROM EMPLOYEE ORDER BY EMPNAME;
SELECT * FROM SALARY WHERE SALAMT >50000 ORDER BY ECODE DESC;

 USING ALIAS
SELECT ECODE SALAMT*12 “ANNUAL SALARY” FROM SALARY ORDER BY “ANNUAL SALARY”

5
USE OF FUNCTIONS WITH DML STATEMENTS
 STRING FUNCTION
SELECT CHAR(65); -- To display the character against ASCII CODE 65.
SELECT CONCAT(ECODE, EMPNAME) AS “CODENAME” FROM EMPLOYEE; -- To merge two columns.
SELECT LOWER(EMPNAME) AS “EMPLOYEENAME” FROM EMPLOYEE; -- Display in lower case.
SELECT LCASE(EMPNAME) AS “EMPLOYEENAME” FROM EMPLOYEE; -- Display in lower case.
SELECT UPPER(EMPNAME) AS “EMPLOYEENAME” FROM EMPLOYEE; -- Display in UPPER case.
SELECT UCASE(EMPNAME) AS “EMPLOYEENAME” FROM EMPLOYEE; -- Display in UPPER case.
SELECT SUBSTR(EMPNAME,1,3) FROM EMPLOYEE; -- Display first three character from empname.
SELECT RTRIM(EMPNAME) FROM EMPLOYEE; -- Removes space from right side of empname.
SELECT LTRIM(EMPNAME) FROM EMPLOYEE; -- Removes space from left side of empname.
SELECT TRIM(EMPNAME) FROM EMPLOYEE; -- Removes space from both sides of empname.

MORE ABOUT TRIM


SELECT TRIM(LEADING ‘ ‘ FROM ‘ RAVI KUMAR ‘); -- Removes spaces from leading / left side.
SELECT TRIM(LEADING ‘A‘ FROM ‘AAAARAVI KUMARAAA‘); -- Removes A from leading / left side.
SELECT TRIM(BOTH ‘ ‘ FROM ‘ RAVI KUMAR ‘); -- Removes spaces from both sides.
SELECT TRIM(BOTH ‘A‘ FROM ‘AAAARAVI KUMARAAA‘); -- Removes A from both sides.
SELECT TRIM(TRAILING ‘ ‘ FROM ‘ RAVI KUMAR ‘); -- Removes spaces from trailing / right side.
SELECT TRIM(TRAILING ‘A‘ FROM ‘AAAARAVI KUMARAAA‘); -- Removes A from trailing / right side.

SELECT ECODE, INSTR(EMPNAME, ‘R’) FROM EMPLOYEE; -- Display position of R in empname.


SELECT ECODE, LENGTH(EMPNAME) FROM EMPLOYEE; -- Display No. of characters in empname.
SELECT ECODE, LEFT(EMPNAME,3) FROM EMPLOYEE; -- Display three characters from left side of empname.
SELECT ECODE, RIGHT(EMPNAME,3) FROM EMPLOYEE; -- Display three characters from right side of empname.
SELECT ECODE, MID(EMPNAME,3,2) FROM EMPLOYEE; -- Display 2 characters starts 3rd character of empname.

 NUMERIC FUNCTION
SELECT ECODE, MOD(15,6) FROM DUAL; -- display remainder 3.
SELECT MOD(SALAMT,1000) FROM SALARY;
SELECT POWER(5,3) FROM DUAL; -- Calculate 5 raise to power 3. i.e.75.
SELECT ROUND(15.193, 1) FROM DUAL; --- Display 15.2 (Round off)
SELECT ROUND(15.193, -1) FROM DUAL; --- Display 20 (Round off)
SELECT SIGN(-45) FROM DUAL; -- Display -1.
SELECT SQRT(64) FROM DUAL; -- Calculate SQUARE ROOT. i.e. 8.
SELECT TRUNCATE (15.19,1) FROM DUAL;--- Display 15.1.

 DATE/TIME FUNCTION
CURDATE()/CURRENT_DATE()
SELECT CURDATE(); -- Display system date.
SELECT DATE(‘2010-04-13 01:02:33’); -- Display 2010-04-13 (date only).
SELECT MONTH(‘2010-04-13’); --- Display month only i.e. 04.
SELECT YEAR(‘2010-04-13’); --- Display year only i.e. 2010.
SELECT DAYOFMONTH(‘2010-04-13’); --- Display day only i.e. 13.
SELECT DAYNAME((‘2010-04-13’); --- Display day i.e. TUESDAY.
SELECT DAYOFWEEK((‘2010-04-13’); --- Display day of week only i.e. 3
SELECT DAYOFYEAR((‘2010-04-13’); --- Display day of year only i.e. 103.
SELECT NOW(); -- Display system date & time.
SELECT SYSDATE();-- Display system date & time.

6
7
MySQL Aggregate Functions: SUM, AVG, MAX, MIN , COUNT, DISTINCT

Aggregate Functions are all about


 Performing calculations on multiple rows
 Of a single column of a table
 And returning a single value.
COUNT Function
The COUNT function returns the total number of values in the specified field. It works on both numeric and
non-numeric data types. All aggregate functions by default exclude nulls values before working on the
data.
COUNT (*) is a special implementation of the COUNT function that returns the count of all the rows in a
specified table. COUNT (*) also considers Nulls and duplicates.
The table shown below shows data in movierentals table

reference_ transaction_ return_date membership_ movie_id movie_


number date number returned
11 20-06-2012 NULL 1 1 0

12 22-06-2012 25-06-2012 1 2 0

13 22-06-2012 25-06-2012 3 2 0

14 21-06-2012 24-06-2012 2 2 0

15 23-06-2012 NULL 3 3 0

we want to get the number of times that the movie with id 2 has been rented out
SELECT COUNT(movie_id) FROM movierentals WHERE movie_id = 2;
Executing the above query in MySQL workbench against myflixdb gives us the following results.
COUNT('movie_id')
3
DISTINCT Keyword
The DISTINCT keyword that allows us to omit duplicates from our results. This is achieved by grouping similar
values together. To appreciate the concept of Distinct, lets execute a simple query

SELECT movie_id FROM movierentals;


movie_id
1
2
2
2
3
Now let's execute the same query with the distinct keyword -
SELECT DISTINCT movie_id FROM movierentals;
As shown below, distinct omits duplicate records from the results.
movie_id
1
2
3

MIN function
The MIN function returns the smallest value in the specified table field.

8
As an example, let's suppose we want to know the year in which the oldest movie in our library was released,
we can use MySQL's MIN function to get the desired information.
The following query helps us achieve that
SELECT MIN(year_released) FROM movies;

Executing the above query in MySQL workbench against myflixdb gives us the following results.
MIN(year_released)
2005

MAX function
Just as the name suggests, the MAX function is the opposite of the MIN function. It returns the largest value
from the specified table field.
Let's assume we want to get the year that the latest movie in our database was released. We can easily use
the MAX function to achieve that.
The following example returns the latest movie year released.
SELECT MAX(year_released) FROM movies;

Executing the above query in MySQL workbench using myflixdb gives us the following results.
MAX(year_released)
2012

SUM function
Suppose we want a report that gives total amount of payments made so far. We can use the
MySQL SUM function which returns the sum of all the values in the specified column. SUM works on
numeric fields only. Null values are excluded from the result returned.
The following table shows the data in payments table-
payment_ membership_ payment_ description amount_ external_ reference
id number date paid _number

1 1 23-07-2012 Movie rental payment 2500 11

2 1 25-07-2012 Movie rental payment 2000 12

3 3 30-07-2012 Movie rental payment 6000 NULL


The query shown below gets the all payments made and sums them up to return a single result.
SELECT SUM(amount_paid) FROM payments;
Executing the above query in MySQL workbench against the myflixdb gives the following results.
SUM(amount_paid)
10500
AVG function
MySQL AVG function returns the average of the values in a specified column. Just like the SUM function,
it works only on numeric data types.
Suppose we want to find the average amount paid. We can use the following query -
SELECT AVG(amount_paid) FROM payments;
Executing the above query in MySQL workbench, gives us the following results.
AVG(amount_paid)
3500

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy