14
Experiment – 1 - Aim: - Create a database and write the programs to carry out following operation: Show DataBases :-You can view all available databases with the SHOW DATABASES Command. Syntax :- mysql> show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | college | | company | | employees | | mysql | | satudent | | student |

Experiment No. 1 DBMS Creating a Database

Embed Size (px)

DESCRIPTION

Experiment No. 1 DBMS Creating a Database

Citation preview

Page 1: Experiment No. 1 DBMS Creating a Database

Experiment – 1

-

Aim: - Create a database and write the programs to carry out following operation:

Show DataBases:-You can view all available databases with the SHOW DATABASES Command.

Syntax:- mysql> show databases;

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| college |

| company |

| employees |

| mysql |

| satudent |

| student |

| studfeesmang |

| test |

Page 2: Experiment No. 1 DBMS Creating a Database

1. Adding a record in databaseInsert:- In this command we add a record in the database.Syntax:- insert table<table name>Example:- mysql> create database college;Query OK, 1 row affected (0.00 sec)

mysql> use college;Database changed mysql> create table student -> ( -> stuname varchar(30), -> sturollno int, -> stuadd varchar(30) -> );Query OK, 0 rows affected (0.09 sec)

mysql> insert into student(stuname,sturollno,stuadd) -> values('pushpa',9358,'sirsa');Query OK, 1 row affected (0.03 sec)

mysql> insert into student(stuname,sturollno,stuadd) -> values('pinki',9378,'sirsa');Query OK, 1 row affected (0.02 sec)

mysql> insert into student(stuname,sturollno,stuadd) -> values('manu',9453,'hisar');Query OK, 1 row affected (0.01 sec)

mysql> insert into student(stuname,sturollno,stuadd) -> values('sahil',9678,'kaithal');Query OK, 1 row affected (0.01 sec)

mysql> select * from student;+---------+-----------+---------+| stuname | sturollno | stuadd |

Page 3: Experiment No. 1 DBMS Creating a Database

+---------+-----------+---------+| pushpa | 9358 | sirsa || pinki | 9378 | sirsa || manu | 9453 | hisar || sahil | 9678 | kaithal |+---------+-----------+---------+4 rows in set (0.02 sec)

mysql>

2. Delete a record in the database.

Delete : in this command we delete a record in the database.

Syntax: :- mysql> delete from tablename

where columnname=expression;

mysql> delete from student

-> where sturollno=9678;

Query OK, 1 row affected (0.02 sec)

mysql> select * from student;

+---------+-----------+--------+

| stuname | sturollno | stuadd |

+---------+-----------+--------+

| pushpa | 9358 | sirsa |

| pinki | 9378 | sirsa |

| manu | 9453 | hisar |

+---------+-----------+--------+

Page 4: Experiment No. 1 DBMS Creating a Database

3 rows in set (0.00 sec)

mysql> delete from student

-> where sturollno=9378;

Query OK, 1 row affected (0.02 sec)

mysql> select * from student;

+---------+-----------+--------+

| stuname | sturollno | stuadd |

+---------+-----------+--------+

| pushpa | 9358 | sirsa |

| manu | 9453 | hisar |

+---------+-----------+--------+

2 rows in set (0.00 sec)

mysql>

3. Modify the record in the databaseModify: this operation is done by using Alter command,in this command we can add or Delete a particular row means this command is used to changes in database.Syntax:alter table<table name> Modify (coloum name datatype(size));

mysql> alter table student -> add stuclass varchar(20);

Page 5: Experiment No. 1 DBMS Creating a Database

Query OK, 2 rows affected (0.11 sec)Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from student;+---------+-----------+--------+----------+| stuname | sturollno | stuadd | stuclass |+---------+-----------+--------+----------+| pushpa | 9358 | sirsa | NULL || manu | 9453 | hisar | NULL |+---------+-----------+--------+----------+2 rows in set (0.00 sec)

