0% found this document useful (0 votes)
44 views

Commands

The document discusses various SQL commands used for managing databases and tables. It covers commands for creating databases and tables, altering table structures, inserting and modifying data, and selecting data with queries using operators, conditions and more. Specific commands covered include CREATE, ALTER, TRUNCATE, DROP, INSERT, UPDATE, DELETE, SELECT, DISTINCT, BETWEEN, LIKE and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Commands

The document discusses various SQL commands used for managing databases and tables. It covers commands for creating databases and tables, altering table structures, inserting and modifying data, and selecting data with queries using operators, conditions and more. Specific commands covered include CREATE, ALTER, TRUNCATE, DROP, INSERT, UPDATE, DELETE, SELECT, DISTINCT, BETWEEN, LIKE and more.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL Commands

Lecturer No 6
To see existing databases
show databases;

To create new database:


create database database_name;

To use existing databases;


use database_name;

To creating table
create table table_name (
column1 datatype[size],
column2 datatype[size],
column3 datatype [size],
....
);

To see existing tables


show tables;

creating new table from existing table


create table table_name as select * from existing table_name;

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;

To insert records into the table


Insert into table_name values (vale1,value2….);

Lecturer No 7
Alter Table
Alter table query is used to modify structure of a table. We can add
delete modify column.

1.Adding New Column in a Table


alter table table_name add column_name datatype;

2.Dropping Column from Table


alter table table_name drop column column_name;

3.Modify Column of a Table


alter table table_name modify column column_name datatype;

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

create table Course_Details (course_id int,course_code


varchar(20)); basic table

create table student2020(stud_id int,name


varchar(20),course_code varchar(20) references
Course_Details(course_code)); reference table

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,……);

2.Inserting values in specific column


insert into table_name (column1,column2…)values
(value1,value2,……);

3.Inserting records from exesting table into new table


insert into table_name select column1,column2… from
table_name2 where condition;

UPDATE
update table_name set column_name=new value[where
condition]

DELETE
1.To Delete all records
delete from table_name;

2.To Delete specific records


delete from table_name where condition;
Lecturer No 11
DQL Commands

SELECT
1.To select column wise data
select column1, column2…from table_name;

2.To select all data


select * 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;

SAVEPOINT & ROLLACK


To create savepoint
savepoint savepoint_name;

To get rollback
rollback to savepoint_name;

COMMIT
commit;
Lecturer No 12
SQL Operators

Arithmetic +, -, *, /, %
Operators

=, >, <, >=, <=,


Comparison
Operators
!=, <> , !>, !<

ALL ,AND ,ANY, BETWEEN,


SQL Operators Logical Operators
EXISTS, IN, LIKE, NOT, OR, SOME

Bit wise & | ^


Operators

Compound += -= /= %=
Operators &= ^= |*=
Lecturer No 13
SET Operators
UNION
select column_name from table_1
union
select column_name from table_2;

select deptno from emp union select deptno from dept;

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%";

You might also like