You are on page 1of 38

COMPUTER SYSTEM ESSENTIALS

19CSE101

Amrita School of Computing

Amrita Vishwa Vidyapeetham


Chennai - 601 103, Tamil Nadu, India.
BONAFIDE CERTIFICATE
University Reg. No : CH.EN.U4CSE22179

This is to certify that this is a bonafide record work done by,

PRITHIVI .R.S
studying B.Tech Computer Science Engineering

Internal Examiner! Internal Examiner2

2
INDEX

Experiment No Date Title Page No

1 16/12/2022 Linux Exercise 1 3

2 16/12/2022 Linux Exercise 2 14

3 30/12/2022 Linux Exercise 3 18

4 20/1/2023 MySQL Queries 24


LAB EXERCISE-01

8
1. Display the path of your current directory.

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-
PC:~$ pwd
/home/asecomputerlab

2. Make a new directory named main.

aseconputerlab@aseconputerlab-HP-ProDesk-400-G7-Microtower-PC:«$ mkdir min

3. Now change to the directory main.


aseccmputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:cd main

4. Make the directories in the following hierarchy using a single


command.
Dirl —Dir2—Dir3

asecoaputerlib^asecaipiiterlib-fiP-ProDe3H00-57-Micr
5. Print the path of the current directory.

aseccmputerlab@&secomputerlab-HP-ProDesk-4QQ-G7-Microtower-
PC:«/main$ pwd
/home/asecomputerlab/main

6. Go to Dir3 using a single command.

asecanpiiterlab(}asecaiiputerlab-HP-ProDesk-400-G7-Microtower-PC:*/imn$ cd dirl/dir

7. Create a new file demol, type and save the


following contents, This is my first
file in shell. ^
I can edit this file!!!
asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~/niain/dirl/dir2/dir3$ cat >
demol This is my first file in shell.
I car, edit this file!!!_

8. Create a new file demo2, type and save the


following contents, Hi!!! This is the
second file.
I am doing shell commands.
I can edit this file!!!

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PO/main/dirl/dir2/dii3? cat > demo2


Hi!.'.'This is the second file.
I am doing shell commands.
I can edit this file!!!

9. Display the contents of file demol in terminal.

aseconputerlab@aseconputerlab-HP-ProDesk-400-G7-Microtom-PC: ~/inain/dirl/dir2/dir3$ cat


demol This is ray first file in shell.
I can edit this f i l e ! ! !

10. List the files and folders present in Dir3.

aseconputerlabfasecoiq)uterlab-HP-ProDesH00-G7-
MicrotoKi-PC:*/iMin/dirl/dir2/dir3$ Is
demol demo2
11. Go to Dir 2.

asecoiiputerlabSi3ecaipiiterlab-HP-Pro!}esH(}()-G7-MicrotovBr-PC>/m
12. Go to your home directory.

bsecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~/nmn/dirl/dir2$ cd *■

10
13. Stay where you are, and list the contents of Dir3.

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~$ Is main/dirl/dir2/dir3
demol demo2

14. List all the files (including hidden files) in your home
directory.

aseconputerlabQaseccaputerlab-BP-ProDesk-400-G7-Microtower-PC: Is -al
total 244
drwxr-xr-x 38 asecomputerlab asecomputerlab 4096 Dec 21 15:54 .
drwxr-xr-x 5 root root 4096 Apr 7 2022 ..
drwxrwxr-x 2 asecomputerlab asecomputerlab 4096 Oct 21 20:31 .android
-rw------- 1 asecomputerlab asecomputerlab 11113 Dec 14 11:33 ,bash_history
-rw-r—r— 1 asecomputerlab asecomputerlab 220 Dec 24 2021 .bash_logout
-rw-r—r— 1 asecomputerlab asecomputerlab 3954 Apr 1 2022 .bashrc
drwx------ 25 asecomputerlab asecomputerlab 4096 Nov 25 11:04 .cache
drwxrwxr-x 2 asecomputerlab asecomputerlab 4096 Dec 8 15:36
ch.en.u4csel9005
drwxrwxr-x 2 asecomputerlab asecomputerlab 4096 Dec 8 19:59
ch.en.u4cse21233
drwxrwxr-x 2 asecomputerlab asecomputerlab 4096 Dec 21 15:56
ch.en.u4cse22053
drwx------ 11 asecomputerlab asecomputerlab 4096 Nov 25 17:04 .config
drwxrwxr-x 4 asecomputerlab asecomputerlab 4096 Dec 2 16:15 cse
drwxrwxr-x 2 asecomputerlab asecomputerlab 4096 Nov 18 16:22 cse2
drwx------ 3 root root 4096 Oct 20 19:37 .dbus
15. Create 2a new
drwxr-xr-x file testl, type
asecomputerlab and save4096
asecomputerlab the contents
Dec 14 17:33 Desktop