mysql> update student -> set stuclass=7 -> where stuname='pushpa';Query OK, 1 row affected (0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0

mysql>mysql> update student -> set stuclass=12 -> where stuname='manu';Query OK, 1 row affected (0.02 sec)Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;+---------+-----------+--------+----------+| stuname | sturollno | stuadd | stuclass |+---------+-----------+--------+----------+| pushpa | 9358 | sirsa | 7 || manu | 9453 | hisar | 12 |+---------+-----------+--------+----------+2 rows in set (0.00 sec)

mysql> alter table student

Page 6: Experiment No. 1 DBMS Creating a Database

-> drop stuclass;Query OK, 2 rows affected (0.08 sec)Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from student;+---------+-----------+--------+| stuname | sturollno | stuadd |+---------+-----------+--------+| pushpa | 9358 | sirsa || manu | 9453 | hisar |+---------+-----------+--------+2 rows in set (0.00 sec)

mysql> select stuname -> from student -> where sturollno=9358;+---------+| stuname |+---------+| pushpa |+---------+1 row in set (0.00 sec)

mysql>4. Generate queries

Generate Queries: in this operation we use relational operator and some special operators in condition like select,and,or,not,between and like,not,sum,count,min,max.

Select command: The sql select into statement is used to select data from a

SQL database table and to insert it to a different table at the same time.

Syntax:select * from <table name>;

Page 7: Experiment No. 1 DBMS Creating a Database

mysql> select * from student;

+---------+-----------+--------+

| stuname | sturollno | stuadd |

+---------+-----------+--------+

| pushpa | 9358 | sirsa |

| pinki | 9378 | sirsa |

| manu | 9453 | hisar |

+---------+-----------+--------+

3 rows in set (0.00 sec)

USING ARITHMETIC OPERATORS

We may need to modify the way in which data is displayed and want to perform calculations. This can be done with arithmetic expressions using +, -, *, /.

E.g.

mysql> select ssn,ename,dnumber,salary,salary*0.10 from employee;

+------+---------+---------+--------+-------------+

| ssn | ename | dnumber | salary | salary*0.10 |

+------+---------+---------+--------+-------------+

| 1 | rosy | d01 | 25000 | 2500.00 |

| 2 | nitin | d02 | 30000 | 3000.00 |

| 3 | rohit | d03 | 23000 | 2300.00 |

| 4 | kashish | d04 | 20000 | 2000.00 |

+------+---------+---------+--------+-------------+

Page 8: Experiment No. 1 DBMS Creating a Database

4 rows in set (0.02 sec)

Defining a Column Alias

Specify the alias after the column in the SELECT list using a space as a separator. Thus, Column Alias renames column headings and is useful with calculation.

E.g.

mysql> select ssn,ename,dnumber,salary,salary*0.10 bonus from employee;

+------+---------+---------+--------+---------+

| ssn | ename | dnumber | salary | bonus |

+------+---------+---------+--------+---------+

| 1 | rosy | d01 | 25000 | 2500.00 |

| 2 | nitin | d02 | 30000 | 3000.00 |

| 3 | rohit | d03 | 23000 | 2300.00 |

| 4 | kashish | d04 | 20000 | 2000.00 |

+------+---------+---------+--------+---------+

4 rows in set (0.00 sec)

LOGICAL OPERATORS

A logical operator combines the result of two component conditions to produce a single result based on them or to invert the result of single condition.

There are three logical operators in SQL. These are:

1) OR– Returns TRUE if either component condition is TRUE.

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where dnumber="d01"or dnumber="d02";

Page 9: Experiment No. 1 DBMS Creating a Database

+------+-------+---------+--------+

| ssn | ename | dnumber | salary |

+------+-------+---------+--------+

| 1 | rosy | d01 | 25000 |

| 2 | nitin | d02 | 30000 |

+------+-------+---------+--------+

2 rows in set (0.06 sec)

AND – Returns TRUE if both component conditions are TRUE.

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where (dnumber="d01"or dnumber="d02")and salary<=25000;

+------+-------+---------+--------+

| ssn | ename | dnumber | salary |

+------+-------+---------+--------+

| 1 | rosy | d01 | 25000 |

+------+-------+---------+--------+

1 row in set (0.00 sec)

NOT – Returns FALSE if the following condition is TRUE.

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where not(dnumber="d01"or dnumber="d02");

+------+---------+---------+--------+

| ssn | ename | dnumber | salary |

Page 10: Experiment No. 1 DBMS Creating a Database

+------+---------+---------+--------+

| 3 | rohit | d03 | 23000 |

| 4 | kashish | d04 | 20000 |

+------+---------+---------+--------+

2 rows in set (0.02 sec)

SPECIAL OPERATORS

There are certain special operators used with WHERE clause. These are:

1) BETWEEN Operator: The BETWEEN operator is used to specify a range of values. The range you specify contains a lower and an upper range. The values specified with BETWEEN operator are inclusive.

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where salary between 20000 and 25000;

+------+---------+---------+--------+

| ssn | ename | dnumber | salary |

+------+---------+---------+--------+

| 1 | rosy | d01 | 25000 |

| 3 | rohit | d03 | 23000 |

| 4 | kashish | d04 | 20000 |

+------+---------+---------+--------+

3 rows in set (0.02 sec)

IN Operator: The IN Operator is used to match any list of values.

Page 11: Experiment No. 1 DBMS Creating a Database

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where salary in(30000,25000);

+------+-------+---------+--------+

| ssn | ename | dnumber | salary |

+------+-------+---------+--------+

| 1 | rosy | d01 | 25000 |

| 2 | nitin | d02 | 30000 |

+------+-------+---------+--------+

2 rows in set (0.00 sec)

LIKE OPERATOR:In certain cases you may not always know the exact value to search for. You can select rows that match a character pattern by using LIKE operator. This operator is used with CHAR datatype.

Two symbols can be used to construct search string:

Symbol Description% Represents any sequence of zero or more characters_ Represents any single character

E.g.

mysql> select ssn,ename,dnumber,salary from employee

-> where ename like "r%";

+------+-------+---------+--------+

| ssn | ename | dnumber | salary |

Page 12: Experiment No. 1 DBMS Creating a Database

+------+-------+---------+--------+

| 1 | rosy | d01 | 25000 |

| 3 | rohit | d03 | 23000 |

+------+-------+---------+--------+

2 rows in set (0.00 sec)