MySQL Command
MySQL Command
exit;
To exit MySQL command line.
Exact Numeric:
o INTEGER
o SMALLINT
o BIGINT
o NUMERIC
o DECIMAL
Boolean:
BOOLEAN
Character Strings:
CHARACTER
CHARACTER VARYING (VARCHAR)
Date times:
DATE
TIME WITHOUT TIMEZONE
TIMESTAMP WITHOUT TIMEZONE
TIME WITH TIMEZONE
TIMESTAMP WITH TIMEZONE
A database is a collection of organized records that the user can conveniently access and
manage. It organizes the data into tables, rows, columns, and indexes to make it easier to
retrieve the information we need quickly. Let’s try to build a student database using the
following command.
The following syntax can create a database. It also verifies whether the database name is
already in use.
Use the following syntax to replace the current database with another database you’re
working on:
Using the following syntax, we can permanently destroy a database and its associated files.
All the tables that are a part of that database will be deleted if we use the following
command.
As you can see in the snapshot above, we created a database named ‘students’ using create
command, and deleted a database named class using DROP command.
A table in a database is a collection of related data organized in a table structure. It’s made up
of columns and rows. A table is a collection of data components in relational and flat-file
databases that uses a model of vertical columns and horizontal rows, with the cell being the
unit where a row and column intersect.
MARKS TABLE
ID Name Marks
001 Ashish 94
002 Bharat 81
003 Deepak 78
004 Fatima 92
The datatype can be integer, char (fixed length sring), varchar (variable length string), binary
etc. The data which is going to be stored in a column decides the datatype of the column. For
example, if the column is going to store numerals, integer datatype can be used or if the
column name is going to store a string of variable length, varchar can be used. For example,
to create the above Marks table, type the following code:
CREATE TABLE Marks(ID integer, Name varchar (100), Marks integer);
Assume we have a contact book with the users’ names and phone numbers. We’re looking for
a phone number in this contact book. If the contact book is in an unordered manner, which
means the names aren’t organized alphabetically, we’ll have to go through all the pages and
read every name until we don’t locate the requested name. Sequential searching is the name
for this form of search. While this approach is correct, applying this in a large database will
consume a lot of time. In this situation, database indexing aids in the retrieval of the desired
result and increases the query’s overall performance.
Indexes are used to quickly locate records with certain column values. It is similar to the
index given in a book, you can easily locate a chapter using the index of the book.
A unique index ensures that the index key includes no duplicate values, ensuring that each
entry in the table is distinct in some sense. To create a unique index, use the following
syntax:
CREATE UNIQUE INDEX index_name
ON table_name (column,…);
A view is a database object that doesn’t have any data in it. Its contents are based on the table
that serves as the foundation. It has the same rows and columns as a genuine table. In
MySQL, a View is a virtual table that is produced by connecting one or more tables in a
query. It works in the same way as the base table, but it doesn’t have any data of its own. The
fundamental distinction between a view and a table is that views are definitions constructed
on top of other tables (or views).
Let’s try to understand it with an example. We are going to create a view from the Marks
table mentioned above.
We can use SELECT to see the actual view, just like what we do with tables.
Rename a view:
RENAME TABLE view_name
TO new_view_name;
For example, to query the entire contents of a table, use the following command:
SELECT * FROM table_name;
The above query will return each record of the given table.
For example:
If you want MySQL to show only some columns of the table, you can use the following
syntax:
SELECT
column1, column2, …
FROM
Table_name;
We can filter records using WHERE clause. It returns only those records which fulfill a
specified set of condition.
SELECT column_list
FROM table_name
WHERE condition;
For example, if we want to write a query to retrieve the name of students whose score is less
than 80, it will look something like this:
SELECT Name from Marks
WHERE Marks <80;
You can change the output of the column name using column alias:
SELECT
column1 AS alias_name,
expression AS alias,
…
FROM
Table_name;
The name of the column will be changed to alias_name in the result (not in the actual table).
You can sort a result set in ascending and descending order by writing the following code::
SELECT
select_list
FROM
table_name
ORDER BY
column1 ASC [DESC],
column2 ASC [DESC];
Join
In a join, you can get records from two (or more) logically connected tables in one result set.
JOIN clauses are used to return the rows of two or more queries that use two or more tables
that have a meaningful relationship based on a set of values in common. These values are
normally the same column name and datatype that occur in both of the connected tables.
The join key is usually, but not always, the primary key of one table and a foreign key in
another table. As long as the data in the columns matches, the join can be executed.
members_id name
1 John
2 Jane
3 Mary
4 David
5 Amelia
committee_id name
1 John
2 Mary
3 Amelia
4 Joe
Inner Join
Each row from the first table is compared to every record from the second table in the inner
join clause. If the join condition is met in both rows, the inner join clause creates a new row
with a column that contains all columns from both tables and includes it in the result set. In
other words, only matched rows from both tables are included in the inner join clause. Each
row from the first table is compared to every record from the second table in the inner join
clause.
To query data from multiple tables using inner join, use the following command:
SELECT select_list
FROM table1
INNER JOIN table2 ON condition;
FROM
members m
INNER JOIN committees c ON c.name = m.name;
It will produce the following result:
1 John 1 John
3 Mary 2 Mary
5 Amelia 3 Amelia
Left join
A join predicate is required for a left join, just as it is for an inner join.The left join selects
data from the table on the left. The left join compares every row in the left table to every row
in the right table. The left join clause creates a new row whose columns comprise all
columns of the rows in both tables if the values in the two rows satisfy the join condition, and
includes this row in the result set. The left join clause still creates a new row whose columns
contain columns from the left table and NULL for columns from the right table if the values
in the two rows do not match.
You can query data from multiple tables using left join by using the following syntax:
SELECT select_list
FROM table1
LEFT JOIN table2 ON condition;
FROM
members m
LEFT JOIN committees c USING(name);
The resulting table will look like this:
1 John 1 John
3 Mary 2 Mary
5 Amelia 3 Amelia
The right join clause is similar to the left join clause, with the exception that the left and right
tables are treated differently. Instead of selecting data from the left table, the right join selects
data from the right table.
You can query data from multiple tables using the right join:
SELECT select_list
FROM table1
RIGHT JOIN table2 ON condition;
1 John 1 John
3 Mary 2 Mary
5 Amelia 3 Amelia
A Cartesian product of rows from the joined tables is created by the cross join. To create the
result set, the cross join joins every row from the first table with every row from the right
table. Let’s say there are n rows in the first table and m rows in the second table. The nxm
rows will be returned by the cross join that joins the tables.
Syntax:
SELECT select_list
FROM table1
CROSS JOIN table2;
In order to cross join our two tables, the syntax will look like this:
SELECT
m.member_id,
m.name AS member,
c.committee_id,
c.name AS committee
FROM
members m
CROSS JOIN committees c;
Searching
LIKE and RLIKE clauses are used to search desired records from the table.
The SQL LIKE clause compares a value to other values that are similar using wildcard
operators. With the LIKE operator, there are two wildcards that can be used.
The % sign can be used to indicate zero, one, or more characters. A single number or letter is
represented by the underscore. These symbols can be mixed and matched.
The syntax for LIKE clause
SELECT select_list
FROM table_name
WHERE column LIKE ‘%pattern%’ (or ‘_ABC’);
E.g, ‘S%’ will fetch all values that start with S.
‘_AB’ will fetch all values that have A and B at second and third places respectively.
In MySQL, this operator is used to pattern match a string expression against a pattern.
Syntax:
SELECT select_list
FROM table_name
WHERE column RLIKE ‘regular_expression’;