into your file. I am working with linux


shell.
Good bye

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:-$ cat >testl I am


working with linux shell.
Goodbye_

16. Copy the contents of testl to test2 in the same directory.

jsecofUterJatJasecc!iiipiJterJal)-iiP-ProM-Jll()-57-l!icrotci
11
17. Rename test2 as test3.

asecomputerldbfasecomputerlab-HP-ProDesk-400-G7-MicrotoHe

18. Determine the file type of test3.

asecomputerlab@dseconputerlab-HP-ProDesk-400-G7-Microtower-PC:*$ file test3 test3:


ASCII text

19. Move the file test3 to the directory Dir3.

asecoo|)iiterlabjasecflfuterlab-flP-ProDes]k-40£)-fi7-Microtowr-

20. Create a file count, with content one to twenty in words with
one line having only one number using a single command.

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~$ cat >count


one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty

12
21. Copy the file count to count2 using cat command.

aseconputerlabfaseconpiiterlab-HP-ProDesk-400-G7-Microtoifer-P

22. Create another file count3 with numbers twenty one to twenty
five (in five lines).
asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:-$ cat >count3
twenty one
twenty two
twenty three
twenty four
twenty five

23. Concatenate the contents of files count2 and count3 and write it
into the file countfinal.

24. Remove the files demol and demo2 in directory Dir3.

asecouiputerlabgaseco]nputerlaib-flP-ProDesJt
25. Go to Dir2 and remove the subdirectory Dir3.

