Commands
Commands
Lecturer No 6
To see existing databases
show databases;
To creating table
create table table_name (
column1 datatype[size],
column2 datatype[size],
column3 datatype [size],
....
);
creating new table having specific fields but all records from existing
table.
create table table_name as select field1,field2 from existing
table_name;
creating new table having specific records but all fields from existing
table.
create table table_name as select * from existing table_name
where condition;
creating new table having no records but all fields from existing
table.
create table table_name as select * from existing table_name
where false condition;
Lecturer No 7
Alter Table
Alter table query is used to modify structure of a table. We can add
delete modify column.
4.Rename of Table
alter table table_name rename to new_name;
Truncate Table
This command is used to delete all records from table.
truncate table table_name;
Drop Table
Drop table command is used to delete table permanently from the
database.
drop table table_name;
Desc
Desc query is used to display structure of table
desc table_name;
Lecturer No 8
Primary Key
create table emp (eno int ,primary key(eno));
Foreign Key
Unique Key
create table student20(stud_id int,name
varchar(20),course_code varchar(20)unique);
Lecturer No 9
NOT NULL
create table Course (course_id int not null,course_code
varchar(20));
DEFAULT
create table stud(rno int,sname varchar(20),age int default 15);
CHECK
create table stud1(rno int,sname varchar(20),age int
check(age>=18));
Lecturer No 10
DML Commands
INSERT
1.Inserting values in all column
insert into table_name values (value1,value2,……);
UPDATE
update table_name set column_name=new value[where
condition]
DELETE
1.To Delete all records
delete from table_name;
SELECT
1.To select column wise data
select column1, column2…from table_name;
WHERE CLAUSE
select column1, column2…from table_name where condition;
DISTINCT CLAUSE
select distinct (column_name) from table_name;
DCL Commands
GRANT
grant select insert update delete on table_name to user_name;
REVOKE
revoke select insert update delete on table_name from
user_name;
To get rollback
rollback to savepoint_name;
COMMIT
commit;
Lecturer No 12
SQL Operators
Arithmetic +, -, *, /, %
Operators
Compound += -= /= %=
Operators &= ^= |*=
Lecturer No 13
SET Operators
UNION
select column_name from table_1
union
select column_name from table_2;
UNION ALL
select column_name from table_1
union all
select column_name from table_2;
INTERSECT
select column_name from table_1
intersect
select column_name from table_2;
MINUS
select column_name from table_1
minus
select column_name from table_2;
BETWEEN & LIKE
BETWEEN
select * from emp where sal between 25000 and 35000;
LIKE
select * from emp where name like "A%";