13-MySQL Basics cs 12
13-MySQL Basics cs 12
DELETE SAVEPOINT
DROP
Creating a Database.
The following command will create School database in MySQL.
mysql> CREATE DATABASE School;
Opening a database
To open an existing database, following command is used.
mysql> USE school ;
Getting listings of database and tables
mysql> SHOW DATABASES;
mysql> SHOW TABLES;
Deleting a Database and Table
mysql> DROP DATABASE School;
mysql> DROP TABLE Student;
Viewing Table Structure
Select database();
mysql> DESCRIBE Student; Shows the name of
currently open database
Working with MySQL Tips: Remember
N-D-S-C
N-Name, D- Data type,
Creating Simple Tables: S- Size, C- Constraints
CREATE TABLE < Table Name>
(<Col name1><data type>[(size)] [Constraints],….);
Data types- INTEGER, NUMERIC(P,D), CHAR(n), VARCHAR(n), DATE etc.
mysql> CREATE TABLE Emp (empID integer,ename char(30),
city char(25), pay decimal(10,2));
All commands
in MySQL
are ended
with ;
Emp
empID ename city pay
Making Simple Queries Using SELECT
The SELECT command of SQL, empowers you to make a
request (queries) to retrieve stored records from the table.
The syntax of SQL is given below-
Syntax Notation: <> User given values [ ] optional clause
SELECT < [Distinct | ALL] *| column name(s)>
FROM <table(s)> [WHERE <condition> ]
[ORDER BY <column(s)> [ASC | DESC] ] ;
[GROUP BY <column(s)> [HAVING <condition>] ] ;
Consider the table Student having some records as –
StID Name Fname DOB City Class
S1 Amitabh Harivansh Rai 1948-11-10 Allahabad 12
S2 Sharukh Firoz 1970-05-10 Delhi 11
S3 Irphan Akbar 1970-10-05 Jaipur 11
S4 Salman Salim Javed 1972-04-10 Mumbai 10
S5 Abhishek Amitabh 1975-03-12 Mumbai 10
Making Simple Queries – Cont..
Selecting all columns
If you want to view all columns of the student table, then you
should give the following command- * represents
mysql> SELECT * FROM Student ; all columns.
MySQL will display the all records with all columns in the Student table.
City
Only Unique Cities
Allahabad
are displayed
Delhi
Jaipur
Mumbai
Making Simple Queries – Cont..
Doing simple calculations
We can also perform simple calculations with SQL Select command.
SQL provide a dummy table named DUAL which can also be used.
mysql> SELECT 4*3 ; OR SELECT 4*3 FROM DUAL;
We can also extend this idea with a columns of the existing table.
mysql> SELECT Name, Sal *12 FROM EMP ;
Relational Operators
We can use the following Relational operators while making
condition.
=, > , < , >=, <=, <>, IS , LIKE, IN, BETWEEN
Logical Operators
We can use the following Logical Operators to connect two
conditions.
OR , AND , NOT (!)
mysql> SELECT Name, City from Student
WHERE City <> ‘Mumbai’ AND Class>10;
ASC is used for Ascending and DESC is used for descending order.
Emp
Code Name Sal
Aggregate function
ignores NULL values i.e.
E1 Ram Kumar NULL NULL values does not
E2 Suchitra 4500 play any role in
E3 Yogendra NULL calculations.
E4 Sushil Kr 3500
E5 Lovely 4000
What is Function?
A function is a special types of command that
performs some operation and returns a single value as
a result.
It is similar to method or library functions in Python,
which can be called by giving some argument.
Types of Functions:
Numeric Functions
String Functions
Date & Time Function
Aggregate Functions
Numeric Functions
These functions may accept some numeric values and
performing required operation, returns numeric values as result.
Aggregate Functions should not be used with other columns which may
have multiple values in the table. The following query is illogical and
wrong. Why? Think yourself….
Select sum(pay), name from Employee;
Inserting Records in a Table
You can insert record in the table by using by using the
following DML command.
INSERT INTO <Table Name> [<Column list>]
VALUES <list of values>
Suppose a table named STUDENT has been created with the
following structure.
StID NAME FNAME DOB CITY CLASS
Child Table in
CREATE TABLE Employee
which Foreign
( EmpNo char(3) NOT NULL PRIMARY KEY, key is defined.
Name char(30) NOT NULL,
City char(20),
Sal decimal(8,2), Parent table and column
to be referenced..
DeptNo char(2),
FOREGIN KEY (DeptNo) REFERENCES Departmet (DeptNo));
Command Description
CREATE Used to create database and tables with contratins
D ALTER Used to add, delete, modify columns in the existing
D
table i.e. modifies table structure.
L
DROP Used to delete table and database
SELECT Used to access/display stored records in the tables
D INSERT Used to insert new record in the table
M
DELETE Used to delete existing records from the table
L
UPDATE Used to modify values of records.