0% found this document useful (0 votes)
6 views4 pages

SQL Setup

Uploaded by

vishnucheppanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

SQL Setup

Uploaded by

vishnucheppanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Setup

Reference:

User management
Create a new user
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Show users

SELECT user, host, plugin FROM mysql.user;

Grant privileges
GRANT ALL PRIVILEGES ON database_name.table_name TO 'username'@'localhost';

#check_out_later to grant specific privileges

Update password
ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

Revoke privileges
REVOKE privileges ON database_name.table_name FROM 'username'@'localhost';

Flush privileges
After major changes in user management, it advisable to flush the privileges to let the changes make
effect. When we flush privileges it reload the grant tables in the database.

FLUSH PRIVILEGES;

Logging in as a user
Ubuntu uses Unix_Auth authentication for root user and cashing_sha2_password for other users by

default.
So root user can login using sudo command and system password like below.

sudo mysql -u root

Other users has to use separate password for logging in.

sudo mysql -u vishnu -p

Delete a user
DROP USER 'username'@'hostname';

Database management
Database creation
CREATE DATABASE database_name;

Show database
SHOW DATABASES;

Activate database
USE databse_name;

Delete Database
DROP DATABASE database_name;

Table management
Create table
CREATE TABLE table_name{
column1 datatype,
column2 datatype,
...
};

Create table as

CREATE TABLE new_table_name AS


SELECT column1, column2 ...
FROM existing_table_name
WHERE condition;

Alter table
ALTER TABLE table_name
ADD column_name datatype;

Show tables
SHOW TABLES;

Delete table
DROP TABLE table_name;

Insert Data
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2 ...);

Select data
SELECT * FROM table_name;

Update data
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

Delete data
DELETE FROM table_name WHERE condition;

Global temp
Global temporary tables are visible to all sessions. They will be dropped when all sessions referring those
ends.

Creation

CREATE GLOBAL TEMPORARY TABLE temp_global (


id INT,
name VARCHAR(50)
);

Usage

-- session 1
INSERT INTO temp_gloabl (id, name) VALUES (, 'Alice');

-- session 2
SELECT * FROM temp_global;

#check_out_later What are sessions? Why the tables has to be temporary?


Dropping

DROP TABLE temp_global;

Local temp
Local temporary tables are only visible to the current session. Those are dropped when the current session
ends.

Creation

CREATE TEMPORARY TABLE temp_local (


id INT,
name VARCHAR(50)
);

Usage

-- Session 1
INSERT INTO temp_local (id, name) VALUES (1, 'Bob');

-- Session 2
SELECT * FROM temp_local;

Dropping
No need for manual dropping of the local temp tables. They are automatically dropped when the session
ends.

SQL View

You might also like