0% found this document useful (0 votes)
9 views11 pages

MySQL 1

The document provides a comprehensive guide on various MySQL commands, including setting passwords, creating and managing databases and tables, and performing mathematical and string operations. It covers commands for displaying data, inserting values, and manipulating string formats, as well as grouping and ordering results. Additionally, it includes examples of SQL queries to demonstrate the functionality of these commands.

Uploaded by

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

MySQL 1

The document provides a comprehensive guide on various MySQL commands, including setting passwords, creating and managing databases and tables, and performing mathematical and string operations. It covers commands for displaying data, inserting values, and manipulating string formats, as well as grouping and ordering results. Additionally, it includes examples of SQL queries to demonstrate the functionality of these commands.

Uploaded by

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

SET PASSWORD

mysql> set password for root@localhost='';

RETRIEVE THE PORT NUMBER THAT THE MySQL SERVER CONFIGURED TO USE
mysql> show global variables like 'PORT';

SHOWS DATABASE
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+

CREATE DATABASE
mysql> create database FIRSTDATABASE;

USE DATABASE
mysql> use <databaseName>

ABSOLUTE VALUE
mysql> SELECT ABS(123);
+----------+
| ABS(123) |
+----------+
| 123 |
+----------+

REMAINDER/MODULE
mysql> select mod(10,4);
+-----------+
| mod(10,4) |
+-----------+
| 2 |
+-----------+

mysql> select MOD(10, 4) as remainder;


+-----------+
| remainder |
+-----------+
| 2 |
+-----------+

POWER/EXPONENT
mysql> select power(4, 2) as result;
+--------+
| result |
+--------+
| 16 |
+--------+

SELECT THE GREATEST NUMBER


mysql> select greatest(2,23,435,343,34);
+---------------------------+
| greatest(2,23,435,343,34) |
+---------------------------+
| 435 |
+---------------------------+

SELECT THE LEAST NUMBER


mysql> select least(1,2,4,53,45,4,4);
+------------------------+
| least(1,2,4,53,45,4,4) |
+------------------------+
| 1 |
+------------------------+

TRUNCATE/SHORTEN/REDUCE
mysql> select TRUNCATE(22.234242,2);
+-----------------------+
| TRUNCATE(22.234242,2) |
+-----------------------+
| 22.23 |
+-----------------------+

SQUARE ROOT
mysql> SELECT SQRT(25) AS RESULT;
+--------+
| RESULT |
+--------+
| 5 |
+--------+

ROUND OFF (WHOLE NUMBER)


mysql> SELECT ROUND(22.987);
+---------------+
| ROUND(22.987) |
+---------------+
| 23 |
+---------------+

ROUND OFF (DECIMAL)


mysql> SELECT ROUND(22.987,2);
+-----------------+
| ROUND(22.987,2) |
+-----------------+
| 22.99 |
+-----------------+

CREATE TABLE
mysql> create table students(stud_id INT PRIMARY KEY, stud_name varchar(255), age
INT, gender char(1), dob DATE, city varchar(255));

INSERT VALUE IN TABLE


mysql> INSERT INTO <tableName> VALUES(10000, "nnnn", 19, "M", "2005-4-15",
"BAGUIO");

mysql> INSERT INTO <tableName>(STUD_ID, STUD_NAME, AGE, GENDER, DOB, CITY)


VALUES(10001, "asd", 19, "M", "2005-4-15", "BAGUIO");

SHOW TABLES
mysql> SHOW TABLES;
+-------------------------+
| Tables_in_firstdatabase |
+-------------------------+
| students |
+-------------------------+

DISPLAY THE TABLE


mysql> select * from <tableName>;
+---------+------------------+------+--------+------------+--------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+--------+
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
+---------+------------------+------+--------+------------+--------+

DESCRIBE/DISPLAY THE DETAILS OF THE TABLE


mysql> describe <tableName>;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| stud_id | int | NO | PRI | NULL | |
| stud_name | varchar(255) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| dob | date | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+

DESCRIBE/DISPLAY THE DETAILS SPECIFIC FIELD


mysql> describe <tableName> id;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| stud_id | int | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+

