You are on page 1of 19

ASSIGNMENT 1

Ishita Gupta

Electronics Engineering

191061033

AIM: Study of DDL and DML operations on database Tables using SQL.
TOOL: MariaDB
PROGRAMMING LANGUAGE: Structured Query language (SQL)

THEORY:
1. SQL:
SQL (Structured Query Language) is used in programming and designed for managing
data held in relational database management systems (RDBMS). It is particularly useful
in handling structured data. SQL can execute queries against a database, retrieve data
from a database, insert records into database tables, etc.

2. DDL and DML Commands

DDL is an acronym for Data Definition Language. It consists of SQL commands that can
be used to define database schema.
CREATE - Creates a database or its object.
DROP - To delete objects from the database.
ALTER - To alter the structure of the database.
TRUNCATE - To remove all records from a table.
RENAME - To rename an object existing in the database.

DML is an acronym for Data Manipulation Language. It deals with the manipulation of
the present data in the database.
INSERT - To insert data into a table.
UPDATE - To update existing data within a table.
DELETE - To delete records from a database table.

1
OPERATIONS EXECUTED:
Create Database: create database database_name;
1. Create Database: create database database_name;
2. Use Database: use database_name;
3. Show Databases: show databases;
4. Drop database: drop database database_name;
5. Create Table: create table table_name (colm 1 datatype, colm 2 datatype);
6. Describe Table: desc table_name or describe table_name;
7. Show create table table_name;
8. Insert into Table (2 formats)
9. Insert Null values in table
10. Select queries (with and without * )
11. Select Null values from table : is NULL in where clause
12. Alter table (Add Column): alter table table_name add column_name datatype;
13. Alter table (Drop Column): alter table table_name drop column_name;
14. Alter table (Change Column’s data type): alter table table_name modify
column_name new_datatype;
15. Alter table (Change Column’s name): alter table table_name change
old_column_name new_column_name datatype_to_be_assigned;
16. Update: update table_name set column_name = value where…;
17. Delete: delete from table_name where column_name = value;
18. Truncate: truncate table_name;
19. Rename table: rename table old_table_name to new_table_name;
20. Drop: drop table
table_name; (Use of upward
arrow key)

NOW(), CURDATE(), CURTIME(), USER()

2
OUTPUT:
Setting environment for MariaDB 10.8 (x64)

C:\WINDOWS\System32>mysql -u root -p

Enter password: ******

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 11

Server version: 10.8.4-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE ANIMALS;

Query OK, 1 row affected (0.012 sec)

MariaDB [(none)]> USE ANIMALS;

Database changed

MariaDB [ANIMALS]> SHOW DATABASES;

+ +

| Database |

+ +

| animals |

| database_name |

| database_select |

| information_schema |

| mysql |

| performance_schema |

| ishita |

| sys |

3
| vjti |

+ +

9 rows in set (0.016 sec)

MariaDB [ANIMALS]> CREATE DATABASE BIRDS;

Query OK, 1 row affected (0.001 sec)

MariaDB [ANIMALS]> SHOW DATABASES;

+ +

| Database |

+ +

| animals |

| birds |

| database_name |

| database_select |

| information_schema |

| mysql |

| performance_schema |

| ishita |

| sys |

| vjti |

+ +

10 rows in set (0.001 sec)

MariaDB [ANIMALS]> DROP DATABASE BIRDS;

Query OK, 0 rows affected (0.040 sec)

MariaDB [animals]> SHOW DATABASES;

+ +

4
| Database |

+ +

| animals |

| database_name |

| database_select |

| information_schema |

| mysql |

| performance_schema |

| sakshi |

| sys |

| vjti |

+ +

9 rows in set (0.001 sec)

MariaDB [animals]> CREATE TABLE Animals(name VARCHAR(50), type VARCHAR(50), age INT);

Query OK, 0 rows affected (0.040 sec)

MariaDB [animals]> DESC animals;

+ + + + + + +

| Field | Type | Null | Key | Default | Extra |

+ + + + + + +

| name | varchar(50) | YES | | NULL | |

| type | varchar(50) | YES | | NULL | |

| age | int(11) | YES | | NULL | |

+ + + + + + +

3 rows in set (0.043 sec)

MariaDB [animals]> SHOW CREATE TABLE Animals;

5
+ + -
+

| Table | Create Table


|

+ + -
+

