MYSQL COMMANDS
1. Opening and logging into mysql Server
$ mysql -u root -p
password:root
2. To see all the Databases in the mysql Server
mysql> show databases;
DDL
Data Definition Language (DDL) statements are used to define the
database structure or schema. Some examples: Create, Alter, Truncate,
Drop and Rename.
3. CREATE
To create a New Database in the mysql Server
mysql> create database databasename;
4. To use the particular Database;
mysql> use databasename;
5. To create a Table
(say department)
in the Database
mysql> create table department(id int, name varchar(255));
6. ALTER
Alter the table (say department) by adding the new column.
ALTER TABLE table_name
ADD column_name datatype;
eg;
mysql> alter table department add age int;
7. TRUNCATE
Truncate removes all rows from a
table. Truncate
the table (say department) by
using following command
mysql> truncate table department;
8. DROP
The DROP command removes a table from the database. Drop the
table (say department) by
following command.
mysql> drop table department;
9. RENAME
It renames the table in database. Rename (say department into
employee) by using following command.
mysql> rename table department to employee;
DML
Data Manipulation Language (DML) statements are used for managing
data within schema objects. Some examples: insert, select, delete and
updates.
10. INSERT
>Insert data into a table. To
add a row in the Table (say in the department table)
mysql> insert into department(id,name)values(1,"admin");
>To add multiple rows in the Table (say
in the department table)
insert into
department(id,name)values(2,"staffroom"),(3,"classroom"),(4,"Lab");
11. SELECT
Retrieves data from the a database. To see the row/tuple is added in
Table (say department table)
mysql> select * from department;
>To
see the complete description of a particular table;
describe table_name;
eg:
mysql> describe department;
>To
see the list of Tables in the Database
mysql> show tables;
> To count the number of rows in the table (say department)
mysql> select count(*) from department;
12. DELETE
>To delete a row from Table. (say department table)
mysql> delete from department where id=3;
>To
see whether the row
is deleted from the Table.
mysql> select * from department;
13. UPDATE
Updates the existing data within a table.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
> To update department name from computers to civil of one tuple
which had id=2;
mysql> update department set name=civil where id=2;
DCL
Data Control Language (DCL) statements. Some examples:
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT command
To Create a new user
To Create a new user
CREATE USER 'jeffrey'@'localhost';
14. GRANT
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
privilege_name is the access right or
privilege granted to the user. Some of the access rights are ALL,
EXECUTE, and SELECT.
object_name is the name of an database
object like TABLE, VIEW, STORED PROC and SEQUENCE.
user_name is the name of the user to
whom an access right is being granted.
PUBLIC is used to grant access rights to
all users.
ROLES are a set of privileges grouped
together.
WITH GRANT OPTION - allows a user to
grant access rights to other users.
Privileges and Roles:
Privileges: Privileges defines the access rights provided to a user
on a database object. There are two types of privileges.
1) System privileges - This allows the user to CREATE, ALTER, or DROP
database objects.
2) Object privileges - This allows the user to EXECUTE, SELECT,
INSERT, UPDATE, or DELETE data from database objects to which the
privileges apply.
Few CREATE system privileges are listed below:
System
Privileges
|
Description
|
CREATE
|
Allows
users to create the specified object in their own schema.
|
CREATE
ANY
|
Allows
users to create the specified object in any schema.
|
The above rules also apply for ALTER and DROP system privileges.
Few of the object privileges are listed below:
Object
Privileges
|
Description
|
INSERT
|
allows
users to insert rows into a table.
|
SELECT |
allows
users to select data from a database object.
|
UPDATE |
allows
user to update data in a table.
|
EXECUTE |
allows user to execute a stored procedure or a function.
|
> In beginnng user is “root”. Create a new user say “aiktc”
using the following command.
mysql> create user 'aiktc'@'localhost';
> From the “root” give grant permission to the new “aiktc”
user for the particular database (say college)
GRANT all
ON college
TO aiktc;
> Log in as aiktc and type show databases command and college
database will be listed there. “All” permission is given by the root user so all the database activity
(DML and DDL) can be performed on the database.
15. REVOKE
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
> If “root” user wants to remove or revoke the permission
given to the “aiktc” user, the following command can be used.
REVOKE all
ON college
TO aiktc;
Good Work!
ReplyDeleteNice job.
ReplyDelete