Create Table: Performing Operation On Table Data
Create Table: Performing Operation On Table Data
1 | SHK
ECS-II | MySQLUnit-2
Alter Table (Modifying structure of Table)
The ALTER TABLE statement is used to change the structure of an existing table. It is used to add,
modify or delete columns an existing table. It is also used to rename a table name and column
names. You can also add and drop various constraints on an existing table.
3 | SHK
ECS-II | MySQLUnit-2
Insert Statement
INSERT statement is used to store or add data in MySQL table within the database. We can perform
insertion of records in two ways:
1. Insert record in a single row
2. Insert record in multiple rows
1. Single Row:
Syntax:insert into table_name (field1, field2,…..fieldn ) values ( value1, value2,…..valuen );
Example:insert into emp (id, name, occupation, age) values (101, 'Ravi', 'engineer', 32);
2. Multiple Rows:
Syntax:insert into table_name values (val1, val2,...valn ), (val1, val2,...valn).....(val1,val2,...valn);
Ex:insert into emp values (102, 'Joseph', 'Developer', 30), (103, 'Mahi', 'Leader', 28), (104,'Sam','Scientist', 45);
3. If we want to store records without giving all fields, we use the following partial field statements
insert into emp (name, occupation) values ('satish', 'scientist'), ('binod', 'actor');
Insert into Select Statement
We want to insert data of one table to other table in the same or different database.It is not easy to
enter these data using the insert query manually. We use of insert into select query. It allows us to
fill tables quickly.Query copies data from one table and inserts them in the other table.
o The data types in source and target tables must be the same.
o The existing records in the target table should be unaffected.
Adding single or multiple from one table into another table
Syntax: insert into table_name2 select * from table_name1; or insert into table2 table table1;
insert into person select * from emp; or insert into person table emp;
Copy only specific records from one table into another table using ‘where clause’
Syntax: insert into table_name2 select * from table_name1 where condition;
insert into person select * from empwhereoccupation='Developer';
4 | SHK
ECS-II | MySQLUnit-2
Where clause applies a filter on the retrieved rows. When a WHERE clause is added to the
SQL query, the Oracle engine compares each record in the table with specified condition. All
operators such as logical, arithmetic, etc. can be used in the condition of WHERE clause.
3. Selected Columns and Selected Rows
Syntax: select <col_name1>,<col_name2>,.<col_name n> from <table_name> where <condition> ;
Example: select eno, ename from emp where eno=10;
It will display all records with specific fields after specified condition WHERE will be true.
Eliminating Duplicate Rows
In a table, there may be a chance to exist a duplicate value. DISTINCT command is used to retrieve
only unique data. SELECT UNIQUE & SELECT DISTINCT statements are same.
Syntax: select distinct <Col_Name1>,<Col_Name2>,…<Col_Name n> From <Table_name>;
or
select distinct * from <table_name>;
Example: select distinct eno, ename from emp;
or
select distinct * from emp;
Distinct clause- allows removing duplicates data from the result set. This clause only used with
SELECT statement. It scans all the values of the specified columns and display only unique values. It
eliminates rows that have exactly same contents in each column.
Arithmetic calculation
Example: Select employee name, salary and compute salary * 0.05 for each row retrieved:
Select ename, sal, sal * 0.05 “Salary” from emp;
Here the default output column sal*0.05 is renamed with Salary.
Delete (Deleting Data)
Delete statement is used to delete one or more rows from a table.
Delete query use following two way,
o Remove all rows
o Remove only specific rows
Delete statement does not free containing space by the table.
Syntax: delete from <table_name> where <condition>;
If condition is not specified then it will be delete all rows.
Example: delete from emp; // it will delete all the records from a emp table.
If condition is specified then it will delete all rows that satisfy the specified condition.
Example: delete from emp where eno>10;
It will delete all the records for which ENO is greater than 10.
UPDATE (Updating a Contents of Table)
The UPDATE statement changes in existing rows data either adding new data or modifying existing
data.User can update
all rows from table (update all table rows)
selected rows from table (update only specific data/rows)
Syntax: update <table_name> set <columnname1>=<expression1>, <columnname2>=
<expression2>……, <columnnamen>=<expressionn> where <condtion>;
OR
update <table_name> set <column_name=expression> where <conditions>;
Example: update emp set city=‘sangola’;
All rows from table: If condition is not specified then it will be update entire table rows.It can
update multiple fields at the same time.
update emp set salary=500;
5 | SHK
ECS-II | MySQLUnit-2
update emp set address='pune', salary=1000;
Selected rows from table: If condition is specified then it will be update only specific table
rows.It can update single fields at the same time.
update emp set city=‘sangola’ where eno=10;
6 | SHK