| Animals | CREATE TABLE `animals` (

`name` varchar(50) DEFAULT NULL,

`type` varchar(50) DEFAULT NULL,

`age` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |

+ + -
+

1 row in set (0.003 sec)

MariaDB [animals]> INSERT INTO Animals(name,type,age) VALUES('Shelby','Dog',2), ('Skitu', 'Cat', 4);

Query OK, 2 rows affected (0.011 sec)

Records: 2 Duplicates: 0 Warnings: 0

MariaDB [animals]> SELECT * FROM Animals;

+ + + +

| name | type | age |

+ + + +

| Shelby | Dog | 2 |

| Skitu | Cat | 4 |

+ + + +

2 rows in set (0.004 sec)

MariaDB [animals]> INSERT INTO Animals(name, age) VALUES('Alex',3);

Query OK, 1 row affected (0.004 sec)

6
MariaDB [animals]> INSERT INTO Animals()VALUES();

Query OK, 1 row affected (0.043 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + +

| name | type | age |

+ + + +

| Shelby | Dog | 2 |

| Skitu | Cat | 4 |

| Alex | NULL | 3 |

| NULL | NULL | NULL |

+ + + +

4 rows in set (0.001 sec)

MariaDB [animals]> SELECT name, age FROM Animals;

+ + +

| name | age |

+ + +

| Shelby | 2 |

| Skitu | 4 |

| Alex | 3 |

| NULL | NULL |

+ + +

4 rows in set (0.003 sec)

MariaDB [animals]> SELECT * FROM Animals WHERE type is NULL;

+ + + +

| name | type | age |

7
+ + + +

| Alex | NULL | 3 |

| NULL | NULL | NULL |

+ + + +

2 rows in set (0.005 sec)

MariaDB [animals]> ALTER TABLE Animals ADD(owner VARCHAR(50), owner_age VARCHAR(50));

Query OK, 0 rows affected (0.018 sec)

Records: 0 Duplicates: 0 Warnings: 0

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 2 | NULL | NULL |

| Skitu | Cat | 4 | NULL | NULL |

| Alex | NULL | 3 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

4 rows in set (0.017 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 2 | NULL | NULL |

| Skitu | Cat | 4 | NULL | NULL |

| Alex | NULL | 3 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL |

8
+ + + + + +

4 rows in set (0.001 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 2 | NULL | NULL |

| Skitu | Cat | 4 | NULL | NULL |

| Alex | NULL | 3 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

4 rows in set (0.001 sec)

MariaDB [animals]> UPDATE Animals SET owner_age = 30 WHERE name = 'Shelby';

Query OK, 1 row affected (0.007 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [animals]> UPDATE Animals SET owner_age = 34 WHERE name = 'Skitu';

Query OK, 1 row affected (0.003 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 2 | NULL | 30 |

| Skitu | Cat | 4 | NULL | 34 |

| Alex | NULL | 3 | NULL | NULL |

9
| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

4 rows in set (0.001 sec)

MariaDB [animals]> UPDATE Animals SET age = 8 WHERE name = 'Shelby';

Query OK, 1 row affected (0.001 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 8 | NULL | 30 |

| Skitu | Cat | 4 | NULL | 34 |

| Alex | NULL | 3 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

4 rows in set (0.000 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 8 | NULL | 30 |

| Skitu | Cat | 4 | NULL | 34 |

| Alex | NULL | 3 | NULL | NULL |

| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

4 rows in set (0.001 sec)

10
MariaDB [animals]> DELETE FROM Animals WHERE name = 'Alex';

Query OK, 1 row affected (0.004 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 8 | NULL | 30 |

| Skitu | Cat | 4 | NULL | 34 |

| NULL | NULL | NULL | NULL | NULL |

+ + + + + +

3 rows in set (0.001 sec)

MariaDB [animals]> DELETE FROM Animals WHERE type is NULL;

Query OK, 1 row affected (0.003 sec)

MariaDB [animals]> SELECT * FROM Animals;

+ + + + + +

| name | type | age | owner | owner_age |

+ + + + + +

| Shelby | Dog | 8 | NULL | 30 |

| Skitu | Cat | 4 | NULL | 34 |

+ + + + + +

2 rows in set (0.001 sec)

MariaDB [animals]> ALTER TABLE animals RENAME TO types_of_animals;

Query OK, 0 rows affected (0.013 sec)

11
MariaDB [animals]> SHOW TABLES;

+ +

| Tables_in_animals |

+ +

| types_of_animals |

+ +

1 row in set (0.001 sec)

MariaDB [animals]> TRUNCATE TABLE types_of_animals;

Query OK, 0 rows affected (0.023 sec)

MariaDB [animals]> SELECT * FROM types_of_animals;

Empty set (0.001 sec)

MariaDB [animals]> DROP TABLE types_of_animals;

Query OK, 0 rows affected (0.012 sec)

MariaDB [animals]> SHOW TABLES;

Empty set (0.001 sec)

SNAPSHOTS OF CODE:

12
13
14
15
16
17
18
CONCLUSION:
We have studied and executed various DDL and DML operations on database tables using

SQL. Operations like CREATE, INSERT, UPDATE, DELETE, ALTER, etc. are executed in the

above code.

19

You might also like