0% found this document useful (0 votes)
27 views21 pages

My+sql 1 1

MySQL is a relational database management system. It allows storage, retrieval and management of data in the form of related tables. Some key features of MySQL include that it is open source, supports a number of programming languages, and provides powerful data querying and manipulation functionality.

Uploaded by

thejaka aloka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views21 pages

My+sql 1 1

MySQL is a relational database management system. It allows storage, retrieval and management of data in the form of related tables. Some key features of MySQL include that it is open source, supports a number of programming languages, and provides powerful data querying and manipulation functionality.

Uploaded by

thejaka aloka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

úHqy.

; úuiqï ni - SQL (Structured Query Language)

My SQL hkq ckms%h újD; fla; o;a; iuqod l<uKdlrK uDÿldx.hls' fuh ixj¾Okh lsÍu iy
kj ixialrK t<s±laùï isÿlrkq ,nkafka MySQ LAB úisk's
My SQL hkq iïnkaê; o;a; iuqodh l<uKdlrK uDÿldx.hls' fndfyda mß.Kl l%uf,aLk iu.
ls%hd;aul l< yel' (Wod - C, C++, Eiffel, Java, Perl, PHP, Python, Ruby) fõ.j;a" kuHYS,S
"wdrlaIdj iys; iy úYd, o;a; m%udKhla .nvdl< yels o;a; iuqodh l<uKdlrK moaO;shls'
My SQL úúO fufyhqï moaO;s j, ls%hd;aul l< yels w;r fjí wvú ks¾udKfha§ nyq,j Ndú;d
lrhs'

MySQL m%ldYk tys l%shdj,sh wkqj j¾. lrkq ,efí'

• o;a; w¾: ksrEmK NdIdj (Data Definition Language- DDL)


• o;a; fufyhùu i`oyd NdIdj (Data Manipulation Language- DML)
• o;a; md,k NdIdj (Data Control Language- DCL)
• oDIH w¾: ksrEmk NdIdj (View Definition Language- VDL)

o;a; w¾: ksrEmK NdIdj (Data Definition Language- DDL)

fuu fldgi Ndú;fhka o;a; mdolhla (Database) ks¾udKh lsÍu" uelSu" j.=j, jHqyh ieliSu"
Wm,eÅ (Field) fjkia lsÍu" Wm,eÅ uelSu wdÈh isÿl, yel'

My SQL ia:dmkh lr .ekSu'

My SQL uDÿldx.h wka;¾cd,fhka fkdñf,a nd.; lr.; yel' ta i`oyd http://dev.mysql.com


fhduqj Ndú;d l< yel' thg wu;rj WAMP, XAMPP, Workbench jeks uDÿldx. Ndú;d l<
yel'

My SQL újD; lr .ekSu'

Start All programs MySQL MySQL Command Line Client


My SQL ia:dmkh lsÍfïoS ,ndÿka uqrmoh ,ndfokak'
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 241 to server version: 3.23.49
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

My SQL ;=<ska bj;aùu'

mysql> QUIT
mysql> exit

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 1|Page
My SQL wkqjdoh iy oskh ksÍlaIKh'

mysql> SELECT VERSION(), CURRENT_DATE;


+---------------------+--------------+
| VERSION() | CURRENT_DATE |
+---------------------+--------------+
| 5.0.27-community-nt | 2017-03-21 |
+---------------------+--------------+

My SQL fla; ,sùfïoS lemsg,a isïm,a wlaIr fíoh n, fkdmj;S'

mysql> SELECT VERSION(), CURRENT_DATE;


mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

My SQL ;=<ska .Ks; l¾u isÿlsÍu'

mysql> SELECT SIN(PI()/4), (4+1)*5;


+-------------+---------+
| SIN(PI()/4) | (4+1)*5 |
+-------------+---------+
| 0.707107 | 25 |
+-------------+---------+
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2017-03-21 09:47:29 |
+---------------------+
1 row in set (0.00 sec)

select
-> user()
-> ,
-> current_Date;
+----------------+--------------+
| user() | current_Date |
+----------------+--------------+
| root@localhost | 2017-03-21 |
+----------------+--------------+

My SQL úOdkhlska bj;aùu'


mysql> SELECT
-> USER()
-> \c
mysql>

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 2|Page
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¾.


Numeric Data Types:
• 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

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 3|Page
Date and Time Types:
• 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.


