You are on page 1of 2

MySQL Module 1 Assignment

1. Creating Database

To Create database the Query is CREATE DATABASE Vishnu. This will create a new database.

2. Using the database.

To use the created Database the Query is USE Vishnu.

3. Creating Table

To Create a table the Query is


CREATE TABLE trainee
(
trainee_id int IDENTITY(1,1) primary key,
trainee_name varchar(50),
trainee_age int,
city varchar(50),
course varchar(50),
cellphone bigint,
trainer varchar(255) default 'mrinmoy'
)
This will Create a table trainee with columns
i) Trainee_id, Here the it is denoted by IDENTITY which means is NOT NULL and Unique and
(1,1) indicates starting number and Increment number. The INT indicate it is an integer.
ii) Trainee_name, it is of varchar character which means variable Character.
iii) Trainee_age it is integer data type.
iv) City, it is of varchar character
v) Course, it is of varchar character
vi) Cellphone, as a cell phone number consists of 10 numbers, we can use bigint data type.
Which means big integer.
vii) Trainer, it is of varchar. The default constraint automatically takes the value as mrinmoy.
4. Insert Values
insert into trainee ( trainee_name, trainee_age, city, course, cellphone)
values
('saransh', 25, 'mumbai', 'sql server', 1234567890),
('vishnu', 25, 'kanigiri', 'sql server', 9790469085),
('aarthi', 24, 'Delhi', 'sql server', 7896543210),
('aditya', 25, 'bangalore', 'sql server', 8975642310)

5. Alter table.
If I want to add an extra column with column name age to the existing table, the query is
alter table trainee
add gender varchar(50) null
It will create a new column in the table trainee with null values.
To Insert the values into the column the Query is
update trainee
set gender = iif ( trainee_id in (1, 2, 4), 'M', 'F')
It will update the age column with ‘M’ for those who are having trainee_id as 1, 2, 4 and the rest
of the trainee_id with ‘F’

6) Select from the table.


To see the contents of the table, The Query is select * from trainee
It will display all columns containing in a table.

7) Delete a column.
To delete a Column in a table the Query is
alter table trainee
drop column gender
The column will be deleted.

8) Delete a Table
drop table trainee

9) Delete a Database
drop database vishnu

You might also like