My+sql 1 1
My+sql 1 1
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'
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'
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'
select
-> user()
-> ,
-> current_Date;
+----------------+--------------+
| user() | current_Date |
+----------------+--------------+
| root@localhost | 2017-03-21 |
+----------------+--------------+
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'
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.
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)
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í'
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'
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
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
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)
fuys (*) ;rej ixfla;h u.ska ish¨u o;a; (All) hk w¾:h w`.jhs'
+---------+-------------+------+-----+---------+-------+
| 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)
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 |
+------+----------------+-------------+