asecofuterJ#secoiiipute:lab-HP-ProDesJ(-400-G7-
Wicrotower-PC>J cdmin/dirl/dir2
aseconputeriab(?aseconputerIab-iiP-ProDesJc-400-G7-
Microtover-PO/iMin/dirI/dir2^ n remove
26. Come back to your home folder and -r dir3Dir2.

asecomputerlab@aseconputerlab-HP-ProDesk-400-G7-
Microtower-PC:»/nain/dirl/dir2$ cd *
asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-
Microtower-PC:rmdir min/dirl/dir2
13
27. Display first 10 lines of the file countfinal in terminal.

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:head countfinal
one
two
three
four
five
six
seven
eight
nine
ten_

28. Display last 10 lines of the file countfinal in terminal.

asecomputerlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~$ tail countfinal


sixteen
seventeen
eighteen
nineteen
twenty
twenty one
twenty two
twenty three
twenty four
twenty five_

29. Display first 5 lines of the file countfinal in terminal.


asecomputerlab@asecornputerlab-HP-ProDesk-400-G7-Microtower-PO$ head ~n 5
countfinal
one
two
three
four
five

30. Display last 4 lines of the file countfinal in terminal.


14
asecojnputerlai»(?aseco/?puterIai-flP-ProDesA-4(30-G7-Micr(?tover-PC:^^ tail -n 4 count
final
twenty two
twenty three
treaty four
twenty five

31. Display the contents of the file countfinal in the inverted


form.(last line first and first line last)

asecompiiterlab@asecomputerlab-HP-ProDesk-400-G7-Microtower-PC:~$ tac
countfinal twenty five twenty four twenty three
twenty two
twenty one
twenty
nineteen
eighteen
seventeen
sixteen
fifteen
fourteen
thirteen
twelve
eleven
ten
nine
eight
seven
six
five
four
three
two
one_

15
LAB EXERCISE-02

18
1. Create a file demo with the following contents Student Prithivi
Essentials 20 PSAT 24 Maths 35 Cultural 27 English 65
Student Raj Essentials 25 PSAT 26 Maths 31 Cultural 20 English
91
Student Sabha Essentials 19 PSAT 18 Maths 29 Cultural 15
English 47
Student Dhina Essentials 23 PSAT 15 Maths 45 Cultural 24
English 96
Student Hari Essentials 9 PSAT 7 Maths 12 Cultural 13 English 5

vboxuser@Ubuntu: $ cat >demo


Student Prithivi Essentials 20 PSAT 24 Maths 35 Cultural 27 English 65
Student Raj Essentials 25 PSAT 26 Maths 31 Cultural 20 English 91 Student
Sabha Essentials 19 PSAT 18 Maths 29 Cultural 15 English 47
Student Dhina Essentials 23 PSAT 15 Maths 45 Cultural 24
English 96
Student Hari Essentials 9 PSAT 7 Maths 14 Cultural 15 English
6

2. Find the marks obtained by Sabha in all the subjects


vboxuser@Ubuntu: $ cat demo | grep 'Sabha'
Student Essentials 19 PSAT 18 Maths 29 Cultural 15 English 47

3. Print the marks for essentials in the increasing order


vboxuser@Ubuntu: $ cat demo | cut *d ' ' -f 4 | sort -n

9
19
20
23
25

4. Find the maximum marks scored in PSAT


i/boxuser@Ubuntu: $ catcut -d ' ' -f 6 sort -n tail -nl
demo 26 | |
5. Find the minimum marks obtained in Cultural
vboxuser@Ubuntu: $ cat deno |cut -d '
' -f 6 |sort -n | tail -nl 26
6. Save the marks obtained by all the students in maths
into a file and display it in the terminal using a single
command
vboxuser@Ubuntu: $ cat deno | cut -d ' ' -f 8 | tee file
35
31
29
45
14

7. Print the first 3 letters of all student names.


vboxuser@Ubuntu: $ cat deno | cut -d ' ' -f 2 | cut -c 1-3
Pri
Raj
Sab
Dhi
Har
8. Print the contents of file demo in terminal with all
alphabets in capital letters.

vboxuser@Ubuntu: $ cat demo | tr a-z A-Z


STUDENT PRITHIVI ESSENTIALS 20 PSAT 24 MATHS 35 CULTURAL 27 ENGLISH 65
STUDENT RAJ ESSENTIALS 25 PSAT 26 MATHS 31 CULTURAL 20 ENGLISH 91 STUDENT
SABHA ESSENTIALS 19 PSAT 18 MATHS 29 CULTURAL 15 ENGLISH 47
STUDENT DHINA ESSENTIALS 23 PSAT 15 MATHS 45 CULTURAL 24 ENGLISH
96
STUDENT HARI ESSENTIALS 9 PSAT 7 MATHS 14 CULTURAL 15 ENGLISH 6

9- Print all student names after deleting the letter ‘a’


k/boxuser@Ubuntu: $ cat deno | cut -d ' ' -f 2 | tr -d a
Prithivi
Sbh
Jhin
Hri
10.Count the number of lines, words and characters in
demo file after removing the letter ‘S’

: !> cat deno | | tr -d 'S' | wc


5 60 331
Lab Exercise 3

(CA) Ubuntu X + v

question-1.sh •
29 #//bin/bash 28
27 # 1. Create a file Studdetails with the following contents 26 cat > Studdetails « EOF
25 Student Alice Essentials 20 PSAT 22 Maths 34 Cultural 25 Physics 80 English 70 24 Student Bob Essentials 23 PSAT
21 Maths 32 Cultural 18 Physics 18 English 94 23 Student Boby Essentials 43 PSAT 31 Maths 22 Cultural 8 Physics 76
English 93 22 Student Clara Essentials 18 PSAT 16 Maths 27 Cultural 12 Physics 34 English
45
21 Student Dirch Essentials 25 PSAT 23 Maths 48 Cultural 25 Physics 45 English 98+
20 Student Eve Essentials 8 PSAT 6 Maths 12 Cultural 13 Physics 4 English 5 19 EOF 18
17 echo "2. Filter the details of the student Bob"
16 cat Studdetails | grep '\<Bob\>'
15 echo ""
14
13 echo "3. Find the number of students with their names containing the letter a, e or i"
12 cat Studdetails | cut -d ' ' -i 2 | grep '[aei]' | wc -1
11 echo ""
10
9 echo "4. Find the marks of students whose names starts with ‘b* Cease insensitive)"
8 cat Studdetails | grep -i '\<b.*\>'
7 echo ""
6
5 echo "5. Find the names of students whose names starts with ‘b} and ends with *y’ (case insensitive) 4 cat
Studdetails | cut -d ' ' -f 2 | grep -i ,nb.*y$'
3 echo ""

17
1. Write shell scripts for the following actions in the given order:
a. Create a file Studdetails with the following contents Student Alice
Essentials 20 PSAT 22 Maths 34 Cultural 25 Physics 80 English 70
Student Bob Essentials 23 PSAT 21 Maths 32 Cultural 18 Physics 18 English 94
Student Boby Essentials 43 PSAT 31 Maths 22 Cultural 8 Physics 76 English 93
Student Clara Essentials 18 PSAT 16 Maths 27 Cultural 12 Physics 34 English 45
Student Dirck Essentials 25 PSAT 23 Maths 48 Cultural 25 Physics 45 English 98+
Student Eve Essentials 8 PSAT 6 Maths 12 Cultural 13 Physics 4 English 5

b. Filter the details of the student Bob

c. Find the number of students with their names containing the letter a, e
or I
d. Find the marks of students whose names starts with ‘b’ (case
insensitive)
e. Find the names of students whose names starts with ‘b’ and ends with
‘y’ (case insensitive)

16

18
2. Write a shell script to perform the following actions in the given order:

§3 Ubuntu X

question-2.sh •
32 U 1.Create a file numericdata with the following contents
31 cat > numericdata « EOF
30 Uollam 56754 6754 7654
29 Vallihkavu 5H328 7548 45675
28 Trivandrum 16U23 6654 6754
27 Ernahulam 28796 8549 9875
26 Kayamhulam 35589 75892 3451
25 kottayam 45557 6773 6547
24 tirukulum 45675 56476 7896
23 EOF
22
21 echo "2. Display the details of Places that starts with ‘T’Ccase sensitive)"
20 cat numericdata | grep ,A7'
19 echo ""
18
17 echo "3. Display code3 in sorted order(ascending) of the places that start with ‘K’Ccase insensitive) and ends with am" 16 cat
numericdata | grep -i 'Ak.*am\>' | cut -d ' ' -f 4 | sort -n 15 echo 14
13
12 echo "4. Filter code2 that starts with 6 and ends with 4"
11 cat numericdata | cut -d ' ’ -f 3 | grep 'A6.*4$'
10 echo ""
9
8 echo "5. Filter code2 having one or more occurrence of the digit 6"
7 cat numericdata | cut -d ' ' -f 3 | grep -E '6+'
6 echo ""
5
4 echo "6. Filter all codel having one or more occurrence of the digit 5"
3 cat numericdata | cut -d 1 ' -f 2 | grep -E '5+'
2 echo ""

19
a. Create a file numericdata with the following contents Kollam 56754 6754
7654
Vallikkavu 54328 7548 45675 Trivandrum 16423 6654 6754 Ernakulam 28796
8549 9875
Kayamkulam 35589 75892 3451 kottayam 45557 6773 6547 tirukulum 45675
56476 7896
(Hint : First field is referred as Place second as code1 third as code2 and fourth as
code3)

f. Display the details of Places that starts with ‘T’(case sensitive)

g. Display code3 in sorted order(ascending) of the places that


start with ‘K’(case insensitive) and ends with am

h. Filter code2 that starts with 6 and ends with 4


i. Filter code2 having one or more occurrence of the digit 6

j. Filter all code1 having one or more occurrence of the digit 5.

20
3. Write a shell script to perform the following actions in the given order:
f. Write the contents of ls -la to a file named HiddenDetails
g. Write the contents of file HiddenDetails to the file HiddenFiles after replacing
multiple occurrences of space with a single space.
h. Using the file HiddenFiles, find the total number of files and folders that starts
with ‘p’(case insensitive)

i. Using the file HiddenFiles, find the names of files and folders that starts with
‘.’ and ends with ‘nda’
j. Using the file HiddenFiles, list all the directories
(The first field in ls -la denotes the permissions associated with the file. If the first
letter in permission is ‘d’ then it is a directory)

22
23
4. Write shell program to perform the following actions:
f. Write the contents of the command man sort into a file
SortManual.
g. Filter all lines form the file SortManual that starts with
‘compare’.
h. Filter all lines form the file SortManual that ends with ‘sort’.

i. Display the number of lines in SortManual that has the word


‘numeric’.
j. Write the contents of file SortManual to the file sort after
replacing multiple occurrences of ‘-’ with a single ‘-’
§ Ubuntu X + v “OX

question-M.sh
1 |.'/bin/b ash
1
2 # a. Write the contents of the command man sort into a file SortManual.
3 nan sort > SortManual
4
5 echo "b. Filter all lines form the file SortManual that starts with
‘compare’."
6 cat SortManual | grep '"compare1
7 echo
8
9 echo "c. Filter all lines form the file SortManual that ends with
‘sort’."
10 cat SortManual | grep 'sort}'
11 echo
12
13 echo "d. Display the number of lines in SortManual that has the word
‘numeric’."
14 cat SortManual | grep '\<muneric\>' | wc -l
15 echo ""
16
17 # e. Write the contents of file SortManual to the file sort after
replacing multiple occurrences of with a single
18 cat SortManual | tr -s '-' > sort

24
MySQL QUERIES
21

1) CREATE COMMAND:
mysql> create table crew (sno int not null, name varchar(10),expertise char(10),share int(5));
Query OR, 0 rows affected, 1 warning (0.20 sec)

mysql> desc heist;


ERROR 1146 (42S02): Table 'heist.heist' doesn't exist
mysql> desc crew;
-+----------- -+------ --------- -------
-- +- key |+- +
1 Type | Null | Default | Extra |
sno | int 1 NO | 1NULL | 1
name | varchar(10) 1 YES | 1NULL 1
expertise j char(10) 1 YES | 1NULL 1
share | int 1 YES | 1NULL j 1
-+-----------
4 rows in set (0.04 sec) - +-----_____ --------- -------
-- -+- + _ +- +

2) ALTER COMMAND:

2.1 ADD COLUMN:


mysql> alter table crew add(experience_in_years int ); Query Ok,
0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc crew;

+-------------- -+-------------+------+-----+---------+-------+
| Type | Null | key | Default | Extra
---
+- - | - -+-- --- --+--- ----
| Field - +- - +
1 sno i int i NO 1 1 NULL 1 i
1 name i varchar(10) i YES 1 1 NULL 1 1
1 expertise i char(10) i YES 1 1 NULL 1 1
1 share i int i YES 1 1 NULL 1 1
1 experience_in_years i int i YES 1 1 NULL 1 1
5 rows in set (0.00 sec)
_ _ —
-+-- ---+ - ---1-- ----
+- - - --- +

2.2 DROP COLUMN:

mysql> alter table crew drop experience_in_years;


Query Ok, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc crew;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | key | Default |
Extra |
+-----------+-------------+------+-----+---------+------
-+
| sno | int | NO | | NULL | |
j name | varchar(10) | YES | j NULL
jjexpertise j j j jj j
char(10) YES NULL
share j j
int YES NULL
+-----------+-------------+------+-----+---------+------
-+
4 rows in set (0.01 sec)
22

2.3 MODIFY COLUMN:


mysql> alter table crew modify name varchar(20);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc crew;


-------+
Field Type Null Key |Default
1 1 1 1 Extra |
sno 1 int 1 NO 1 1NULL 1 1
name 1 varchar(20) 1 YES 1 1NULL 1 1
expertise 1 char(10) 1 YES 1 1NULL 1 1
share 1 int 1 YES 1 1NULL 1 1
______ _____+_ -------+
4 rows in set (0.00 sec)

2.4 RENAME:

mysql> alter table chocolates -> rename


to candies;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from candies;
+-----+------------+-------+
| sno | brand | price |
+-----+------------+-------+
| 1 | fivestar | 10 |
2 j munch j 5 j
3 j dairy milk j 20
|
+-----+------------+-------+
3 rows in set (0.01 sec)

23

3) DROP TABLE:
mysql> drop table crew;
Query Ok, 0 rows affected (0.05 sec)

mysql> desc crew;


ERROR 1146 (42S02): Table 'heist.crew' doesn't exist

4) TRUNCATE:

