( (size),
(size) ... );
Name of attribute Data Type
Student_1D. int
Student_Name char(30)
‘Age int
Phone int
‘Address varchar(50)
[age ta- eee ona
cae Meta
Sere er gel
Age int,
Peas
CES LDH
Query OK, 0 rows affected (0.04 sec)
Describe table:
To view the structure of table (like attributes and its data types, keys, constraints, default
values), desc command is used.
SQL Syntax:
desc
; OR describe
;
170| PaceSe mee tte
iin
NVI
NUTR
cNUTa
va
Insert command:
To insert data in table, insert command is used (one row at a time). Here in this example, data
of 4 students are inserted in table student.
SQL Syntax:
insert into
values (, , ...);
erage CN
Cree mao
Cae erariaer ie
PERO eae ga: Ct) Ord cP
Kduery 0K, 1 row affected (0.08 sec)
EC ORR eae Besa Oa aed oe Mr eee
duery 0K, 1 row affected (0.11 sec)
Select command:
To show the data of a table, select command is used. Let’s show the data of 4 students in
student table that was inserted in the previous command.
SQL Syntax:
select * from
;
Chet eae 2 Ot al er
Steen ected
ures
oo
feel
ee
ws in set (0.86 sec)
Drop table command
To delete data as well as structure of a table, drop command is used
SQL Syntax:
drop table
[alter option);
Add a column to the table:
We have table student which was created in previous section
ear
era
ets
str)
SQL Syntax:
alter table
add [constraint];
Example: If we want to add a column class with data type varchar and size 0 and nulls are
not allowed.
ee ee comer
eee e ee
Ceasers Desi
Tee iu ast
Poe e
Reg Fer
Drop a column from the table:
Let’s delete a column class from table student which we added in the previous section.
SQL Syntax:
alter table
drop column;
178| Paceysql> alter table student drop column class;
OO ear aera ig gts
Se Dryer )
LOM Co Sede ues a
(steerer er
ats EEC
eo 8769876
Ea) em Wk} rte
ery ui
errata
rows in set (0.
Modifying column of a table: we have different ways to modify a table like column
name, data type, default value, size, order of column, constraints.
1.
Changing column name: We can change the column name of a table using alter
command. For example, in table student, we are going to change column name
Student_ID to ID.
SQL Syntax:
alter table
change column
;
mysql> alter table student change column Student_ID ID int;
Query OK, @ rows affected (@. 14 sec)
Records:'@ Duplicates: @ Warnings: @
mysql> select x from student;
gurusram
jaipur
76 | noida
4 rows in set (8,00 sec)
Changing column data type: We can change the column data type from varchar to
char or int to varchar etc. of a table using alter command. For example, in table
student, we are going to change datatype of column ID from int to varchar.
SQL Syntax:
alter table
change column ;
mysql> desc student
i Type i Null | Key | Default | Extra |
iGo Tinea] 7 ai i 1
Sraaent Ware | toot ' |
I i tot i |
| Bhgne | | | | i i
| Addr | i it \ i
S rows in set (8.00 sec)
mysql> alter table student modify column ID varchar(5@);
Query’ OK, 4 rows affected (8.48 sec)
Records!’ 4 Duplicates: @ Warnings: @
179| Pagemysql> alter table student modify column ID varchar(52);
Query’ OK, 4 rows affected (0.48 sec)
Records:’4 Duplicates: @ Warnings: @
mysql> desc student;
a ee
| Type
char tag
1 Intcity
I int (11)
I hi
S rows in set (@.@1 sec)
3. Changing maximum size of the data in a column: We can change the maximum size
of the data in a column of a table using alter command. For example, in table student,
we are going to change size of column ID from varchar(50) to varchar(40).
SQL Syntax:
alter table
change column ;
chartae
int (qd)
int Ct)
varchar(SQ)
mysql> alter table student modify column ID varchar(4@);
Query OK, 4 rows affected (4.31 sec)
Records:'4 Duplicates: @ Warnings: @
varchar (40)
char
intcd)
int(1L)
4. Changing order of the column: We can change the order of the column of a table using
alter command. For example, in table student, we are going to place column ID after
column Age.
SQL Syntax:
alter table
add primary key();
181| Pacev
aysql> alter table student add primary key(ID);
Ruery OK, 4 rows affected (0.51 sec)
Records:'4 Duplicates: @ Warnings: O
nysql> desc student;
I Field | Type TNull | Key | Default | Extra |
t i har(40) | ifPRI]i i i
! Seudent Name | char(3a) | YES {Leet} NULL | |
' | intcaiy 1 Yes | 1 NUCC i
| Beene | intra) | YES | 1 NuCr |
| Address |_varchar(S9) | YES I I NUCL I
5 rows in set (0.16 sec)
Dropping primary key: We are going to remove primary key at column ID which
we added in the previous section,
Command: alter table
drop primary key;
sql gltgr table student degp primary keyi
Bier ofa" Sue sefected' (B38 ScetorY Kev
eeotdet'a® Boplicates:°8 Harhines: @
ysql> desc student j
Extra |
rows in set (0.00 sec)
Adding a new column: We are going to add a column country with data type char
of size 50 to the table student using alter command
Command: alter table
add column ;
mysql> alter table student add column country char(S@);
Query OK, 4 rows affected (0.27 sec)
Records:'4 Duplicates: @ Warnings: @
mysql> desc student
1 Field | Type T Null | Key | Default I Extra |
1 | varchar(4a) | NO. | i 1 i
1 Student _Nane | char(3a) | YES | PUL} i
1 1 intCiip 1 YES | FNULE |
| BRS. | int(i1) 1 YES | FNUEE i
| Address | Varchar(so) | YES | FNUEE |
| country I char(Se) | YES | I Nuc i
6 rows in set (0.01 sec)
Dropping a column: We are going to remove a column ‘country’ which we added
in the last section using alter command.
Command: alter table
drop column ;
182| Pavemysql> alter table student drop column country;
Query OK, 4 rows affected (9.31 sec)
Records:'4 Duplicates: @ Warnings: @
| Field I Type UNull | Key | Default | Extra |
i i No. | i 1 1
1 I! 30. t i PMULL 1
| Age 1 Ent (1d) ives | PNUCL 1
| PRone | int (1) | YES | PMuce 1
| Address t I I 1 1
S rows in set (0.01 sec)
DML (Data Manipulation Language) Commands:
These commands are used to make any changes in the data of the table.
DML commands: insert, delete, update, select etc.
We have already covered few DML Commands like insert and select. Now we will discuss
delete and update command.
Delete comman
Delete command is used to delete data from the table. Where clause is used to give condition
ina SQL query. All those tuples which satisfies the condition will be deleted from the table.
SQL Syntax:
delete from
where ;
Now let's delete data of all those students from student table whose ID is greater than 5.
98769876
88769876
68769876
98769875
rows in set (0.00 sec)
mysql> delete from student where ID?5;
Query OK, 2 rows affected (@.13 sec)
mysql> select ® from student;
| ID | Student_Name | Age | Phone | Address |
2 1 Amit | 47 | 98769876 | delhi i
12 | Sonam | 18 | 88769876 | gurusram |
13 | Mahesh | 17 | 68769876 | jaipur |
14 | Priya | 18 | 78769876 | noida i
15 1 Monika | 17 | 98769876 | delhi 1
S rows in set (0.08 sec)
183 | PaceUpdate command: Update command is used to update data from the table. Where
clause is used to give condition in a SQL query. All those tuples which satisfies the condition
will be update from the table.
SQL Syntax:
update
set = where ;
Example 1: Let’s update the Age to 18 of that student whose name is Amit.
mysal> select * from student
aot + +
| ID | Student_Nane | Age I I i
pana + +
1 tLAmit 17]! | Sonipat |
12 | Sonam ! | gurugram |
1 3 | Mahesh ! i7 | | jaipur |
14 | Priya | 1g | | noida |
1o t heat ka 1 i7l | delhi |
12 | Ajay | 17 | | jaipur |
13 | sonal | 18 1 | hoida |
16 1, I 161 | delhi |
== +
& rows in set (0.08 sec)
192 | Pavemysql> update student set Age=18 where Student_Name="Amit';
Query OK, 1 row affected (8.09 sec)
Rows matched: 1 Changed: 1 Warnings: @
Sonipat
gurugran
jaipur
noida
delhi
jaipur
noida
Example 2: Let’s update the city to delhi of that student whose ID is 1 and Age is 17.
mysql> select x from student;
Student _Name | Age |_Phone
in set (@.01 sec)
pysal> update student, set Address=\delhi’ where ID=1 and Age=17;
Query OK, 1 row affected (0.11 51
Reoermctohen! “Unenocns 1 Wersitom: @
mysql> select * from student
Amit
Sonam,
Mahesh
gurugram
jaipur
Roida
B rows in set (2.00 sec)
Delete Command:
It is used to delete the existing rows in a table that matches the condition.
‘SQL Syntax:
delete from