Create Database: Mysql USE Bioinfo Database Changed
Create Database: Mysql USE Bioinfo Database Changed
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 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
SELECT STATEMENT
Syntax -SELECT * FROM table name ; Mysql>Select * from professor;
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;
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;
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;
String Operation
% - matches any substring. _ - underscore matches any character. Syntax- SELECT * FROM table name WHERE attribute name LIKE string;
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);
Join
Used to join two table. SELECT [Attribute list]/* FROM table name1 [LEFT/RIGHT JOIN ] table name2 ON condition; Examlpe Table -1 People
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;
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.
-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 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;