mysql> create table chocolates Csno int not null, brand varchar(20) , price intC4) 3; Query OK, 0
rows affected, 1 warning C0.O4 sec}
mysql> desc chocolates;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | int | NO | | NULL | |
j brand j varchar(20) j YES | j NULL
j price j int j YES j | NULL
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec}
mysql> insert into chocolates valuesCl,"fivestar",10),
-> C2,"munch" ,5)

-> ,
-> C3,"dairy milk" ,20);
Query OK, 3 rows affected C0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from chocolates;
+-----+------------+-------+
| sno | brand | price |
+-----+------------+-------+
| 1 | fivestar | 10 |
2 j munch j 5 j
j 3 j dairy milk j 20 j
+-----+------------+-------+
3 rows in set C0.00 sec)
mysql> truncate table chocolates;
Query OK, 0 rows affected CO.04 sec)
mysql> select * from chocolates;
Empty set C0.O0 sec)
5) INSERT :

mysql> INSERT INTO hospital CSNO, PATIENT , DOB , AMOUNT )


-> VALUES Cl,'Smitha','2003-05-09',1500),
-> C2,'Tate','1985-11-05’,20000),
-> C3,'Greta','2002-04-12',300);
Query OK, 3 rows affected (0-02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM hospital ;
+-----+---------+------------+--------+
| SNO | PATIENT | DOB | AMOUNT |
+-----+---------+------------+--------+
| 1 | Smitha | 2003-05-09 | 1500 |
2 j Tate | 1985-11-05 | 20000 |
j 3 j Greta | 2002-04-12 | 300 |
+-----+---------+------------+--------+
3 rows in set C0.00 sec)

