Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Mysql

Entering password:
On the running machine: If you are logging in on the same machine that MySQL is running on,
you can omit the host, and simply use the following:
mysql -u root -p
On the host server: host and user represent the host name where your MySQL server is running
and the user name of your MySQL account.
mysql -h host -u user –p

 If you want to supply your password on the command line after the -p option, you must
do so with no intervening space (for example, as -ppassword, not as -p password).

Basic queries
Show the version and the date of mysql> SELECT VERSION(), CURRENT_DATE;

To show all the databases:


mysql> SHOW DATABASES;

 The test database often is available as a workspace for users to try things out.
 SHOW DATABASES does not show databases that you have no privileges for if you do
not have the SHOW DATABASES privilege.
Creating database
mysql> CREATE DATABASE database name;

 database names are case-sensitive (unlike SQL keywords), so you must always refer to
your database as menagerie, not as Menagerie, MENAGERIE, or some other variant. This
is also true for table names. (Under Windows, this restriction does not apply, although
you must refer to databases and tables using the same lettercase throughout a given
query.
Making the current database:
USE menagerie
 Your database needs to be created only once, but you must select it for use each time you begin
a mysql session. You can do this by issuing a USE statement as shown in the example.
Alternatively, you can select the database on the command line when you invoke mysql. Just
specify its name after any connection parameters that you might need to provide.
mysql -h host -u user -p database name

To view which database is you are currently working on: SELECT DATABASE();

Data types
INT -----------------------------------whole number
DECIMAL(M,N)-----------------------------M is total number of digit N- digit after decimal
VARCHAR(L)---------------------string of length l
BLOB------------------------------------binary large objects, store large data for images
DATE------------------------------------“yyyy-mm-dd”
TIMESTAMP---------------------------“yyyy-mm-dd hh:mm:ss”

Creating a table
To show table: mysql> SHOW TABLES;

To create table: CREATE TABLE identifier(

Student_id INT PRIMARY KEY, the primary key indicate the attribute is the primary key
name VARCHAR(20),

Major VARCHAR(20) the last attribute does not end with comma

PRIMARY KEY(STUDENT_ID) ######### IS ANOTHER WAY OF INDICATING THE PRIMARY KEY

);

######## commands must end by semicolon;;;;;

#### CREATING SUPER KEY

PRIMARY KEY(student_id, name),

To describe the attributes of the table: DESCRIBE student;

To delete table: DROP TABLE student;

To delete specific column: ALTER TABLE student DROP COLUMN gpa;

To add attribute after creation of table: ALTER TABLE student ADD gpa DECIMAL(3,2);

ALTER TABLE (table name) ADD (new attribute name) (datatype);

INSERTING DATA INTO TABLE


Inserting data:

INSERT INTO student VALUES(1,”SEMIR”, “software engineering”);

If you don’t know one value: INSERT INTO student( student_id , name) VALUES(1,"semir”);

 The missing value will be NULL

To view table:

SELECT * FROM student;

SETTING ATTRIBUTE CONSTRAINTS


CREATE TABLE identifier(

Student_id INT PRIMARY KEY,

Student_id INT AUTO_INCREMENT, #### the data inserted will be automatically incremented, but first
needed to be initiated
name VARCHAR(20) NOT NULL ,#####THE COLUMN CAN NOT BE NULL

Major VARCHAR(20) UNIQUE ###cannot have repeated value

Major VARCHAR(20) DEFAULT ‘undecided’ ##### if no data is entered the default will be undecided

);

COMMENT: --two dash then write your comment

UPDATING AND DELETING ROW


To update mass values:

UPDATE student

SET major = “bio” ### SET name= “abebe” , major = “undecided”

WHERE major = “biology”; ###where student_id =4;

### WHERE major =”bio” OR major = “chemistry”;

To delete row:

DELETE FROM student

WHERE student_id = 5; ###WHERE name = “abebe” AND major = “biology”

BASIC QUERIES
SELECTING:
SELECT * FROM student; ###* IS TO MEAN ALL

SELECT name FROM student; ### select only name column

SELECT name , major FROM student; ### select only name and major column

SELECT student.name , student.major FROM student; ###to be more specific

SELECT student.name FROM student LIMIT 2; ###show only 2 rows

SELECT * FROM student WHERE major=”chemistry”; ### select students with major chemistry

SELECT * FROM student WHERE student_id <> 2; ##<> not equal symbol

WHERE name IN (“abebe”,”molla”,”chala”); ##select students whose name is in parenthesis

ORDERING OR SORTING

SELECT student,name

FROM student

ORDER BY name; ### order in sort of name attribute

ORDER BY name DESC; ### in descending order

ORDER BY name ASC; ### in ascending order

ORDER BY name, student_id DESC; ### order by name if same value occur then by student id

FOREIGN KEY
Can be defined only after all the tables are created

FOREIGN KEY (mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL

UPDATING A FOREIGN KEY AFTER CREATION OF TABEL

CREATE TABLE client(

    client_id INT PRIMARY KEY,

    client_name VARCHAR(40),

    branch_id INT, ########NEEDS TO BE DEFINED FIRST

    FOREIGN KEY(branch_id) REFERENCES branch(branch_id) ON DELETE SET NULL

);
COMPLEX QUERIES
Changing the attribute name and selecting:

SELECT first_name AS forename, last_name AS surname FROM employee;


To find all types of different values on the column:

SELECT DISTINCT sex FROM employee

FUNCTIONS

COUNT ----- to count the given argument:

SELECT COUNT(emp_id)

FROM employee

WHERE sex =”F” AND birth_date > “1970-01-01”; #### NULL won’t be counted

Aggregate of count

SELECT COUNT(sex) , sex ### the column with red color will be visible

FROM employee

GROUP BY sex;####### show result by dividing into separate rows

AVG-------calculate the average:

SELECT AVG(emp_id)

FROM employee;

SUM------calculate the sum:

SELECT SUM(emp_id)

FROM employee;

WILDCARD
%----any number of character

_ one character

LIKE----LINKE THIS PATTERN

SELECT *

FROM client

WHERE client_name LIKE "%LLC";

UNION
SELECT first_name  #####we cannot add additional column here like SELECT first-name, lastname only
but we can if we add additional in client

FROM employee

UNION

SELECT branch_name

FROM branch

UNION

SELECT client_name

From client

#### first name and branch name must have same data type

JOINS
SELECT employee.emp_id, employee.first_name, branch.branch_name ###these will be visible

FROM employee

JOIN branch

ON employee.emp_id = branch.mgr_id; ### where they are true

LEFT JOIN branch ### all wil be listed which don’t share same value but with NULL value from left
table

right JOIN branch ### all wil be listed which don’t share same value but with NULL value from right or
on table
NESTED QUERIES
SELECT employee.first_name , employee.last_name

FROM employee

WHERE employee.emp_id IN (

SELECT works_with.emp_id

FROM works_with

WHERE works_with.total_sales > 30000);

You might also like