mysql> insert into students values(102, "Jose", 22, "M", "2016-11-23", "La
Trinidad");

mysql> insert into students values(103, "Jose", 45, "M", "2016-11-23","Baguio");

mysql> insert into students values(104, "Tina", 30, "F", "2016-11-23","Baguio");

DISPLAY ALL
mysql> SELECT * FROM <tableName>;
+---------+------------------+------+--------+------------+-------------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+-------------+
| 102 | Jose | 22 | M | 2016-11-23 | La Trinidad |
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
| 2300217 | My Database | 21 | F | 2005-04-13 | Manila |
+---------+------------------+------+--------+------------+-------------+

DISPLAY SELECTED VALUE


mysql> select gender from <tableName>;
+--------+
| gender |
+--------+
| M |
| F |
+--------+

mysql> select gender from <tableName> where gender="M";


+--------+
| gender |
+--------+
| M |
+--------+

mysql> SELECT city from <tableName> where city="Baguio";


+--------+
| city |
+--------+
| Baguio |
| Baguio |
| BAGUIO |
+--------+

mysql> SELECT stud_name from <tableName> where city="Baguio";


+------------------+
| stud_name |
+------------------+
| Jose |
| Tina |
| nnn |
+------------------+

mysql> select * from students where age=32 or city="Baguio";


+---------+------------------+------+--------+------------+--------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+--------+
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
+---------+------------------+------+--------+------------+--------+

mysql> select city from students;


+-------------+
| city |
+-------------+
| La Trinidad |
| Baguio |
| Baguio |
| BAGUIO |
| Manila |
+-------------+

COUNTS THE SELECTED VALUE ACCORDING TO GROUP


mysql> select city, count(stud_id) as total_students from students group by city;
+-------------+----------------+
| city | total_students |
+-------------+----------------+
| La Trinidad | 1 |
| Baguio | 3 |
| Manila | 1 |
+-------------+----------------+

ARRANGE/RANK (ASCENDING)
mysql> select * from students order by city;
+---------+------------------+------+--------+------------+-------------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+-------------+
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 106 | Roy | 31 | M | 2016-11-23 | Baguio |
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
| 102 | Jose | 22 | M | 2016-11-23 | La Trinidad |
| 2300217 | My Database | 21 | F | 2005-04-13 | Manila |
+---------+------------------+------+--------+------------+-------------+

mysql> select * from students order by age;


+---------+------------------+------+--------+------------+-------------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+-------------+
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
| 2300217 | My Database | 21 | F | 2005-04-13 | Manila |
| 102 | Jose | 22 | M | 2016-11-23 | La Trinidad |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 106 | Roy | 31 | M | 2016-11-23 | Baguio |
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
+---------+------------------+------+--------+------------+-------------+

ARRANGE/RANK (DESCENDING)
mysql> select * from students order by age desc;
+---------+------------------+------+--------+------------+-------------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+-------------+
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
| 106 | Roy | 31 | M | 2016-11-23 | Baguio |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 102 | Jose | 22 | M | 2016-11-23 | La Trinidad |
| 2300217 | My Database | 21 | F | 2005-04-13 | Manila |
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
+---------+------------------+------+--------+------------+-------------+

mysql> select * from students order by stud_name desc;


+---------+------------------+------+--------+------------+-------------+
| stud_id | stud_name | age | gender | dob | city |
+---------+------------------+------+--------+------------+-------------+
| 10000 | nnnn | 19 | M | 2005-04-15 | BAGUIO |
| 104 | Tina | 30 | F | 2016-11-23 | Baguio |
| 106 | Roy | 31 | M | 2016-11-23 | Baguio |
| 2300217 | My Database | 21 | F | 2005-04-13 | Manila |
| 102 | Jose | 22 | M | 2016-11-23 | La Trinidad |
| 103 | Jose | 45 | M | 2016-11-23 | Baguio |
+---------+------------------+------+--------+------------+-------------+

UPPERCASE/CAPITALIZE
mysql> select upper(stud_name) from students;
+------------------+
| upper(stud_name) |
+------------------+
| JOSE |
| JOSE |
| TINA |
| ROY |
| nnn |
| MY DATABASE |
+------------------+

LOWERCASE
mysql> select lcase(stud_name) from students;
+------------------+
| lcase(stud_name) |
+------------------+
| jose |
| jose |
| tina |
| roy |
| nnn |
| my database |
+------------------+

COUNTS THE LENGTH //charlength


mysql> select stud_name, length(stud_name) from students;
+------------------+-------------------+
| stud_name | length(stud_name) |
+------------------+-------------------+
| Jose | 4 |
| Jose | 4 |
| Tina | 4 |
| Roy | 3 |
| nnn | 16 |
| My Database | 11 |
+------------------+-------------------+

CONCATENATE/JOINING CHARACTER STRING END TO END


mysql> select concat("Jich", "is", "my", "name");
+------------------------------------+
| concat("Jich", "is", "my", "name") |
+------------------------------------+
| Jichismyname |
+------------------------------------+
mysql> select stud_name, age, concat(stud_name, " ", age) from students;
+------------------+------+-----------------------------+
| stud_name | age | concat(stud_name, " ", age) |
+------------------+------+-----------------------------+
| Jose | 22 | Jose 22 |
| Jose | 45 | Jose 45 |
| Tina | 30 | Tina 30 |
| Roy | 31 | Roy 31 |
| nnn | 19 | nnn 19 |
| My Database | 21 | My Database 21 |
+------------------+------+-----------------------------+

mysql> select stud_name, age, length(stud_name) from students;


+------------------+-----------------------+
| stud_name | length(stud_name) |
+------------------+-----------------------+
| Jose | 4 |
| Jose | 4 |
| Tina | 4 |
| Roy | 3 |
| nnn | 3 |
| My Database | 11 |
+------------------+-----------------------+

REVERSE THE WORD


mysql> select reverse(stud_name) from students;
+--------------------+
| reverse(stud_name) |
+--------------------+
| esoJ |
| esoJ |
| aniT |
| yoR |
| nnn |
| esabataD yM |
+--------------------+

mysql> select abs(age) from students;


+----------+
| abs(age) |
+----------+
| 22 |
| 45 |
| 30 |
| 31 |
| 19 |
| 21 |
+----------+
REPLACE
mysql> select replace("Orange is a fruit", "fruit", "color");
+------------------------------------------------+
| replace("Orange is a fruit", "fruit", "color") |
+------------------------------------------------+
| Orange is a color |
+------------------------------------------------+

mysql> select length(" WORLD WAR Z ");


+--------------------------------------------------------------+
| length(" WORLD WAR Z ") |
+--------------------------------------------------------------+
| 50 |
+--------------------------------------------------------------+

TRIM ON RIGHT
mysql> select rtrim(" WORLD WAR Z ");
+-------------------------------------------------------------+
| rtrim(" WORLD WAR Z ") |
+-------------------------------------------------------------+
| WORLD WAR Z |
+-------------------------------------------------------------+

TRIM ON LEFT
mysql> select ltrim(" WORLD WAR Z ");
+-------------------------------------------------------------+
| ltrim(" WORLD WAR Z ") |
+-------------------------------------------------------------+
| WORLD WAR Z |
+-------------------------------------------------------------+

COUNTS THE LEFT TRIM


mysql> select length(ltrim(" WORLD WAR Z "));
+---------------------------------------------------------------------+
| length(ltrim(" WORLD WAR Z ")) |
+---------------------------------------------------------------------+
| 22 |
+---------------------------------------------------------------------+

COUNTS THE POSITION


mysql> select position("Jich" in "My name is Jich");
+---------------------------------------+
| position("Jich" in "My name is Jich") |
+---------------------------------------+
| 12 |
+---------------------------------------+
ASCII VALUE
mysql> select ascii('a');
+------------+
| ascii('a') |
+------------+
| 97 |
+------------+

mysql> select ascii(age) as ascii_age from students;


+-----------+
| ascii_age |
+-----------+
| 50 |
| 52 |
| 51 |
| 51 |
| 49 |
| 50 |
+-----------+

mysql> select age,ascii(age) as ascii_age from students;


+------+-----------+
| age | ascii_age |
+------+-----------+
| 22 | 50 |
| 45 | 52 |
| 30 | 51 |
| 31 | 51 |
| 19 | 49 |
| 21 | 50 |
+------+-----------+

ERROR HANDLING

CREATE DATABASE
mysql> create database if not exists FIRSTDATABASE;

DELETE/DROP DATABASE
mysql> drop database if exists FIRSTDATABASE;

ALTER TABLE

ADD COLUMN
mysql> alter table <tableName> add column age int(25);
DROP COLUMN
mysql> alter table <tableName> drop column age;

REMOVE/DROP PRIMARY KEY


mysql> alter table <tableName> drop primary key;

ADD PRIMARY KEY


mysql> alter table <tableName> add primary key(id);

You might also like