6) UPDATE :

mysql> UPDATE hospital


-> SET PATIENT = 'Selena' , AMOUNT = 2000 ->
WHERE SNO = 1;
Query OR, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM hospital ;


+-----+---------+------------+--------+
| SNO | PATIENT | DOB | AMOUNT
|
+-----+---------+------------+--------+
| 1 | Selena | 2003-05-09 |
2000 |
2 j Tate j 1985-11-05 j
20000 j
j 3 | Greta | 2002-04-12 j 300 |
+-----+---------+------------+--------+
7) DELETE
3 rows in :set (0.00 sec)

26
7.1 DELETING A PARTICULAR RECORD USING CONDITION

mysql> DELETE FROM hospital WHERE PATIENT = 'Greta';


Query OK, 1 row affected CO.01 sec)

mysql> SELECT * FROM hospital ;


+-----+---------+------------+--------+
| SNO | PATIENT | DOB | AMOUNT |
+-----+---------+------------+--------+
| 1 | Selena | 2003-05-09 | 2000
|
j 2 | Tate | 1985-11-05 | 20000 |
+-----+---------+------------+--------+
2 rows in set (0.00 sec)

mysql> SELECT PATIENT,DOB FROM hospital ;

| PATIENT | DOB

| Selena | 2003-05-09 | |
Tate | 1985-11-05 |

