You are on page 1of 14

Create Database

Syntax - CREATE DATABASE database name; Mysql> Create database Bioinfo ; Query OK, 1 row affected (0.00 sec)

TO Use Current database Syntax USE database name; Mysql> USE Bioinfo; Database changed

To Show All database


Syntax SHOW DATABASES; Mysql> SHOW DATABASES; Database bioinfo mysql test 3 rows in set (0.03 sec)

To Create Table
Syntax CREATE TABLE table name (Attribute List with data type); Mysql> CREATE TABLE student ( stud_id char(5) , name varchar(20) , dob date, sex char(1)); Querry OK, 0 rows affected (0.09 sec)

To Alter Table
Syntax ALTER TABLE table name; Mysql >ALTER TABLE student ADD PRIMARY KEY(stud_id); Mysql> ALTER TABLE student ADD COLUMN course char(5); Mysql> ALTER TABLE student DROP COLUMN sex; Mysql> ALTER TABLE student ALTER COLUMN ;

To View Schema

Syntax - DESCRIBE table name; Mysql> DISCRIBE student;

SELECT STATEMENT
Syntax -SELECT * FROM table name ; Mysql>Select * from professor;

Mysql>Select name, dob, sex from student;

Where Clause Syntax - SELECT * FROM table name WHERE Condition ; Mysql>SELECT * FROM student where sex =M;

Distinct Vs All By default Duplicate are not removed . For Removing Duplicate explicitly apply DISTINCT SELECT [DISTINCT] Attribute name FROM table name; Mysql>select all city from student;

Mysql> Select distinct city from student;

Order By Clause
Syntax- SELECT * FROM table name (Condition ) ORDER BY attribute name [ASC/DESC]; ASC - Ascending order.(Default) DESC Descending order. Mysql> Select * from student ORDER BY name;

Mysql> Select * from student ORDER BY name desc;

Between Operation
Syntax -SELECT * FROM table name where attribute name BETWEEN value1 AND value2; Mysql> Select * from marks where third between 70 and 75;

Logical Operater
<, >, <=, >=, =, Mysql> Select * from mark where first >=65 and first <=70;

Rename Opration Mysql> Select s.stud_id, name, first, second from student as s, marks as m where s.stud_id = m.stud_id;

Mysql> Select name, post as designation from professor;

String Operation
% - matches any substring. _ - underscore matches any character. Syntax- SELECT * FROM table name WHERE attribute name LIKE string;

Mysql> Select * from student where name like ___sh%;

Set Operation
Union To find all entry in two relation. (Eliminates Duplicate) Mysql> Select teacher from sem_one union Select teacher from sem_two;

Union all Retian Duplicates. Mysql> Select teacher from sem_one union all Select teacher from sem_two;

Intersect Two find All common entries in relation. Mysql> Select teacher from sem_one intersect Select teacher from sem_two; Mysql Select teacher from sem_one except select teacher from sem_two;

Aggregate function
average value - avg minimum value - min maximum value - max total sum of values - sum number in group - count Mysql> Select avg(first), avg(second), avg(third), avg(fourth) from marks;

Mysql>select stud_id, first from first where first = (select max(first) from marks);

Mysql>select count(second) from marks where second between 50 and 60;

Null Attribute Mysql> select * from professor where subject is null;

Join
Used to join two table. SELECT [Attribute list]/* FROM table name1 [LEFT/RIGHT JOIN ] table name2 ON condition; Examlpe Table -1 People

Example Table 2 - Property

Left Join Mysql>Select Name, people.id, selling, cost, phone from people left join property on people.id = property.id;

Right Join Mysql>Select Name, People.id, selling, cost phone from people right join property on people.id = property.id;

To Create view
Syntax CREATE VIEW view name AS (SELECT statement); Mysql>Create view stud_mark as select s.stud_id, name, sex as Gender, (first + second + third + fourth) as Total from student as s, marks as m where s.stud_id = m.stud_id;

Mysql> Select * from stud_mark;

To Load Data into Table from Text file


Syntax- load data local infile <path> into table <Table name> [fields terminated by \t lines terminated by \r\n]; Mysql>load data local infile E:\mysql\student.txt into table stud;

To unload table from database to text file


Syntax SELECT * FROM table name INTO outfile path; Mysql> Select * from people into outfile E:\people.xml;

Image : - Excel file of table.

To take backup of Entire database


Go through Comman prompt; Syntax mysql dump u root p Database name > path; Path path for destination file; C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump u root p bioinfo> E:\Mysql\bioinfo_database

Model Test -

Empcontact -

Empdetail

1. Find detail of employ whose name s third letter is e ? Mysql>Select * from empdetail where name like __e%;

2. Find the employee who receive salary more than 3000 and less than 5000 ? Mysql>Select * from empdetail where salary between 3000 and 5000;

3. Find the manager who receive salary higher tha other ? Mysql>Select name from empdetail where desgn = manager order by salary desc limit 1;

4. Detail of Employee who dont have valid contact Number? Mysql> Select * from empcontact where cell is null;

5. Find the city in which more mo. Of employee reciding ? Mysql> Select max(city) from empcontact group by city limit 1;

Mathematical Function
Mod, format, sign, round, sqrt, ceil, floor.

Mysql User Privileges command

-To create user Syntax CREATE USER user name@localhost IDENTIFIED BY password; Mysql> create user Pu@localhost identified by university;

-To view all user Syntax SELECT USER, HOST FROM MYSQL.USER; Mysql> select user, host from mysql.user;

-To Delete user Syntax DELETE FROM MYSQL.USER WHERE USER = user name; Mysql> delete from mysql.user where user = pawan;

-To Access user account Syntax MYSQL U <user name> -p

-To Grant Priveleges to other user Syntax GRANT ALL PRIVILEGES ON *.* TO user name @localhost;

-To Revoke Privileges from other user Syntax REVOKE ALL PROVILEGES ON *.* FROM User name@Local host;

You might also like