Basic Operations With MySQL - Part 1
Basic Operations With MySQL - Part 1
⚫ Creating a new database - Creates a new empty database with the name
dbname.
Now you are inside the space provided for the db dbname. At this
moment the user can create tables.
An SQL developer must decide what type of data that will be stored inside each
column when creating a table. The data type is a guideline for SQL to
understand what type of data is expected inside of each column, and it also
identifies how SQL will interact with the stored data.
The data type in MySQL with the following characteristics:
o The type of values (fixed or variable) it represents.
o The storage space it takes is based on whether the values are a fixed-
length or variable length.
o Its values can be indexed or not.
o How MySQL performs a comparison of values of a particular data type.
In MySQL there are three main data types: string, numeric, and date and time.
ENUM(val1, val2, ₒ A string object that can have only one value, chosen
val3, ...) from a list of possible values.
ₒ You can list up to 65535 values in an ENUM list.
ₒ If a value is inserted that is not in the list, a blank
value will be inserted.
ₒ The values are sorted in the order you enter them
DOUBLE
PRECISION(size, d)
Note: All the numeric data types may have an extra option: UNSIGNED or
ZEROFILL. If you add the UNSIGNED option, MySQL disallows negative values
for the column. If you add the ZEROFILL option, MySQL automatically also
adds the UNSIGNED attribute to the column.
3. Date and Time Data Types
TIMESTAMP(fsp) ₒ A timestamp.
ₒ TIMESTAMP values are stored as the number of seconds
since the Unix epoch ('1970-01-01 00:00:00' UTC).
ₒ Format: YYYY-MM-DD hh:mm:ss.
ₒ The supported range is from '1970-01-01 00:00:01' UTC
to '2038-01-09 03:14:07' UTC.
ₒ Automatic initialization and updating to the current date
and time can be specified using DEFAULT
CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP in the column definition
TIME(fsp) ₒ A time.
ₒ Format: hh:mm:ss.
ₒ The supported range is from '-838:59:59' to '838:59:59'
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.… );
Where,
ₒ The column parameters specify the names of the columns of the table.
ₒ The datatype parameter specifies the type of data the column can hold (e.g.
varchar, integer, date, etc.).
Where Clause:
ₒ If you create a new table using an existing table, the new table will be filled
with the existing values from the old table, if the where clause is skipped.
ₒ If where clause is included, then the records satisfying the where clause
condition will be inserted into the new table.
ₒ If the where clause condition is not satisfied then none of the records will
be inserted into the new table. Thus an empty table will be created.
Syntax:
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE <condition>;
SHOW TABLES allows you to get all the tables at your fingertips.
MySQL returns the results in a table with one column. The tables are
ordered in alphabetical order.
The statement to return only the names of those databases that satisfies
the pattern.
Example:
AFTER Order_id;
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
Syntax: ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Email" column from the "Customers"
table:
ALTER TABLE Customers
DROP COLUMN Email;
The following are the syntax that illustrates the column rename using the
CHANGE statement:
This syntax can also allow us to change the column's data types.
⚫ TRUNCATE TABLE
The TRUNCATE TABLE statement is used to delete the data inside a
table, but not the table itself. It works in the same way as a DELETE command
without using a WHERE clause that deletes complete rows from a table.
The following points must be considered while using the TRUNCATE command:
✓ We cannot use the WHERE clause with this command so that filtering of
records is not possible.
✓ We cannot rollback the deleted data after executing this command because
the log is not maintained while performing this operation.
✓ We cannot use the truncate statement when a table is referenced by a
foreign key or participates in an indexed view.
✓ The TRUNCATE command doesn't fire DELETE triggers associated with the
table that is being truncated because it does not operate on individual rows.
INSERT Statement
MySQL INSERT statement is used to store or add data in MySQL table within
the database. We can perform insertion of records in two ways using a single
query in MySQL:
• Insert record in a single row
• Insert record in multiple rows
Syntax: To insert a single record.
NOTE:
• It also ensures that the column name and values should be the
same. Also, the position of columns and corresponding values must
be the same.
[WHERE Clause];
UPDATE employees
SET age = age+1
WHERE firstname = 'Dirk' and lastname='Smith';
DELETE Statement
If you want to delete a record from any MySQL table, then you can use the SQL
command DELETE FROM.
⚫ If the WHERE clause is not specified, then all the records will be deleted
from the given MySQL table.
⚫ You can specify any condition using the WHERE clause.
⚫ You can delete records in a single table at a time.
Example:
B. Delete the records from the Client table whose last names are ‘Kumar’.
1. Create a Employees table that will contain the following information about
your new employees: firstname, lastname, title, age, and salary(5,2)
8. Create a table EmpSal (EmpID, basic) from Employees (EmpID, Salary) table.
9. Alter the table by adding the columns DA, HRA, Gross, PF, IT and Net.
10. Create a table named EmpLoc with columns EmpID, Department with same
structure as Employees table but without any records.