2 rows in set (0.00 sec)

7.2 DELETING ALL RECORDS :

mysql> DELETE FROM hospital;


Query OK, 2 rows affected (0.01 sec)

mysql> SELECT * FROM hospital ;


Empty set (0.00 sec)

8) SELECT :

8.1 SELECT CERTAIN FIELDS :


8.2 SELECTING ALL FIELDS :

27
8.3 SELECTING DISTINCT FIELDS :

mysql> SELECT * FROM hospital;


+-----+---------+------------+--------+
| SNO | PATIENT | DOB | AMOUNT |

+— --- --—---- ----—------ -+
- - -
1 1 1 Smitha 1 2003-05-09 1 15001
1 2 1 Tate 1 1985-11-05 1 200001
1 3 1 Greta 1 2002-04-12 1 3001
1 4 1 Travis 1 1990-03-04 1 15001
1 5 1 Robert 1 2000-07-05 1 3001
5 rows in set C0.00 sec)
+— ____ -+

mysql> SELECT DISTINCT AMOUNT FROM hospital;


+--------+
| AMOUNT |
+--------+
| 1500 |
| 20000 |
| 300 |
+--------+
3 rows in set (0.00 sec)

9) OPERATORS

9.1 AND :

28
mysql> SELECT * FROM HEROES;
-+--------- -+---------
SNO FNAME AGE
1 | LNAME | PLACE 1
11 SON | GOKU | JAPAN 1 50
21 IRON | MAN | USA 1 48
31 BLACK | PANTHER | WAKANDA 1 38
41 THOR | NULL NULL 1 NULL
51 SPIDER | MAN | USA 1 20
61 ANT | MAN | USA 1 40
71 HULK | NULL | USA 1 45
_ _ _ -+--------- -+--------- _
7 rows in set CO.00 sec)
mysql> SELECT * FROM HEROES WHERE LNAME = 'MAN' AND AGE > 40;
-+------+
| SNO FNAME LNAME | PLACE 1 AGE |
1 1
CM 1 11 IRON 1 MAN | USA 1
1 + —
1 1
1 -------+- 1
h - + 1
1 row in set (0.00 sec) 1 -C=
1 00 i
+ — H

