You are on page 1of 6

SQL - Practical

Double Click

MySql Icon

Type the Sql Administrator Password [ now you will get a prompt liks Mysql> ] then you
can start ur practical.

1. To Check the Correct Version

SELECT

VERSION();

Output
+-------------------+
| VERSION() |
+-------------------+
| 5.0.3-alpha-debug |
+-------------------+
1 row in set (0.00 sec)
2. CREATE a USER
Create

3.

user

fsd@localhost identified

by wsu12345;

Data base
a. To View the Databases
Show databases;
b. To Create a New database
create database

mscitdb;

c. To delete the Data Base


Drop database

mscitdb;

d. To Use the Database


Use mscitdb;

e. set permission to the user on a - data base


GRANT INSERT,UPDATE ON mscitdb TO
Output
[If successful, MySQL will reply with:
Query OK, 1 row affected (0.00 sec)]

f. To Cancel all permission to a user


REVOKE ALL on mscit

TO fsd;

Privileges you can GRANT and REVOKE


Type of Operation

Statement

View table data

SELECT

Add rows to a table

INSERT

Modify data in a table

UPDATE

Delete rows

DELETE

Reference a table from another

REFERENCES

Drop tables

DROP

Create or Alter tables

CREATE, ALTER

Index a table by an expression

INDEX

All privileges

ALL

fsd;

4.Tables
a. To create a Table [ first use a Database before create a table .Ex- use mscitdb; ]
Create table msctbl ( name char(30), idno int, mark1
b. To view the tables

int);

show tables;

c.

To View the Table Structure

Describe mscittbl;

d.

To View the Assigned Keys

show keys from mscittbl;

c.

To delete a table

drop table mscittbl;

d.

To insert data into a Table

insert into mscittbl values (hafdamu", 1, 98);


e.

To Delete a record from a table


Syntax :-

DELETE FROM tablename WHERE (rule)


Ex:

delete from mscittbl where rollno= 5;

f. To update the data in a table


Syntax : =>

UPDATE table_name SET col_name = expression WHERE (rule)

Example =>

UPDATE mscittbl SET mark1 = 88 WHERE rollno = 5 ;

g. To View the Data in a Table [ Execute queries ]


Ex:
USE bsccsdb ;
SELECT * FROM mscittbl ;
To view the students those who are got >80 in compilerDesign.

SELECT * FROM mscittbl WHERE cdmsmark >80;

5. Create a view
CREATE VIEW mscitview ON mscittbl AS select idno, name FROM mscittbl;

6.

Creating a Table from an Existing Table


Syntax :CREATE TABLE NEW_TABLE(FIELD1, FIELD2, FIELD3)
AS (SELECT FIELD1, FIELD2, FIELD3FROM OLD_TABLE <WHERE...>
Ex
Create table mscit1(sno int) as (select

7.

idno , name from mscittbl ) ;

Inserting the data from an Existing Table into a newly created table
Syntax :INSERT NEW_TABLE SELECT <field1, field2... | *> from OLD_TABLE
<WHERE...>;
Ex:

Insert mscit1 select idno, name from mscittbl;

8. ALTER the TABLE


Under alter we can do the Following : -

Add , Delete , Modify & Insert

To Change the name field size :


ALTER TABLE mscittbl MODIFY NAME CHAR(100);
To Add a new Field to the table:ALTER TABLE mscittbl ADD projmark int ;

9. Using special functions in sql


[ Sum ,avg, max ,min ,mod , power , SQRT , LOWER ,UPPER , SIGN
,LTRIM ,RTRIM
COS, COSH, SIN, TAN,EXP , Mod , POWER , SQRT and USER ]
Ex.: SELECT NAME, AVG(mark1) FROM mscittbl ;

10.

Sort the Table

Using the following clauses we can sort the Records in the table.
WHERE l STARTING WITH l ORDER BY l GROUP BY l HAVING
Ex.: ->

SELECT *

11.

FROM mscittbl GROUP BY NAME;

Joining 2 Table

To View the Records of 2 or more table


SELECT

a.NAME, b.name

FROM

bsccstbl

a, msctbl

b;

12.

DISTINCT - used to Sort & Remove the Duplicate


Records
select DISTINCT totalmark from mscittbl;

13.

String Concatenation

SELECT LASTNAME || ','


mscittbl;
NAME
Ronoldo ,christiyano

||

FIRSTNAME

NAME

FROM

MEZA , Messy

14.

UNION and UNION ALL

UNION returns the results of two


duplicate rows.
Syntax : table2;

15.

queries minus the

SELECT NAME FROM tabl1 UNION

SELECT NAME FROM

INTERSECT

INTERSECT returns only the rows found by both queries. The next
SELECT statement shows the list of players who play on both teams:

Syntax : SELECT * FROM table1


table2;

INTERSECT

SELECT * FROM

You might also like