4. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 3 | P a g e
oekg ilialr we;s o;a; iuqodhka ksÍlaIKh'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| company |
| mysql |
| test |
+--------------------+
4 rows in set (0.11 sec)
o;a; iuqodhla ilia lsÍu
mysql> create database student;
Query OK, 1 row affected (0.06 sec)
ilial, o;a; iuqodh Ndú;d lsÍu
mysql> use student;
Database changed
o;a; iuqodh ;=, j.= we;aoehs ksÍlaIKh
mysql> show tables;
Empty set (0.00 sec)
ilial, o;a; iuqodh bj;a lsÍu
mysql> drop database student;
Query OK, 0 rows affected (0.31 sec)
My SQL ys Ndú;djk o;a; j¾.
NumericDataTypes:
• INT - A normal-sized integer
• TINYINT - A very small integer
• SMALLINT - A small integer
• MEDIUMINT - A medium-sized integer
• BIGINT - A large integer
• FLOAT(M,D) - A floating-point number
• DOUBLE(M,D) - A double precision floating-point number
• DECIMAL(M,D) - An unpacked floating-point number
5. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 4 | P a g e
DateandTimeTypes:
• DATE - A date in YYYY-MM-DD format
• DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format
• TIME - Stores the time in HH:MM:SS format.
• YEAR(M) - Stores a year in 2-digit or 4-digit format.
StringTypes:
• CHAR(M) - A fixed-length string between 1 and 255 characters
• VARCHAR(M) - A variable-length string between 1 and 255 characters in length
j.=jla ks¾udKh lsÍu'
mysql> create database ex1;
Query OK, 1 row affected (0.00 sec)
mysql> use ex1;
Database changed
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Query OK, 0 rows affected (0.22 sec)
ilia l, j.=jla bj;a lsÍu'
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| courses |
| stu_details |
+-------------------+
mysql> drop table courses;
Query OK, 0 rows affected (0.15 sec)
j.=j ms<sn`o úia;r n,d.ekSu'
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 5 | P a g e
j.=jlg m%d:ñl h;=rla tla lsÍu'
j.=jlg m%d:ñl h;=rla tla l< yels wdldr folla mj;S'
1' j.=j ks¾udKh lrk wjia:dfõ§
2' j.=j iïmQ¾Kfhka ks¾udKh lr wjidkfha§
j.=j ks¾udKh lrk wjia:dfõ§ j.=jlg m%d:ñl h;=rla tla lsÍu'
mysql> create database student;
Query OK, 1 row affected (0.00 sec)
mysql> use student;
Database changed
mysql> create table stu_details(sti int(4) not null, name varchar(20), address varchar(40), class
varchar(3), primary key (sti));
Query OK, 0 rows affected (0.14 sec)
mysql> describe stu_details;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sti | int(4) | NO | PRI | | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| class | varchar(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
j.=j ks¾udKh l, miqj m%d:ñl h;=rla tla lsÍu'
mysql> create table courses(cid int(4) not null, name varchar(20), duration varchar(10), price int(5));
Query OK, 0 rows affected (0.14 sec)
mysql> describe courses;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| cid | int(4) | NO | | | |
| name | varchar(20) | YES | | NULL | |
| duration | varchar(10) | YES | | NULL | |
| price | int(5) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 6 | P a g e
mysql> alter table courses add primary key (cid);
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe courses;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| cid | int(4) | NO | PRI | | |
| name | varchar(20) | YES | | NULL | |
| duration | varchar(10) | YES | | NULL | |
| price | int(5) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
j.=jl m%d:ñl h;=r bj;a lsÍu'
mysql> alter table stu_details drop primary key;
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe stu_details;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| sti | int(4) | NO | | | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| class | varchar(3) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
j.=jlg wd.ka;=l h;=rla tla lsÍu'
j.=jlg wd.ka;=l h;=rla tla l< yels wdldr folla mj;S'
1' j.=j ks¾udKh lrk wjia:dfõ§
2' j.=j iïmQ¾Kfhka ks¾udKh lr wjidkfha§
8. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 7 | P a g e
j.=j ks¾udKh lrk wjia:dfõ§ j.=jlg wd.ka;=l h;=rla tla lsÍu'
mysql> create table course(cid int(4) not null,cname varchar(20), duration varchar(10), price
int(10),sti int(4), primary key (cid), foreign key (sti) references stu_details(sti));
Query OK, 0 rows affected (0.13 sec)
mysql> describe course;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| cid | int(4) | NO | PRI | | |
| cname | varchar(20) | YES | | NULL | |
| duration | varchar(10) | YES | | NULL | |
| price | int(10) | YES | | NULL | |
| sti | int(4) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
j.=j iïmQ¾Kfhka ks¾udKh l, miqj wd.ka;=l h;=rla tla lsÍu'
mysql> create table sport(spid int(4) not null, name varchar(20), sti int(4));
Query OK, 0 rows affected (0.39 sec)
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| course |
| sport |
| stu_details |
+-------------------+
3 rows in set (0.00 sec)
mysql> alter table sport add foreign key (spid) references stu_details(sti);
Query OK, 0 rows affected (0.22 sec)
Records: 0 Duplicates: 0 Warnings: 0
j.=jl wd.ka;=l h;=r bj;a lsÍu'
mysql> alter table sport drop foreign key spid;
ERROR 1025 (HY000): Error on rename of '.studentsport' to '.student#sql2-c34-2' (errno: 152)
13. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 12 | P a g e
+-----+--------------------+
| sti | name |
+-----+--------------------+
| 1 | Dilshan Tharaka |
| 2 | Nadun Madusanka |
| 3 | Tharaka Lakmal |
| 4 | Isuru Yuresh |
| 5 | Tishan Edirisinghe |
| 6 | Thilanka Sachith |
+-----+--------------------+
mysql> select cid, cname, price from course;
+-----+--------+-------+
| cid | cname | price |
+-----+--------+-------+
| 1 | DICS | 22000 |
| 2 | DTP | 19000 |
| 3 | Office | 12000 |
+-----+--------+-------+
Where wka;¾jdlH (Clause) Ndú;h
Wm,elshdkhl (Record) we;s ish¨ o;a; tljr m%;sodkh lsÍu'
mysql> select * from stu_details where name='Dilshan Tharaka';
+-----+-----------------+------------+-------+
| sti | name | address | class |
+-----+-----------------+------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
+-----+-----------------+------------+-------+
1 row in set (0.10 sec)
mysql> select sti,name from stu_details where name='Dilshan Tharaka';
+-----+-----------------+
| sti | name |
+-----+-----------------+
| 1 | Dilshan Tharaka |
+-----+-----------------+
1 row in set (0.00 sec)
mysql> select * from stu_details where address='polonnaruwa' and class='G12';
+-----+--------------------+-------------+-------+
| sti | name | address | class |
+-----+--------------------+-------------+-------+
| 3 | Tharaka Lakmal | Polonnaruwa | G12 |
| 5 | Tishan Edirisinghe | Polonnaruwa | G12 |
+-----+--------------------+-------------+-------+
2 rows in set (0.00 sec)
14. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 13 | P a g e
mysql> select * from stu_details where address='polonnaruwa' or class='G12';
+-----+--------------------+-------------+-------+
| sti | name | address | class |
+-----+--------------------+-------------+-------+
| 3 | Tharaka Lakmal | Polonnaruwa | G12 |
| 4 | Isuru Yuresh | Polonnaruwa | G13 |
| 5 | Tishan Edirisinghe | Polonnaruwa | G12 |
+-----+--------------------+-------------+-------+
3 rows in set (0.00 sec)
mysql> select * from stu_details where not (address='polonnaruwa');
+-----+------------------+--------------+-------+
| sti | name | address | class |
+-----+------------------+--------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
| 2 | Nadun Madusanka | Kaduruwela | G13 |
| 6 | Thilanka Sachith | Hingurakgoda | G13 |
+-----+------------------+--------------+-------+
3 rows in set (0.06 sec)
mysql> select * from stu_details where (address='polonnaruwa'and class='G13') or name='Tharaka
Lakmal';
+-----+----------------+-------------+-------+
| sti | name | address | class |
+-----+----------------+-------------+-------+
| 3 | Tharaka Lakmal | Polonnaruwa | G12 |
| 4 | Isuru Yuresh | Polonnaruwa | G13 |
+-----+----------------+-------------+-------+
2 rows in set (0.00 sec)
mejereu'
name owner species sex birth death
Fluffy Harold cat f 1993-02-04
Claws Gwen cat m 1994-03-17
Buffy Harold dog f 1989-05-13
Fang Benny dog m 1990-08-27
Bowser Diane dog m 1998-08-31 1995-07-29
Chirpy Gwen bird f 1998-09-11
Whistler Gwen bird 1997-12-09
Slim Benny snake m 1996-04-29
15. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 14 | P a g e
1' by; úia;r j,g wkqj pet f,i j.=jla ks¾udKh i`oyd wjYH My SQL fla;h ,shkak'
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species
VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
2. Tn úiska ks¾udKh l< j.=j ks¾udKh ù we;a±hs ksÍlaIKh lsÍug Ndú;d lrk fla;h
l=ulao@
mysql> show tables;
+------------------+
| Tables_in_test |
+------------------+
| pet |
+------------------+
1 row in set (0.01 sec)
3. j.=j úia;rd;aulj ksÍlaIKh i`oyd ,súh hq;= fla;h l=ulao@
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)
4. by; j.=jg m<uq Wm,elshdkh tla lsÍu i`oyd ,súh hq;= fla;h l=ulao@
INSERT INTO pet VALUES ('Fluffy', 'Harold', 'cat', 'f', '1999-02-04', NULL);
5. ish¨ o;a; tlal, miq j.=j ±lsh yels wdldrh ksÍlaIKh i`oyd ,súh hq;= fla;h l=ulao@
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1999-02-04 | NULL |
| Claws | Gwen | cat | f | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1999-08-27 | NULL |
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)
16. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 15 | P a g e
6. my; fla; Ndú;fhka isÿjk ksÍlaIKhka ,shkak'
mysql> SELECT * FROM pet WHERE name = "Bowser";
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
1 row in set (0.00 sec)
SELECT * FROM pet WHERE birth >= "1998-1-1";
SELECT * FROM pet WHERE species = "dog" AND sex = "f";
SELECT * FROM pet WHERE species = "snake" OR species = "bird";
mysql> select name, birth from pet;
+----------+------------+
| name | birth |
+----------+------------+
| Fluffy | 1999-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1999-08-27 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
+----------+------------+
17. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 16 | P a g e
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Fluffy | 1999-02-04 |
| Fang | 1999-08-27 |
+----------+------------+
8 rows in set (0.02 sec)
mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
+----------+------------+
| name | birth |
+----------+------------+
| Fang | 1999-08-27 |
| Fluffy | 1999-02-04 |
| Chirpy | 1998-09-11 |
| Bowser | 1998-08-31 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
mysql> SELECT * FROM pet WHERE name LIKE "b%";
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
mysql> SELECT * FROM pet WHERE name LIKE "%fy";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
18. wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 17 | P a g e
mysql> SELECT * FROM pet WHERE name LIKE "%w%";
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
mysql> SELECT * FROM pet WHERE name LIKE "_____";
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
mysql> SELECT * FROM pet WHERE name REGEXP "^b";
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
mysql> SELECT * FROM pet WHERE name REGEXP "fy$";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9 |
+----------+
j.=jl we;s Wm,elshla (Field) bj;a lsÍu'
mysql> alter table stu_details drop column class;
Query OK, 6 rows affected (0.27 sec)
Records: 6 Duplicates: 0 Warnings: 0