String Types:
• 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 | |
+---------+-------------+------+-----+---------+-------+
wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 4|Page
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 | |
+----------+-------------+------+-----+---------+-------+
wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 5|Page
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§

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 6|Page
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 '.\student\sport' to '.\student\#sql2-c34-2' (errno: 152)

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 7|Page
fuysoS iSud ixfla;ho (Constraint) we;=,;a lr wd.ka;=l h;=r bj;a l< hq;=h' tfia fkdue;s
jqjfyd;a by; wdldrfha fodaY mKsúvhla ,efí'

iSud ixfla;h (Constraint) ksÍlaIKh'


show create table sport;

| sport | CREATE TABLE `sport` (


`spid` int(4) NOT NULL,
`name` varchar(20) default NULL,
`sti` int(4) default NULL,
PRIMARY KEY (`spid`),
CONSTRAINT `sport_ibfk_1` FOREIGN KEY (`spid`) REFERENCES `stu_details` (`sti`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

mysql> alter table sport drop foreign key sport_ibfk_1;


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

wd.ka;=l h;=r bj;aù we;aoehs ksÍlaIKh'


mysql> show create table sport;

| sport | CREATE TABLE `sport` (


`spid` int(4) NOT NULL,
`name` varchar(20) default NULL,
`sti` int(4) default NULL,
PRIMARY KEY (`spid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)

jHQy.; úi`ÿï NdIdj fhdod .ksñka o;a; fufyhùu


(Data Manipulation Language- DML)

DML NdIdj Ndú;dlr j.=j,g my; i`oyka úodk ,ndfokq ,efí'


• j.=jlg w¨;a Wm,eÅhdk(Record) we;=,;a lsÍu - INSERT
• j.=jl Wm,eÅhdk hdj;ald,Sk lsÍu - UPDATE
• j.=jl wvx.= Wm,eÅhdk bj;a lsÍu – DELETE
• j.=jl wvx.= Wm,eÅhdk kej; ,nd.ekSu – SELECT

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 8|Page
j.=jlg o;a; we;=,;a lsÍu (Insert Query)

My SQL Ndú;fhka ilial, j.=jlg o;a; tla l< yels wdldr lsysmhls'

1' Insert into úOdkh Ndú;fhka'

mysql> show tables;


+-------------------+
| Tables_in_student |
+-------------------+
| course |
| sport |
| stu_details |
+-------------------+
3 rows in set (0.00 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)

mysql> insert into stu_details values(1,'Dilshan Tharaka', 'Kaduruwela', 'G13');


Query OK, 1 row affected (0.09 sec)

mysql> select * from stu_details;


+-----+-----------------+------------+-------+
| sti | name | address | class |
+-----+-----------------+------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
+-----+-----------------+------------+-------+
2' Set úOdkh Ndú;fhka'

mysql> insert into stu_details set sti=2,name='Nadun Madusanka', address='Kaduruwela',class='G13';


Query OK, 1 row affected (0.06 sec)

mysql> select * from stu_details;


+-----+-----------------+------------+-------+
| sti | name | address | class |
+-----+-----------------+------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
| 2 | Nadun Madusanka | Kaduruwela | G13 |
+-----+-----------------+------------+-------+

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 9|Page
j.=jlg o;a; lsysmhla tljr tlalsÍu'

mysql> insert into stu_details (sti, name, address, class) values(3,'Tharaka Lakmal', 'Polonnaruwa',
'G12'), (4,'Isuru Yuresh','Polonnaruwa','G13'), (5,'Tishan Edirisinghe', 'Polonnaruwa', 'G12'), (6,
'Thilanka Sachith', 'Hingurakgoda', 'G13');
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select * from stu_details;


+-----+--------------------+--------------+-------+
| sti | name | address | class |
+-----+--------------------+--------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
| 2 | Nadun Madusanka | Kaduruwela | G13 |
| 3 | Tharaka Lakmal | Polonnaruwa | G12 |
| 4 | Isuru Yuresh | Polonnaruwa | G13 |
| 5 | Tishan Edirisinghe | Polonnaruwa | G12 |
| 6 | Thilanka Sachith | Hingurakgoda | G13 |
+-----+--------------------+--------------+-------+
6 rows in set (0.06 sec)

3' text f.dkqjla Ndú;fhka'

fuys§ j.=jlg tla l, hq;= o;a; text f.dkqjl igyka lr .;hq;=hs' miqj tu f.dkqj ;=, we;s
o;a; j.=jlg tla lr .; yel'

mysql> LOAD DATA LOCAL INFILE 'C:/Users/Aruna Herath/Desktop/course.txt' into table course;
Query OK, 3 rows affected (0.06 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from course;


+-----+--------+----------+-------+------+
| cid | cname | duration | price | sti |
+-----+--------+----------+-------+------+
| 1 | DICS | 4 | 22000 | 1 |
| 2 | DTP | 4 | 19000 | 4 |
| 3 | Office | 3 | 12000 | 1 |
+-----+--------+----------+-------+------+
3 rows in set (0.00 sec)

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 10 | P a g e


j.=jl we;s o;a; keje; ,nd.ekSu' (Select Query)

j.=jl we;s o;a; úúOdldrfhka ,nd.; yel' fï i`oyd Select úOdkh Ndú;d lrhs' j.=jl we;s
o;a; keje; ,ndfokqfha j.=jla wdldrfhkau fõ'
iïmQ¾K j.=fõu we;s o;a; tljr ,nd.ekSu'
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| course |
| sport |
| stu_details |
+-------------------+
3 rows in set (0.00 sec)

mysql> select * from stu_Details;


+-----+--------------------+--------------+-------+
| sti | name | address | class |
+-----+--------------------+--------------+-------+
| 1 | Dilshan Tharaka | Kaduruwela | G13 |
| 2 | Nadun Madusanka | Kaduruwela | G13 |
| 3 | Tharaka Lakmal | Polonnaruwa | G12 |
| 4 | Isuru Yuresh | Polonnaruwa | G13 |
| 5 | Tishan Edirisinghe | Polonnaruwa | G12 |
| 6 | Thilanka Sachith | Hingurakgoda | G13 |
+-----+--------------------+--------------+-------+
6 rows in set (0.00 sec)
mysql> select * from course;
+-----+--------+----------+-------+------+
| cid | cname | duration | price | sti |
+-----+--------+----------+-------+------+
| 1 | DICS | 4 | 22000 | 1 |
| 2 | DTP | 4 | 19000 | 4 |
| 3 | Office | 3 | 12000 | 1 |
+-----+--------+----------+-------+------+
3 rows in set (0.00 sec)

fuys (*) ;rej ixfla;h u.ska ish¨u o;a; (All) hk w¾:h w`.jhs'

j.=jl we;s Wm,els (Field) w;ßka lSmhl o;a; ,nd.ekSu'

mysql> select sti, name from stu_Details;

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 11 | 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)

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 12 | 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

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 13 | 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)

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 14 | 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 |
+----------+------------+

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 15 | 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 |
+--------+--------+---------+------+------------+-------+

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 16 | 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

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 17 | P a g e


j.=jlg kj Wm,elshla (Field) tla lsÍu'

mysql> alter table stu_details add column class varchar(3);


Query OK, 6 rows affected (0.26 sec)
Records: 6 Duplicates: 0 Warnings: 0

j.=jl we;s Wm,elshla (Field) hdj;ald,Sk lsÍu'

mysql> alter table stu_details change class grade varchar(3);


Query OK, 6 rows affected (0.20 sec)
Records: 6 Duplicates: 0 Warnings: 0

j.=jl we;s Wm,elshl (Field) o;a; j¾.h fjkia lsÍu'

mysql> alter table stu_details modify grade int(3);


Query OK, 6 rows affected (0.29 sec)
Records: 6 Duplicates: 0 Warnings: 0

j.=jl we;s Wm,elshdkh (Record) hdj;a ld,Sk lsÍu'


mysql> update stu_details set name='D.Tharaka', Address='Kaduruwela', grade=13 where sti=1;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stu_details;
+-----+--------------------+--------------+-------+
| sti | name | address | grade |
+-----+--------------------+--------------+-------+
| 1 | D.Tharaka | Kaduruwela | 13 |
| 2 | Nadun Madusanka | Kaduruwela | NULL |
| 3 | Tharaka Lakmal | Polonnaruwa | NULL |
| 4 | Isuru Yuresh | Polonnaruwa | NULL |
| 5 | Tishan Edirisinghe | Polonnaruwa | NULL |
| 6 | Thilanka Sachith | Hingurakgoda | NULL |

j.=jl we;s Wm,elshdkh (Record) bj;a lsÍu'


mysql> delete from stu_details where sti=6;
Query OK, 1 row affected (0.10 sec)
mysql> select * from stu_details;
+-----+--------------------+-------------+-------+
| sti | name | address | grade |
+-----+--------------------+-------------+-------+
| 1 | D.Tharaka | Kaduruwela | 13 |
| 2 | Nadun Madusanka | Kaduruwela | NULL |
| 3 | Tharaka Lakmal | Polonnaruwa | NULL |
| 4 | Isuru Yuresh | Polonnaruwa | NULL |
| 5 | Tishan Edirisinghe | Polonnaruwa | NULL |
+-----+--------------------+-------------+-------+
wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 18 | P a g e
Cascade úOdkh Ndú;d lsÍu'

j.=jl fyda j.= lsysmhl wd.ka;=l h;=r Ndú;fhka iïnkaOlr we;s wjia:djl§ tu iEu
j.=jlg we;s Wm,elshdkhka tlajr bj;a lsÍu i`oyd fuu úOdkh Ndú;d l< yelsh' fï i`oyd
j.= ilia lsÍu wdrïN lrk wjia:dfõ§u on delete cascade úOdkh ,ndÈh hq;=hs'
mysql> create table students(stid int(4) not null references sport(stid), name varchar(20),address
varchar(40), primary key (stid));
Query OK, 0 rows affected (0.12 sec)
mysql> describe students;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stid | int(4) | NO | PRI | | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
mysql> create table sport(spid int(4) not null, name varchar(20),stid int(4), primary key (spid), foreign
key(stid) references students(stid) on delete cascade);
Query OK, 0 rows affected (0.14 sec)
mysql> describe sport;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| spid | int(4) | NO | PRI | | |
| name | varchar(20) | YES | | NULL | |
| stid | int(4) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> insert into students values(101, 'Pasindu Piumal','Polonnaruwa');
Query OK, 1 row affected (0.04 sec)
mysql> insert into sport values(1, 'Cricket',101);
Query OK, 1 row affected (0.09 sec)
mysql> select * from students;
+------+----------------+-------------+
| stid | name | address |
+------+----------------+-------------+
| 101 | Pasindu Piumal | Polonnaruwa |
+------+----------------+-------------+

mysql> select * from sport;


+------+---------+------+
| spid | name | stid |
+------+---------+------+
| 1 | Cricket | 101 |
+------+---------+------+

wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 19 | P a g e


tla j.=jla muKla Ndú;fhka j.= foflysu Wm,elshdk bj;a lsÍu'
mysql> delete from students where name='Pasindu Piumal';
Query OK, 1 row affected (0.10 sec)

mysql> select * from sport;


Empty set (0.00 sec)

mysql> select * from students;


Empty set (0.00 sec)

j.=jl we;s ish¨ o;a; bj;a lsÍu'

mysql> delete from students;


Query OK, 1 row affected (0.09 sec)

j.= lsysmhl we;s o;a; ,nd.ekSu'

mysql> select * from students,sport;


+------+----------------+-------------+------+---------+------+
| stid | name | address | spid | name | stid |
+------+----------------+-------------+------+---------+------+
| 101 | Pasindu Piumal | Polonnaruwa | 1 | Cricket | 101 |
+------+----------------+-------------+------+---------+------+

mysql> select * from students,sport where students.stid=sport.stid;


+------+----------------+-------------+------+---------+------+
| stid | name | address | spid | name | stid |
+------+----------------+-------------+------+---------+------+
| 101 | Pasindu Piumal | Polonnaruwa | 1 | Cricket | 101 |
+------+----------------+-------------+------+---------+------+

mysql> select students.stid, sport.spid from students,sport where students.stid=sport.stid;


+------+------+
| stid | spid |
+------+------+
| 101 | 1 |
+------+------+

mysql> select students.stid, students.name, sport.spid from students, sport where


students.stid=sport.stid;
+------+----------------+------+
| stid | name | spid |
+------+----------------+------+
| 101 | Pasindu Piumal | 1 |
+------+----------------+------+
1 row in set (0.00 sec)
wreK fyar;a w'fmd'i' Wiia fm< f;dr;=re yd ikaksfõok ;dlaIKh - My SQL 20 | P a g e

You might also like