You are on page 1of 6

SQL Exercises ICT (A/L)

Login to the database server SHOW databases; USE db1; SHOW TABLES; CREATE DATABASE mydb; DROP DATABASE mydb; CREATE TABLE people1 (P_Id int,LastName varchar(8),FirstName char(8)); DESCRIBE people1; INSERT INTO people1 VALUES(234,ali,omar);

Select * from people1; Select p_id,lastname from people1; Select * from people1 where lastname=ali; Select p_id,lastname from people1 where lastname=ali;

INSERT INTO people1 (LastName,FirstName) VALUES(halim,mena); SELECT * FROM people1 WHERE p_id IS NULL

SELECT * FROM people1 WHERE p_id IS NOT NULL INSERT INTO people1 values(22,'muhandhirum','upali'); DELETE FROM people1; DROP TABLE people1; CREATE TABLE people2 (P_Id int NOT NULL, LastName varchar(20) NOT NULL, FirstName varchar(10)) INSERT INTO people2 values(22,'muhandhirum','upali'); INSERT INTO people2 values(22,'muhandhirum','upul'); INSERT INTO people2 values(23,'hathurusinghe','aravinda');

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500

CREATE TABLE people3 ( P_Id int NOT NULL UNIQUE, LastName varchar(15) NOT NULL, FirstName varchar(10))

DESCRIBE people3;

INSERT INTO people3(LastName,FirstName) VALUES(potter,harry); INSERT INTO people3 values(22,'muhandhirum','upali'); INSERT INTO people3 values(22,'muhandhirum','upali'); drop TABLE people3;

CREATE TABLE people4( P_Id int NOT NULL, LastName varchar(15) NOT NULL, FirstName varchar(10)); ALTER TABLE people4 ADD PRIMARY KEY (P_Id); DESCRIBE people4; INSERT INTO people4 values(22,'muhandhirum','upali'); INSERT INTO people4 values(22,'muhandhirum','upali'); INSERT INTO people4(LastName,FirstName) VALUES(silva,nimali); ALTER TABLE people4 ADD age int INSERT INTO people4 VALUES(110,silva,nimali,23); DESCRIBE people4; INSERT INTO people4 VALUES(112,deen,taniya,23); INSERT INTO people4 VALUES(115,bolt,usain,26); ALTER TABLE people4 Drop column age; INSERT INTO people4 VALUES(111,bolt,taniya,23);

ALTER TABLE people4 ADD DateOfBirth date INSERT INTO people4 values(100,'muhandhirum','upul','2013/01/15');

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500

SELECT * FROM allstudents WHERE city='kelaniya'; SELECT * FROM allstudents WHERE city LIKE 'kelaniya';

SELECT * FROM allstudents WHERE city LIKE 'ke%'; SELECT * FROM allstudents WHERE city LIKE '%wa%';

SELECT * FROM allstudents WHERE city NOT LIKE '%wa%';

SELECT * FROM allstudents WHERE city LIKE 'k_ _ _ _ _ _ _ _';

SELECT * FROM allstudents WHERE stname LIKE '_uw%'; SELECT * FROM allstudents WHERE city in ('kelaniya','kadawatha');

SELECT * FROM allstudents WHERE city NOT in ('kelaniya','kadawatha'); SELECT * City FROM allstudents; SELECT DISTINCT City FROM allstudents; SELECT DISTINCT(City) FROM allstudents;

SELECT sum(fee) FROM allstudents WHERE city in ('kelaniya','kadawatha');

SELECT count(fee) FROM allstudents WHERE city in ('kelaniya','kadawatha'); SELECT count(*) FROM allstudents WHERE city in ('kelaniya','kadawatha');

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500

SELECT sum(fee),city FROM allstudents GROUP BY city; SELECT year(dofj) FROM allstudents WHERE city=kelaniya;

SELECT distinct(year(dofj)) FROM allstudents WHERE city='kelaniya';

SELECT month(dofj) FROM allstudents WHERE city='kelaniya';

SELECT * FROM allstudents WHERE fee<10000 and (city='nawala' or city='maharagama');

SELECT * FROM allstudents ORDER BY city;

SELECT * FROM allstudents ORDER BY LastName DESC;

SELECT * FROM allstudents ORDER BY city ASC;

update allemployees set deptno=1001 where name='saman';

UPDATE allemployees SET bsalary=bsalary+bsalary*10/100 WHERE deptno=1000;

Update allemployees set bsalary=bsalary+bsalary*10/100;

update allemployees set age=age+1,bsalary=82000 where empno='100'; CREATE TABLE ex1( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT); DESCRIBE ex1; INSERT INTO ex1(name,age) values('nimal',23); INSERT INTO ex1(name,age) values('gamage',27); SELECT * FROM ex1;

INSERT INTO ex1 values(8,'apsara',23); INSERT INTO ex1 values(5,'anne',23); SELECT * FROM ex1;

INSERT INTO ex1(name,age) values('gamage',27); INSERT INTO ex1 values(8,'apsara',23);

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500

CREATE TABLE teacher( tid char(2) primary key, tname varchar(10), telno char(10)); CREATE TABLE subject(sid char(2),sname varchar(10),duration int,tid char(2)); ALTER TABLE subject add primary key(sid); ---- INSERT INSERT INTO INSERT INTO INSERT INTO some records teacher values('10','saman','0797450200'); teacher values(11,'nimal',0792111333); teacher values(12,'perera',0795100200);

INSERT INTO teacher values(12,'perera',0795100200);

INSERT INSERT INSERT INSERT

INTO INTO INTO INTO

subject subject subject subject

values('EN','English',100,10); values('SC','Science',120,11); values('MT','Maths',110,10); values('HT','History',140,15);

Table joins
SELECT tname,sname FROM teacher,subject WHERE teacher.tid=subject.tid;

SELECT tname,sname FROM teacher,subject WHERE subject.tid=teacher.tid;

SELECT tname,sname FROM teacher as t,subject as s WHERE s.tid=t.tid;

Mysql user permissions


mysql -h 127.0.0.1 -u root p Enter password: SELECT user FROM mysql.user; CREATE USER 'gayan'@'127.0.0.1' IDENTIFIED BY '123'; CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'; SELECT user FROM mysql.user; GRANT SELECT ON db1.allstudents TO 'gayan'; mysql -h 127.0.0.1 -u gayan -p Enter password: SHOW databases; USE db1; SHOW TABLEs;

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500

SELECT * FROM allstudents; UPDATE allstudents SET fee=14000 WHERE stname='namal'; GRANT SELECT,UPDATE ON db1.allstudents TO 'gayan'@'127.0.0.1'; UPDATE allstudents SET fee=14000 WHERE stname='namal'; REVOKE UPDATE ON db1.allstudents FROM 'gayan'@'127.0.0.1'; UPDATE allstudents SET fee=14000 WHERE stname='namal'; SELECT * FROM allstudents;

GRANT

ALL

ON

db1.*

TO

'hostname'@'computer';

REVOKE SELECT ON db1.allstudents FROM 'gayan'@'127.0.0.1'; use mysql; delete FROM user where user='gayan'; SELECT * FROM allstudents;

view
A view is a virtual table. a view is a virtual table based on the result-set of an SQL statement. create, update, and delete a view. create view tempview as select * from temp where city='kelaniya'; DROP VIEW tempview;

INDEX
The CREATE INDEX statement is used to create indexes in tables. An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries. Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against. CREATE INDEX index_name ON table_name (column_name) DROP INDEX index_name ON table_name ALTER TABLE table_name DROP INDEX index_name

Sandamal Vidanagamage

www.ICTPiyasa.lk

0712 300 500