SQL stands for Structured Query Language
Is a Non-procedural query language
Used for creating, managing, manipulating
and querying the database objects such as
tables, views etc.
All commands in SQL end with a semicolon(;)
SQL commands are divided into following categories:
Data Definition Language
CREATE, ALTER, DROP,TRUNCATE
Data Manipulation Language
INSERT, UPDATE, DELETE
Data Query Language
SELECT
Data Control Language
GRANT, REVOKE
Transaction control Language
COMMIT, ROLLBACK
DDL commands are used to
Create database objects such as tables, indexes, views etc. in
the database
Alter/Modify the structure of existing database objects
Drop the existing database objects from the database
CREATE
ALTER
DROP
TRUNCATE
In other words DDL deals with the structure of the table
and database
To create a database we use the below syntax
Syntax:
CREATE DATABASE databasename;
COMMIT;
Example:
CREATE DATABASE Company;
To drop database we use the below syntax
Syntax:
Drop Database databasename;
Example:
Drop Database Employee;
In order to know the databases that exist we
use the below command
Syntax: SHOW DATABASES;
This will give us the list of all the available
databases
To use one of the databases available we use
the following command
Syntax: USE databasename;
Example: USE COLLEGE
To create a table, you must specify the below:
Table name
Column names
Column data types
Column sizes
Constraints: restrictions on the data values that a
column can store
In order to view or list the tables in a
database we use the below command
SHOW tables;
Series of rules established for naming all
database objects:
1.Must begin with a letter and can not contain blank
spaces or hyphens
2.From 1 to 30 characters
3.Only alphanumeric characters, and special characters
($ , _, #)
Are the following names valid? Why?
1. customer order
2. customer-order
3. #order
Example:
CREATE TABLE emp(
emp_id int(11),
emp_fname char(20) NOT NULL,
emp_lname varchar(20) NOT NULL,
tel_num varchar(10),
DOB DATE,
Gender char(1),
PRIMARY KEY (emp_id));
Data type
Specifies kind of data that the column stores
Enables DBMS to use storage space more
efficiently by internally storing different types of
data in different ways
Basic types
Character ---CHAR,VARCHAR
Numeric----- INT,DOUBLE(M,D),FLOAT(M,D)
Date/time --- DATE,DATETIME
Integrity constraints: define primary and
foreign keys
Value constraints: define specific data values
or data ranges that must be inserted into
columns and whether values must be unique
or not NULL
UNIQUE
CHECK
NULL
Not NULL: specifies that a column cannot be
empty
CHECK : restricts to specific values
UNIQUE: specifies that a non-primary key column
must have a unique value
Example:
CREATE TABLE employee(
empno int(6) PRIMARY KEY,
ename VARCHAR(30) NOT NULL,
grade CHAR(2),
emp_email VARCHAR(40),
DOB DATE, CHECK (empno > 2999),
UNIQUE (emp_email));
DROP TABLE
Syntax:
DROP TABLE tablename
Example:
DROP TABLE employee
Truncate TABLE
Syntax: TRUNCATE Table <tablename> ;
removes all rows from a table
high-speed data deletion statement
You cannot roll back a TRUNCATE statement
Example
TRUNCATE TABLE dept;
DML commands work with the content of the tables
Used to add, delete and modify the contents of the
tables:
Insert
Update
Delete
DML commands are not committed as soon as they
are issued, an explicit commit is required
Auto commit happens under the following conditions:
Issue of a DDL command
Closing the session
Syntax:
INSERT INTO TABLENAME VALUES(valueofcolumn1,valueof
column2,………)
Examples:
INSERT INTO emp
Values(101,’SUNIL’,’CLERK’,’790266666’,’2008-07-14’,’M’);
INSERT INTO emp
Values(102,’SUNIL’,’MORJE’,’7889977777’,’2008-01-
13’,’M’);
INSERT INTO emp (empno,ename,Gender) Values
(102,’RAJESH’,’M’);
Syntax:
SELECT * from tablename;
Example:
SELECT EMP_ID FROM EMPLOYEE WHERE
EMP_ID=1;
SELECT
To select all the columns of a row we use the below.
Syntax:
SELECT * FROM tablename
Example:
SELECT * from EMPLOYEE;
To select a particular column we use the following
Syntax:
SELECT columnname FROM tablename
Example:
SELECT EMP_ID FROM EMPLOYEE.
Alter command is used to change or modify
the structure of the table.
This includes
adding a new column to a table
dropping a column from a table
changing the size of a column
changing the column datatype
adding a constraint
dropping a constraint
renaming columns of a table
Adding a new column
Syntax:
Alter table tablename add column
datatype(size);
Example:
Alter table employee add emp_email
varchar(30) UNIQUE
Adding a Primary Key
Syntax:
Alter table tablename add primary key
(colum name);
Example:
Alter table employee add primary
key(emp_id);
Change the size of a column and the
datatype:
Syntax:
Alter table tablename modify columnname
datatype(size);
Example:
Alter table employee modify gender char(2);
Can only change data type to compatible
data type (i.e. varchar2 to char)
Syntax:
Alter table tablename drop column
Example:
Alter table employee drop emp_name;
Dropping a constraint
Syntax:
Alter table tablename drop primary key;
Example:
Alter table employee drop primary key;
This command will only remove the primary
key constraint , it will not drop the column.
o Syntax:
Alter table tablename change
column old column new column
datatype(size);
Example:
Alter table employee change column
Emp_email email_address varchar(30);
To change the name of a table use RENAME
Syntax
Rename table old_tablename to
new_tablename;
Example
RENAME table employee TO EMP;
Update statement is used to change the value of a
column
Syntax:
Update tablename set col1=new value, col2=new
value where condition;
Example:
UPDATE emp SET ename = 'KUMAR' WHERE empno =
101;
UPDATE emp SET job = 'PRESIDENT', ename = 'SUNIL'
WHERE empno = 101;
Note:I f the where clause is not used , the values in all the
rows will e modified
Syntax:
Delete from tablename;
DELETE FROM emp;
Delete from tablename where condition;
DELETE FROM emp WHERE deptno = 20;
Delete from tablename where condition ;
DELETE FROM emp WHERE empno = 101 AND
ename LIKE 'S%';
ALIAS is renaming default column name while giving the output
Select <Column Name1> <Alias Name1>, <Column Name2>
<Alias Name2> From <Table Name>
Example: List the Deptno & Dname from Department
SELECT deptno "Department Number", dname FROM Dept;
Department Number DNAME
---------------------------------------------
1 FINANCE
20 RESEARCH
30 SALES
40 OPERATIONS
Dname deptno
Finance 1
Allows to remove duplicates from the result set
Syntax:
SELECT DISTINCT columname FROM tablename;.
Example: List the number of jobs in emp table,
displaying jobs only once, even if duplicate values
exist
SELECT DISTINCT job FROM emp;
JOB
---------------
ANALYST
CLERK
MANAGER
PRESIDENT
Emp_no Job Department_Nu
m
1 ANALYST 1
2 CLERK 2
3 MANAGER 3
4 PRESIDENT 4
5 ANALYST 3
6 CLERK 6
Arithmetic
+ , - , * , /
Relational
= , < , > , <= , >=
< >, != , ^=
Boolean/LOGICAL
AND, OR, NOT
Set Operators
UNION, UNION ALL, INTERSECT, MINUS
Others
IN, BETWEEN
LIKE , IS NULL
Example:
List the employees whose hire date is before 28-SEP-81
SELECT empno, ename, hiredate, deptno
FROM emp WHERE hiredate < = ‘2012-12-20';
EMPNO ENAME HIREDATE DEPTNO
---------------------------------------------------
7369 SMITH 17-DEC-80 20
7499 ALLEN 20-FEB-81 30
7521 WARD 22-FEB-81 30
7566 JONES 02-APR-81 20
7654 MARTIN 28-SEP-81 30
7698 BLAKE 01-MAY-81 30
7782 CLARK 09-JUN-81 10
7844 TURNER 08-SEP-81 30
Example: List the employees who get salary in
the range of 1500 and 3000
SELECT empno, ename, sal
FROM emp WHERE sal >= 1500 AND
sal <= 3000;
Example: List the employee number & names of
department 10 & 20
SELECT empno, ename, sal
FROM emp WHERE deptno=10 OR
deptno=20;
SELECT empno, ename, deptno
FROM emp WHERE sal BETWEEN 1500 AND 2500;
SELECT empno, ename, sal
FROM emp WHERE deptno IN(10,20);
SELECT empno, ename
FROM emp WHERE ename LIKE 'S%';
SELECT empno, ename
FROM emp WHERE ename NOT LIKE 'S%';
SELECT empno, ename,comm
FROM emp WHERE comm IS NULL;
SELECT empno,ename,sal FROM emp
ORDER BY ename;
SELECT ename FROM emp
ORDER BY ename DESC;
SELECT job,ename FROM emp
ORDER BY job,ename;
SELECT job,ename FROM emp
ORDER BY job, ename DESC;
SUM: Returns the sum of the column values provided
SELECT SUM(sal) "Total Sal" FROM emp WHERE deptno = 20;
AVG(n): Returns average value of n
SELECT AVG(Sal) "Average" FROM Emp ;
COUNT: Returns the number of rows for the specified column
count(*) – Slow SELECT count(*) "Total_row" FROM Emp;
count(empno) – Fast SELECT count(ename) "Total_row" FROM Emp;
MIN: Returns the minimum value of an expression
SELECT MIN(Sal) "Minimum" FROM emp;
MAX: Returns the maximum value of an expression
SELECT MAX(Sal) "Maximum" FROM emp;
Note: Aggregate functions ignore NULL values by default
UNION
UNION ALL
INTERSECT
MINUS
Each of these combines the results of two
SELECT statements into a single result
UNION:
SELECT ename, job FROM emp1
UNION
SELECT ename, job FROM emp2;
UNION ALL: Gets all duplicates
SELECT ename, job FROM emp1
UNION ALL
SELECT ename, job FROM emp2;
Data types should be same of both the tables
Returns those rows which are retrieved by
both the queries
E.g. List the employees who are listed in
both Employee & Employee2
SELECT * FROM Employee
INTERSECT
SELECT * FROM Employee2;
The structure of Employee & Employee2
table must be exactly the same
Returns all rows retrieved by the first query,
but not by the second query
E.g. List the Employees who are only listed
in Employee and not in Employee2
SELECT * FROM Employee
MINUS
SELECT * FROM Employee2
CONCAT (col1, col2)
SELECT CONCAT (ename,job) FROM emp;
UPPER, LOWER
SELECT UPPER(ename), LOWER(ename),
FROM emp;
TRIM
SELECT TRIM(ename) FROM emp;
LTRIM, RTRIM
SELECT LTRIM(ename), RTRIM(ename) FROM emp;
LENGTH (STRING)
SELECT LENGTH (ename) FROM emp;
ROUND (m,n): Rounds values to the specified decimal
SELECT ROUND(salary) FROM employee;
TRUNC (m,n): Truncates values to the specified decimal
SELECT TRUNC(salary,1) FROM employee
PI()
Cos(x)
Tan(x);
Sin(x);
Sqrt(x);
DNum Dept_Name
1 FINANCE
2 ADMIN
EMP_ID SAL Dept_No
1 40000 1
2 55000 2
3 60000 1
TABLE: EMPLOYEE TABLE: DEPARTMENT
PK:EMP_ID PK: DNum
Foreign key
CREATE TABLE Department
(Dnum int(10) PRIMARY KEY,
Dept_Name varchar(30));
CREATE TABLE Employee
(EMP_ID int(10) PRIMARY KEY,
sal int(20),
Dept_no int(10),
FOREIGN KEY (Dept_no) REFERENCES
Department (Dnum));
The foreign key must refer to the primary key of the referencing table
While inserting data into the table employee the FK value has to be
present in the department table.
Automatically increments the values in the
table while inserting a new record.
Do not need to insert this value into the
table
Used only with primary keys.
create table marks
(rollno int Auto_increment,
name varchar(30),
sub1 int,
primary key(rolln));
insert into marks //Not the correct way.
values(1,'liz',39);
insert into marks( //The correct way.
name,sub1)
values('liz',39);
List the total salaries of the employees
Select sum(sal) from employee;
EMP_ID SAL Dept_No
1 40000 1
2 5000 2
3 60000 1
4 5000 2
SAL
110000
Syntax:
SELECT column1, column2,…. column_n,
aggregate_function (expression)
FROM tables
GROUP BY column1, column2, ... column_n;
GROUP BY in a SELECT statement collects data
from across multiple records & groups the
results by one or more columns
Example1:
List the sum of the salaries of each department.
Select sum(sal),dept_no
from employee
GROUP BY dept_no;
Can I select emp_id in the output?
EMP_ID SAL Dept_No Joining_date
1 40000 1 13/07/2013
2 55000 2 12/05/2013
3 60000 1 13/07/2013
Example2:
List the max salaries of the employees who
joined the organization on the same day.
Select max(sal),joining_date
from employee
GROUP BY joining_date;
Can I select dept_no in the output?
EMP_ID SAL Dept_No Joining_date
1 40000 1 13/07/2013
2 55000 2 12/05/2013
3 60000 2 13/07/2013
Syntax:
SELECT column1, column2,…. column_n,
aggregate_function (expression)
FROM tables
GROUP BY column1, column2, ... column_n
HAVING condition1 ... condition_n;
HAVING is very similar to WHERE, except that
the statements within it are of an aggregate
nature
Example1:
List the department no’s and sum of the salaries
of the departments which have sum greater than
100000 .
SELECT sum(sal),dept_no
FROM employee
GROUP BY dept_no
HAVING sum(sal) >= 100000;
EMP_ID SAL Dept_No Joining_date
1 40000 1 13/07/2013
2 55000 2 12/05/2013
3 60000 1 13/07/2013
Example2:
List the max salaries of the employees who
joined the organization on the same day and the
joining date is greater than 15/06/2013.
Select max(sal),joining_date
from employee
GROUP BY joining_date
HAVING joining_date>’2013-06-15’;
EMP_ID SAL Dept_No Joining_date
1 40000 1 13/07/2013
2 55000 2 12/05/2013
3 60000 1 13/07/2013
Used when we require data from more
than one tables
The columns compared by the ‘=‘
operator are called join columns and the
join operation is called an EQUI JOIN
Employee Department
List the employee ID’s of all the employees who belong to the
FINANCE department.
Select dept_no from department where
dept_name=‘FINANCE’;
Dept_no
1
select emp_id from employee where dept_no=1;
EMP_ID SAL Dept_No
1 40000 1
2 55000 2
3 60000 1
4 59999 3
DNum Dept_Name
1 FINANCE
2 ADMIN
In case the column name are same across tables.
Employee Department
SELECT * FROM employee,dept
WHERE employee.dept_no=dept.dept_no
SELECT * FROM employe e,dept d
WHERE e.dept_no=d.dept_no;
EMP_ID SAL Dept_No
1 40000 1
2 55000 2
3 60000 1
4 59999 3
Dept_No Dept_Name
1 FINANCE
2 ADMIN
To select specific records using a join
SELECT * FROM employee e,dept d
WHERE e.dept_no=d.dept_no
and d.dept_no=1;
A form of an SQL statement that appears
inside another SQL statement, also termed as
Nested Query
The statement containing a sub-query is
called a parent statement
The parent statement uses the rows returned
by the sub-query
Single row
Multiple row
Multiple column
USING ALL and ANY
Employee Department
List the employee ID’s of all the employee who
belong to the FINANCE department.
EMP_ID SAL Dept_No
1 40000 1
2 55000 2
3 60000 1
4 59999 3
Dept_No Dept_Name
1 FINANCE
2 ADMIN
Method1: 2 QUERIES
Select dept_no from department where
dept_name=‘FINANCE’;
Dept_no
1
select emp_id from employee where dept_no=1;
Method 2: JOIN
Select emp_id from
employee e, department d
where e.deptno=d.deptno
and d.dept_name=‘FINANCE’;
Returning a single row
Select emp_id from employee where dept_no=
(select dept_no from department where dept_name
=‘FINANCE’);
The nested query must return only one column.
Returning multiple rows
List the employee ID’s of all the employee who
belong to the FINANCE or ADMIN department.
Select emp_id from employee where dept_no IN
(select dept_no from department where
dept_name =‘FINANCE’ OR dept_name=‘ADMIN’);
List the employee ID’s of all the employee who
belong to the FINANCE or ADMIN department
and employee id less than 2.
Select emp_id from employee where dept_no IN
(select dept_no from department where
dept_name =‘FINANCE’ OR dept_name=‘ADMIN’)
AND emp_id<2;
ANY: The condition becomes true, if there
exists at least one row selected by the sub-
query for which the comparison holds
Example:
List all the employees from department no
10 whose salaries are greater than any
employees from department no 30
SELECT * FROM emp WHERE sal >=
ANY(SELECT sal FROM emp WHERE
deptno = 30)AND deptno = 10;
All: The condition becomes true, if all the
rows are selected by the sub-query for which
the comparison holds
Example: List the employee whose salary is
greater than all the salaries of employees
from department no 30.
SELECT * FROM Emp WHERE sal >=
ALL(SELECT sal FROM emp WHERE
deptno = 30)AND deptno <> 30;
Returning more than one column
When we want to compare more than one
columns in a sub-query
List the employee names who have max
salary within each department.
e.g. SELECT ename, job FROM emp
WHERE (deptno, sal) IN
(SELECT deptno, max(sal)
FROM emp GROUP BY deptno);
EXISTS: This condition is considered "to be met" if the sub-
query returns at least one row
List the department nums and the department name if
there is atleast one employee in the employee table
Examples:
SELECT deptno, dname FROM dept
WHERE EXISTS (select * from employee);
EMP_ID SAL Dept_No
1 40000 1
2 55000 2
3 60000 1
4 59999 3
Dept_No Dept_Name
1 FINANCE
2 ADMIN
A view is an object that contains no data of
its own
It is a kind of table whose contents are taken
from other tables through the execution of a
query
As the values in the base tables change, the
changes are automatically reflected in the
views
CREATE VIEW viewname
AS select query;
CREATE VIEW test
AS select * from employee;
DROP VIEW viewname;
CREATE VIEW sum_sal AS
select sum(sal),deptNO from employee group
by deptNO;
select * from sum_sal;
+----------+--------+
| sum(sal) | deptNO |
+----------+--------+
| 10000.00 | 1 |
| 3000.00 | 2 |
+----------+--------+
ACID Properties:
Atomicity: ensures that all operations within the work unit
are completed successfully; otherwise, the transaction is
aborted at the point of failure, and previous operations are
rolled back to their former state.
Consistency: ensures that the database properly changes
states upon a successfully committed transaction.
Isolation: enables transactions to operate independently
of and transparent to each other.
Durability: ensures that the result or effect of a
committed transaction persists in case of a system failure.
START TRANSACTION: Begins the transaction
COMMIT: To explicitly save the changes in the
database. Execution of DDL statements does
commit the changes automatically.
ROLLBACK: To undo the changes till the last
commit or till the last savepoint.
SAVEPOINT: Is a transaction marker.
LOCKS: To LOCK tables while transactions are in
progress
START TRANSACTION;
Insert into dept values(6,’RM’);
Insert into dept values(7,’RM’);
Insert into dept values(8,’RM’);
COMMIT;
START TRANSACTION;
Insert into dept values(6,’RM’);
Insert into dept values(7,’RM’);
Insert into dept values(8,’RM’);
ROLLBACK;
START TRANSACTION;
Insert into dept values(6,’RM’);
Insert into dept values(7,’RM’);
Insert into dept values(8,’RM’);
SAVEPOINT A;
Insert into dept values(9,’RM’);
Insert into dept values(10,’RM’);
Insert into dept values(11,’RM’);
ROLLBACK TO SAVEPOINT A;
START TRANSACTION;
Insert into dept values(6,’RM’);
Insert into dept values(7,’RM’);
Insert into dept values(8,’RM’);
SAVEPOINT A;
Insert into dept values(9,’RM’);
Insert into dept values(10,’RM’);
SAVEPOINT B;
Insert into dept values(11,’RM’);
ROLLBACK TO SAVEPOINT B;
Used to control the use of a table by more
than one users.
READ LOCK
Implies that the thread setting the lock can read
data from that table , as can other threads.
No threads can modify the locked table.
LOCK TABLE dept read;
WRITE LOCK
Implies that the thread setting the lock can
modify the data in the table.
No other thread can either read or write to the
table
LOCK TABLE dept WRITE;
A set of SQL statements that is invoked
automatically when a change is made to the
data on the associated table.
Can be defined to be invoked either before
or after the data is changed by INSERT,
UPDATE or DELETE statements.
Events when the trigger can occur.
BEFORE INSERT – activated before data is inserted into the
table.
AFTER INSERT- activated after data is inserted into the table.
BEFORE UPDATE – activated before data in the table is
updated.
AFTER UPDATE - activated after data in the table is updated.
BEFORE DELETE – activated before data is removed from the
table.
AFTER DELETE – activated after data is removed from the
table.
CREATE TRIGGER trigger_name
triggertime triggerevent
ON table_name
FOR EACH ROW
BEGIN
...
END
The trigger name should follow the naming
convention [trigger time]_[trigger event]_[table
name] for example before_update_employees_.
To change the delimiter
Delimiter //
Create a trigger to insert a record into the
employee table(emp_id,dept_no) whenever a new
record is added to the dept table
create trigger after_insert_dept
after insert on dept
for each row begin
insert into employee(emp_id,deptNo)
values(10,4);
end//
create trigger after_insert_dept
after insert on dept
for each row begin
insert into employee(emp_id,deptNo) values(10,4);
end//
Insert into dept values (4,’FMG’);
Insert into dept values (5,’DEV’);
Insert into dept values (6,’ISG’);
Insert into employee values(14,’Vijay’);
Dept_no Dept_na
me
1 HR
2 ADMIN
3 RMG
EmpID Emp_name DeptNo Sal
1 Prachi 2 3000
4 FMG
10 NULL 4 NULL
10 NULL 4 NULL
10 NULL 4 NULL
5 DEV
6 ISG
Create a trigger to insert a new record into
the employee table( emp_id and dept_no )
when a new dept is added to the dept
table.
create trigger after_insert_dept
after insert on dept
for each row begin
insert into employee(emp_id,deptNo)
values(14,new.dept_no);
end//
create trigger Test1
after insert on dept
for each row begin
insert into employee(emp_id,deptNo)
values(14,new.dept_no);
end//
Insert into dept values (4,’FMG’); new = 4, FMG
Insert into dept values (5,’DEV’); new =5, DEV
Insert into dept values (6,’ISG’); new =6, ISG
Insert into employee values(14,’Vijay’); new =14,Vijay
Dept_no Dept_na
me
1 HR
2 ADMIN
3 RMG
EmpID Emp_name DeptNo Sal
1 Prachi 2 3000
4 FMG
14 NULL 4 NULL
14 NULL 5 NULL
14 NULL 6 NULL
5 DEV
6 ISG
Every Time a record is updated in the Dept
table store the new dept name,new dept id
,old name and old dept id in a table named
Old_Dept
create table Old_Dept
(old_name varchar(30),
Old_dept_no int,
New_dept_name varchar(30),
New_dept_no int);
create trigger after_Update_Dept
after update on dept
for each row begin
insert into Old_dept values
(OLD.dept_name,OLD.dept_no,new.dept_name,new.dept_no);
end//
Insert into dept values (4,’FMG’);
Update dept set dept_name=‘ASG’ where dept_no=2;
Update dept set dept_name=‘DEV’,dept_no=5 where dept_no=2;
Dept_no Dept_na
me
1 HR
2 ADMIN
3 RMG
New_dept
_name
New_dept_
num
Old_dept_
name
Old_dept_no
4 FMG
Dept_no Dept_na
me
1 HR
2 ASG
3 RMG
ASG 2 ADMIN 2
DEV 5 ASG 2
4 FMG
Dept_no Dept_na
me
1 HR
5 DEV
3 RMG
4 FMG
create procedure totalsal(out total int)
begin
select count(sal) from employee;
end
call totalsal(@total);
Security is the prevention of unauthorized access
to the database
Within an organization, the database
administrator determines the types of access
various users need for the database
Some users might be able to retrieve and update
data in the database
Others might be able to retrieve any data from
the database but can not make any changes to it
Still other users might be able to access only a
portion of the database
System Privileges
Control the operations that the user can perform
within the database
Create user accounts
Connecting to the database, creating new tables,
shutting down the database, etc.
Object Privileges
Granted on individual database objects
Controls operations that a user can perform on a
specific object (insert data, delete data, etc.)
When we create an object in user schema, we
can then grant object privileges on that object to
other database users
Roles are named groups of related privileges
that are granted to users or other roles
A role can be granted system or schema
object privileges
Any role can be granted to any database
user.
Each role granted to a user is, at a given
time, either enabled or disabled.
The connection stage:
When the user requests a connection for a
specific host ,MySQL will first check whether
an entry exists for the user in the table
The Request Stage:
The operations performed by the users are
compared with the privileges in the table.
GRANT privilege is used to grant access for a
user in the MySQL grant tables.
GRANT privilege
ON database-name.table-name
TO User@domain
IDENTIFIED BY password;
GRANT select,update
ON lisa.employee
TO logger@localhost
IDENTIFIED BY ‘timber’;
REVOKE privilege is used to take away access
for a user in the MySQL grant tables.
REVOKE privilege
ON database-name.table-name
from User@domain
IDENTIFIED BY password;
REVOKE select,update
ON lisa.employee
from logger@localhost
IDENTIFIED BY ‘timber’;
CREATE USER
User name
[IDENTIFIED BY [PASSWORD] 'password']
To enable the user to connect with no
password (which is insecure), do not include
IDENTIFIED BY clause:
CREATE USER 'jeffrey'@'localhost';
To assign a password, use IDENTIFIED BY with
the literal plaintext password value:
CREATE USER 'jeffrey'@'localhost' IDENTIFIED
BY 'mypass';
To avoid specifying the plaintext password if you
know its hash value (the value that PASSWORD()
would return for the password), specify the hash
value preceded by the keyword PASSWORD:
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY
PASSWORD
'*90E462C37378CED12064BB3388827D2BA3A9B689
';
We can grant a privilege to all users by
assigning it to PUBLIC.
Eg.
GRANT SELECT(name,address) ON lisa.emp
TO PUBLIC;
109
C:Userslisa>mysql -u jeffrey
mysql> show databases;
C:Userslisa>mysql -u jeffery1 –p
Enter password: *******