Adamans Activity 2019
Adamans Activity 2019
Syntax:
Example:
1. SHOW DATABASES;
Output
Here, you can see the all created databases.
Syntax:
1. USE database_name;
Example:
1. USE customers;
Syntax:
Example:
1. SHOW DATABASES;
Output:
Syntax:
Parameters:
OR REPLACE: It is optional. It is used when a VIEW already exist. If you do not specify this
clause and the VIEW already exists, the CREATE VIEW statement will return an error.
view_name: It specifies the name of the VIEW that you want to create in MySQL.
WHERE conditions: It is also optional. It specifies the conditions that must be met for the
records to be included in the VIEW.
The following example will create a VIEW name "trainer". This is a virtual table made by
taking data from the table "courses".
Syntax:
Example:
The following example will alter the already created VIEW name "trainer" by adding a new
column.
1. SELECT*FROM trainer;
MySQL Drop VIEW
You can drop the VIEW by using the DROP VIEW statement.
Syntax:
Parameters:
view_name: It specifies the name of the VIEW that you want to drop.
IF EXISTS: It is optional. If you do not specify this clause and the VIEW doesn't exist, the
DROP VIEW statement will return an error.
Example:
Syntax:
Example:
Note:
1. Here, NOT NULL is a field attribute and it is used because we don't want this field to
be NULL. If you will try to create a record with NULL value, then MySQL will raise an
error.
2. The field attribute AUTO_INCREMENT specifies MySQL to go ahead and add the next
available number to the id field.PRIMARY KEY is used to define a column as primary
key. You can use multiple columns separated by comma to define a primary key.
1. SHOW tables;
The ALTER statement is always used with "ADD", "DROP" and "MODIFY" commands
according to the situation.
Parameters
table_name: It specifies the name of the table that you want to modify.
new_column_name: It specifies the name of the new column that you want to add to the
table.
column_definition: It specifies the data type and definition of the column (NULL or NOT
NULL, etc).
FIRST | AFTER column_name: It is optional. It tells MySQL where in the table to create
the column. If this parameter is not specified, the new column will be added to the end of
the table.
Example:
In this example, we add a new column "cus_age" in the existing table "cus_tbl".
Output:
See the recently added column:
Output:
2) Add multiple columns in the table
Syntax:
Example:
In this example, we add two new columns "cus_address", and cus_salary in the existing
table "cus_tbl". cus_address is added after cus_surname column and cus_salary is added
after cus_age column.
Syntax:
Example:
In this example, we modify the column cus_surname to be a data type of varchar(50) and
force the column to allow NULL values.
Output:
Example:
Output:
6) RENAME table
Syntax:
Example:
Output:
See the renamed table:
The TRUNCATE TABLE statement is used when you want to delete the complete data from a
table without removing the table structure.
Syntax:
Example:
This example specifies how to truncate a table. In this example, we truncate the table
"cus_tbl".
Output:
Output:
Syntax:
This example specifies how to drop a table. In this example, we are dropping the table
"cus_tbl".
MySQL Queries
A list of commonly used MySQL queries to create database, use database, create table,
insert record, update record, delete record, select record, truncate table and drop table are
given below.
More Details...
1. use db1;
More Details...
More Details...
More Details...
More Details...
More Details...
More Details...
8) MySQL Select Query
Oracle select query is used to fetch records from database. For example:
More Details...
More Details...
Syntax:
The SQL INSERT INTO command is used to insert data in MySQL table. Following is a
generic syntax:
Field name is optional. If you want to specify partial values, field name is mandatory.
Example:
Or,
Visual Representation:
See the data within the table by using the SELECT command:
Syntax:
Following is a generic syntax of UPDATE command to modify data into the MySQL table:
Note:
Example:
Here, we have a table "cus_tbl" within the database "customers". We are going to update
the data within the table "cus_tbl".
This query will update cus_surname field for a record having cus_id as 5.
1. UPDATE cus_tbl
2. SET cus_surname = 'Ambani'
3. WHERE cus_id = 5;
Visual Representation:
Syntax:
Example:
Output:
MySQL SELECT Statement
The MySQL SELECT statement is used to fetch data from the one or more tables in MySQL.
We can retrieve records of all fields or specified fields.
1. SELECT expressions
2. FROM tables
3. [WHERE conditions];
Example:
Let's take two tables "students" and "officers", having the following data.
Output:
MySQL WHERE Clause
MySQL WHERE Clause is used with SELECT, INSERT, UPDATE and DELETE clause to filter
the results. It specifies a specific position where you have to do the operation.
Syntax:
1. WHERE conditions;
Parameter:
conditions: It specifies the conditions that must be fulfilled for records to be selected.
Table structure:
Execute this query:
1. SELECT *
2. FROM officers
3. WHERE address = 'Mau';
Output:
MySQL WHERE Clause with AND condition
In this example, we are retrieving data from the table "officers" with AND condition.
1. SELECT *
2. FROM officers
3. WHERE address = 'Lucknow'
4. AND officer_id < 5;
Output:
1. SELECT *
2. FROM officers
3. WHERE address = 'Lucknow'
4. OR address = 'Mau';
Output:
MySQL WHERE Clause with combination of
AND & OR conditions
You can also use the AND & OR conditions altogether with the WHERE clause.
1. SELECT *
2. FROM officers
3. WHERE (address = 'Mau' AND officer_name = 'Ajeet')
4. OR (officer_id < 5);
Output:
Syntax:
Parameters
expressions: specify the columns or calculations that you want to retrieve.
tables: specify the name of the tables from where you retrieve records. There must be at
least one table listed in the FROM clause.
WHERE conditions: It is optional. It specifies the conditions that must be met for the
records to be selected.
Note:
o If you put only one expression in the DISTINCT clause, the query will return the
unique values for that expression.
o If you put more than one expression in the DISTINCT clause, the query will retrieve
unique combinations for the expressions listed.
o In MySQL, the DISTINCT clause doesn't ignore NULL values. So if you are using the
DISTINCT clause in your SQL statement, your result set will include NULL as a
distinct value.
Syntax:
1. FROM table1
2. [ { INNER JOIN | LEFT [OUTER] JOIN| RIGHT [OUTER] JOIN } table2
3. ON table1.column1 = table2.column1 ]
Parameters
table1 and table2: specify tables used in the MySQL statement. The two tables are joined
based on table1.column1 = table2.column1.
Note:
o If you are using the FROM clause in a MySQL statement then at least one table must
have been selected.
o If you are using two or more tables in the MySQL FROM clause, these tables are
generally joined using INNER or OUTER joins.
Syntax:
1. SELECT expressions
2. FROM tables
3. [WHERE conditions]
4. ORDER BY expression [ ASC | DESC ];
Parameters
expressions: It specifies the columns that you want to retrieve.
tables: It specifies the tables, from where you want to retrieve records. There must be at
least one table listed in the FROM clause.
WHERE conditions: It is optional. It specifies conditions that must be fulfilled for the
records to be selected.
ASC: It is optional. It sorts the result set in ascending order by expression (default, if no
modifier is provider).
DESC: It is also optional. It sorts the result set in descending order by expression.
Note: You can use MySQL ORDER BY clause in a SELECT statement, SELECT LIMIT
statement, and DELETE LIMIT statement.
1. SELECT *
2. FROM officers
3. WHERE address = 'Lucknow'
4. ORDER BY officer_name;
Output:
Output:
Output:
You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on the
grouped column.
Syntax:
Parameters
expression1, expression2, ... expression_n: It specifies the expressions that are not
encapsulated within an aggregate function and must be included in the GROUP BY clause.
aggregate_function: It specifies a function such as SUM, COUNT, MIN, MAX, or AVG etc.
tables: It specifies the tables, from where you want to retrieve the records. There must be
at least one table listed in the FROM clause.
WHERE conditions: It is optional. It specifies the conditions that must be fulfilled for the
records to be selected.
Output:
Output:
(iii) MySQL GROUP BY Clause with MIN
function
The following example specifies the minimum working hours of the employees form the
table "employees".
Output:
(iv) MySQL GROUP BY Clause with MAX
function
The following example specifies the maximum working hours of the employees form the
table "employees".
Output:
(v) MySQL GROUP BY Clause with AVG
function
The following example specifies the average working hours of the employees form the table
"employees".
Output:
Syntax:
Parameters
aggregate_function: It specifies any one of the aggregate function such as SUM, COUNT,
MIN, MAX, or AVG.
expression1, expression2, ... expression_n: It specifies the expressions that are not
encapsulated within an aggregate function and must be included in the GROUP BY clause.
WHERE conditions: It is optional. It specifies the conditions for the records to be selected.
HAVING condition: It is used to restrict the groups of returned rows. It shows only those
groups in result set whose conditions are TRUE.
Here, we use the SUM function with the HAVING Clause to return the emp_name and sum
of their working hours.
Simply, it can also be used with COUNT, MIN, MAX and AVG functions.