9.2 OR :

mysql> SELECT * FROM HEROES WHERE LNAME = 'MAN' OR PLACE = 'JAPAN'; |

SNO | FNAME | LNAME | PLACE | AGE |


T------1 ---------f- —r-------1-—

11
SON | | JAPAN |
50
GOKU |
IRON MAN | USA
4 rows in set (0-00 sec) 48
12 j
SPIDER | MAN | USA
15 20
j
MAN 1 USA |
-4--------J---
40
16
+------J
ANT |
L--------+ .
j

29
9.3 NOT :

mysql> SELECT * FROM HEROES WHERE NOT LNAME = 'MAN';


T
t" t T T ---T
AGE |
| SNO | FNAME | LNAME | PLACE |
T t T -f- f T
| 1 | SON | GOKU | JAPAN | 50 |
| 3 | BLACK | PANTHER | WAKANDA | 38 |
_L _L
TTTTTT

2 rows in set CO.00 sec)

9.4 COMBINIG AND,OR and NOT OPERATOR :

mysql> SELECT * FROM HEROES WHERE LNAME = 'HAN' OR NOT PLACE = 'JAPAN
T-----T-------- T------- T-------
T-----T

| SNO | | | 1 AGE
FNAME LNAME PLACE |
+ 4- 4- 4- 4- 4-
T

| 2 | IRON | MAN | USA 1 48 1


SPID
1 5 ER | MAN | USA 1 20 |
4 rows in set
1 6 ANT (0.00
| MAN sec)1
| USA 40 |
NUL
7 HULK L USA 45 1
10) NULL AND NOT NULL COMMANDS :

10.1 IS NULL :
10.2 IS NOT NULL :

mysql> SELECT FNAME,PLACE,AGE FROM HEROES WHERE LNAME IS NOT NULL;


FNAME 1 PLACE 1 ii
1 LU 1
(J 1 <

SON 1 JAPAN 1 50
IRON 1 USA 1 48
BLACK 1 WAKANDA 1 38
SPIDER1 USA 1 20
ANT 1 USA 1 40
_ _ _ _
5 rows in set CO.00 sec)

11) LIKE OPERATOR :


mysql> SELECT * FROM HEROES WHERE FNAME LIKE 'S%';

| SNO | FNAME | LNAME | PLACE | AGE |

| 1 | SON | GOKU | JAPAN | 50 |


j 5 j SPIDER j MAN | USA | 20 |

