MySQL Note 2
Select Statement – This Statement is used to retrieve records from a table.
Syntax-
mysql > select column_name from table_name where condition;
Note : if you retrieve data from the whole table you can write * instead of column_name;
Example:
mysql > select * from stud;
id name
1 abc
2 def
3 ghi
4 jkl
5 NULL
5 rows in set (0.00 sec)
mysql > select * from stud where id = 1;
id name
1 abc
mysql > select name from stud where id = 1;
name
abc
mysql > select * from stud where id > 3;
mysql > select * from stud where id < 3;
mysql > select * from stud where id >= 3;
mysql > select * from stud where id <= 3;
mysql > select * from stud where id between 1 and 3;
id name
1 abc
2 def
3 ghi
mysql > select * from stud where id in (1,3,5);
id name
1 abc
3 ghi
5 NULL
LIKE - The LIKE operator is used to compare a value to similar values using wildcard
operators. It allows to use percent sign(%) and underscore ( _ ) to match a given string
pattern.
mysql> select * from stud where name like '_h_';
id name
3 ghi
mysql> select * from stud where name like 'a%';
id name
1 abc
mysql> select * from stud where name is NULL;
id name
5 NULL
mysql> select * from stud where name is not NULL;
id name
1 abc
2 def
3 ghi
4 Jkl
mysql> select * from stud where id = 1 and name = 'abc';
id name
1 abc
mysql> select * from stud where id = 1 and name = 'def';
Empty set
mysql> select * from stud where id = 1 or name = 'def';
id name
1 abc
2 def
Alter Table – Alter Table alters the structure of the table
a) Syntax:
Alter Table table_name add (new_column_name1 datatype,new_column2 datatpe….);
Example
mysql> alter table stud add ( marks int(3), city char(10));
mysql> alter table stud add primary key(id);
mysql> alter table stud add (join_dt date);
mysql> INSERT INTO stud (id,join_dt) VALUES (6,"2023-12-31");
b) Syntax:
ALTER TABLE table_name MODIFY field_1 newdata_type(Size),
MODIFY field_2 newdata_type(Size),.... MODIFY field_n newdata_type(Size);
Example
mysql> alter table stud modify city varchar(30);
c) Syntax:
ALTER TABLE table_name DROP COLUMN field_name1, DROP COLUMN
field_name2;
Example
mysql> ALTER TABLE stud DROP column marks,drop column city;
d) Syntax:
ALTER TABLE table_name rename column old_column_name to
new_column_name;
mysql> ALTER TABLE stud rename column name to sname;
Rename Table - It is used to modify the name of the existing database object.
Syntax - RENAME TABLE old_relation_name TO new_relation_name;
Example - mysql>RENAME TABLE stud TO stud1;
Drop Table - This is used to delete the structure of a relation. It permanently deletes the records in
the table.
Syntax: DROP TABLE relation_name;
Example:
mysql>DROP TABLE stud1;
Update table statement – update table statement updates row in a table;
Syntax – update table_name set field_name = value where condition;
mysql>update stud1 set name = 'Hari' where id = 5;
id name
5 Hari
Delete statement – Delete statement delete records from a table;
Syntax1 – delete from table_name where condition;
Example1 – delete from stud1 where id = 5;
Syntax2- delete from table_name;
Example2- mysql>delete from stud1;
mysql> select id,id+2,id * 3,id -4,id/10,name from stud1;
id id+2 id*3 id-4 id/10 name
1 3 3 -3 0.1000 abc
2 4 6 -2 0.2000 def
3 5 9 -1 0.3000 ghi
4 6 12 0 0.4000 jkl
5 7 15 1 0.5000 NULL
Operator precedence - * / + -
Multiplication and division take priority over addition and subtraction.
Operators of the same priority are evaluated from left to right.
Parentheses are used to force prioritized evaluation and to clarify statements.