DML (Data Manipulation Language) Statements The commands in MySql to insert/update/delete the records are DML Statements. Insert into, Upd...
DML (Data Manipulation Language) Statements
- CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;
- FLUSH PRIVILEGES;
- SHOW GRANTS FOR 'sammy'@'localhost';
- REVOKE type_of_permission ON database_name.table_name FROM 'username'@'host';
- DROP USER 'sammy'@'localhost';
Constraints in Mysql
Querying data from tables
Query all data from a table:
SELECT * FROM table_name;
Code language: SQL (Structured Query Language) (sql)
Example:
- select * from student;
- select * from student where marks>80;
- select * from student where name='raman';
- select * from student where name in ('Rohit','Shaik');
- select * from student where name not in ('Rohit','Shaik');
- select * from student where name like 'S%';
- select * from student where name like '%t';
- select * from student where name like '_____'; -- 5 underscore
- select * from student where name!='Rohit';
- select distinct Grade from student;
- select * from student where marks between 80 and 85;
- select min(Marks) as Min from student where Grade='A';
- select count(*) as Max from student where Grade='A';
- select count(distinct grade) from student;
- select max(Marks) as Max from student where Grade='A';
- select sum(Marks) as Total from student where Grade='A';
- select avg (Marks) as Average from student where Grade='A';
- select name, marks + 10 as Marks from student;
- insert into student (id,name) values (6,'Shyam'); -- inserts only id and name column values
- insert into student values (6,'Shyam',90,'A'); -- inserts a complete row.
- update student set marks=85 where id=6;
- delete from student where grade is null;
SubQuery ( nested query)
select * from student where marks > (select marks from student where name='Rohit');
select * from student where marks < (select marks from student where name='Rohit');
select * from student where id in (select id from sports where sportsname='Hockey');
Joins
Inner join
It displays all the matching records between join tables.
Example:- To find out all the students names who are participating in sports.
select name,sportsname from student s join sports sp on s.id=sp.id;
Example:- To findout all the student names who are participating in Hockey sports.
select name,sports name from student s join sports sp on s.id=sp.id where sportsname='Hockey';
Left Outer Join
It will display all the records of left table and matching records from the right table.
Example: To display all the names of students along with the names of their sports. All the names should be displayed even though they are not participating in any sports.
select name,sportsname from student s left join sports sp on s.id=sp.id;
Right Outer join
It will display all the records of right-hand side table and the matching records from Left-hand side table.
Cross Join
It displays the cartesian product of join tables
select * from student, sports;
Group By Clause
It groups on the basis of a group column and It always used with aggregate funciton(sum,count,avg,min,max)
Example: - To find the count of student grade wise.
select Grade,count(*) from student group by Grade;
Example:- To find the count of students grade-wise but for those students whose count is more than 1
select Grade,count(*) from student group by Grade Having count(*)>1;
Query data from one or more column of a table:
SELECT
column1, column2, ...
FROM
table_name;
Code language: SQL (Structured Query Language) (sql)
Remove duplicate rows from the result of a query:
SELECT
DISTINCT (column)
FROM
table_name;
Code language: SQL (Structured Query Language) (sql)
Query data with a filter using a WHERE
clause:
SELECT select_list
FROM table_name
WHERE condition;
Code language: SQL (Structured Query Language) (sql)
Change the output of the column name using column alias:
SELECT
column1 AS alias_name,
expression AS alias,
...
FROM
table_name;
Code language: SQL (Structured Query Language) (sql)
Query data from multiple tables using inner join:
SELECT select_list
FROM table1
INNER JOIN table2 ON condition;
Code language: SQL (Structured Query Language) (sql)
Query data from multiple tables using left join:
SELECT select_list
FROM table1
LEFT JOIN table2 ON condition;
Code language: SQL (Structured Query Language) (sql)
Query data from multiple tables using right join:
SELECT select_list
FROM table1
RIGHT JOIN table2 ON condition;
Code language: SQL (Structured Query Language) (sql)
Make a Cartesian product of rows:
SELECT select_list
FROM table1
CROSS JOIN table2;
Code language: SQL (Structured Query Language) (sql)
Counting rows in a table.
SELECT COUNT(*)
FROM table_name;
Code language: SQL (Structured Query Language) (sql)
Sorting a result set:
SELECT
select_list
FROM
table_name
ORDER BY
column1 ASC [DESC],
column2 ASC [DESC];
Code language: SQL (Structured Query Language) (sql)
Group rows using the GROUP BY
clause.
SELECT select_list
FROM table_name
GROUP BY column_1, column_2, ...;
Code language: SQL (Structured Query Language) (sql)
Filter group using the HAVING
clause:
SELECT select_list
FROM table_name
GROUP BY column1
HAVING condition;
Code language: SQL (Structured Query Language) (sql)
Modifying data in tables
Insert a new row into a table:
INSERT INTO table_name(column_list)
VALUES(value_list);
Code language: SQL (Structured Query Language) (sql)
Insert multiple rows into a table:
INSERT INTO table_name(column_list)
VALUES(value_list1),
(value_list2),
(value_list3),
...;
Code language: SQL (Structured Query Language) (sql)
Update all rows in a table:
UPDATE table_name
SET column1 = value1,
...;
Code language: SQL (Structured Query Language) (sql)
Update data for a set of rows specified by a condition in WHERE
clause.
UPDATE table_name
SET column_1 = value_1,
...
WHERE conditionDelete all rows in a table
DELETE FROM table_name;
Code language: SQL (Structured Query Language) (sql)
Delete rows specified by a condition:
DELETE FROM table_name
WHERE condition;
Searching
Search for data using the LIKE
operator:
SELECT select_list
FROM table_name
WHERE column LIKE '%pattern%';
COMMENTS