2 rows in set (0.00 sec)


mysql> SELECT * FROM HEROES WHERE PLACE LIKE '%N%';

| SNO | FNAME | LNAME | PLACE | AGE |

| 1 | SON | GOKU | JAPAN | 50 |


| 3 j BLACK j PANTHER | WAKANDA | 38 |

2 rows in set CO.00 sec)

mysql> SELECT * FROM HEROES WHERE PLACE LIKE '%A';


+¬ SNO i FNAME i LNAME I PLACE Ill
-4- -4-- -4- 4 1
- - Ul 1
1 - - -
(J 1 <
1
_
T T T T T
l 2i IRON i MAN i USA l 48
l 3 i BLACK i PANTHER i WAKANDA l 38
l 5 I SPIDER I MAN I USA 1 20
l 6 i ANT i MAN i USA l 40
l
5 rows 7 iset
in HULK i NULL
(0.00 sec) i USA l 45
+-

32

mysql> SELECT * FROM HEROES WHERE FNAME LIKE '__0%';

| SNO | FNAME | LNAME | PLACE | AGE |

| 2 | IRON | MAN | USA | 48 |


j 4 j THOR j NULL | NULL | NULL |

2 rows in set (0.00 sec)


mysql> SELECT * FROM HEROES WHERE FNAME LIKE 'S___;

| SNO | FNAME | LNAME | PLACE | AGE |

| 5 | SPIDER | MAN | USA | 20 |

1 row in set (0.00 sec)

mysql> SELECT * FROM HEROES WHERE PLACE LIKE 'U%A'; | SNO | FNAME

| LNAME | PLACE | AGE | -+-----


2 | IRON | MAN | USA 1 48 1
5 | SPIDER | MAN | USA 20 |
6 | ANT | MAN | USA 1 40 1
7 | HULK | NULL | USA - 1 45 |
+-----
4 rows in set (0.00 sec)

33

12) IN OPERATOR :

12.1 IN :
mysql> SELECT * FROM HEROES WHERE PLACE IN ('USA','JAPAN');
+------ -+--------- +-------+ ------+
¥------- | PLACE | AGE |
| SNO | FNAME
-L -4- | LNAME |
- -L

1 1 | SON | GOKU | JAPAN | 50 |


2| IRON MAN USA 48 |
1 5 | SPIDER | MAN | USA 20 |
| 6 | ANT | MAN | USA 40 |
1 7 +--| HULK ■+-- | NULL ¥-j USA | +- 45 |
---- ------- ------ ------+ ------+
5 rows in set CO.00 sec)

12.2 NOT IN :

mysql> SELECT * FROM HEROES WHERE LNAME NOT IN ('MAN');


| SNO | FNAME | LNAME | PLACE | AGE |

| 1 | SON | GOKU | JAPAN | 50 |


1 3 j BLACK j PANTHER | WAKANDA | 38 |

2 rows in set (0.00 sec)


34

13) BETWEEN OPERATOR :

13.1 BETWEEN :
BETWEEN 40 AND 50;
mysql> SELECT * FROM HEROES WHERE AGE
L _L __l_
r n r n r T T
i n
FNAME LNAME PLACE | ■ 1
| SNO ■ AGE
i
M r H r H r +- " + 1
SON GOKU JAPAN | 50
1 2 IRON MAN USA 48 1
1 6 ANT MAN USA 40 1
45

w
m
13.2 NOT BETWEEN :

■ ■ ■ i ii
HULK
L J
NULL
L 4 L
USA | 1
-J-

4 rows in set (0.00 sec)


mysql> SELECT * FROM HEROES WHERE AGE NOT BETWEEN 30 AND 45;
-L

T ’T T t T” ”—- T
| SNO | FNAME | LNAME | PLACE | AGE |
-L

T ' Tt T T” ’ T

1 1 | SON | GOKU | JAPAN | 50 |


1 2 | IRON | MAN | USA 48 |
1 5 j SPIDER | MAN | USA | 20 |
_l_ -4- - L - L - L
. - I -
T

3 rows in set (0.00 sec)

13.3 BETWEEN WITH IN :


35

You might also like