SQL
SQL lets you access, manipulate and managing data in a relational database management system (RDBMS).
Constraints are used to limit the type of data that can go into a table.
commonly used constraints in SQL:
2)NOT NULL - Ensures that a column doesn't have a NULL value.(empty)
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table.
FOREIGN KEY - it is used to link to tables.
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - if no value is given in any column ,default value for a column is given.
CREATE INDEX - Used to create and retrieve data from the database very quickly.
create table <table_name>
{
column1 name datatype;
column2 name datatype;
column3 name datatype
}
describe table_name;
create table employee { id int; name varchar(20); age number(10); } describe emp;
+----+----------+-----+ | id | name | age | +----+----------+-----+ | | | | | | | | | | | | | | | | | | | | +----+----------+-----+
syntax: drop database database_name;example: drop database company;
when we drop a table , all the data and even the structure of the table is deleted.
and has 1st row=> 1,Ravi,30
and 2nd row=> 4,Pooja,37
and 3rd row=> 6,Arjun,28
syntax: drop table table_name;example: drop database employee;
and has 1st row=> 1,Ravi,30
and 2nd row=> 4,Pooja,37
and 3rd row=> 6,Arjun,28
syntax: truncate table table_name;example: truncate database employee;
+----+----------+-----+ | id | name | age | +----+----------+-----+ | | | | | | | | | | | | +----+----------+-----+
alter command is used to make changes on table structure.
lets us assume we have a table employee with id int, name varchar(20), age number(10);
and dep table with int dep_id , name varchar(20);
1)add column: alter table employee add address varchar(30);
2)remove column: alter table employee drop column age;3)modify datatype:
alter table employee modify id varchar(10);4)modify datatype length: alter table employee modify id varchar(30);5)rename column: alter table employee rename column id to roll_no;6)add constraints: alter table employee add primary key(roll_no); alter table employee add foreign key(dep_id) references dep(dep_id);7)remove constraints: alter table employee drop primary key; alter table employee drop foreign key dep_id;8)rename table: alter table employee rename to emp;
it is used to display some data from a database. The data displayed will be displayed in the form of table .
lets us assume we have a table employee with id int, name varchar(20), age number(10);
and has 1st row=> 1,Ravi,30
and 2nd row=> 4,Pooja,37
and 3rd row=> 6,Arjun,28
case 1) if we are selecting all columns:
syntax: select * from table_name;
example: select * from employee;
+----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | Ravi | 30 | | 4 | Pooja | 37 |
| 6 | Arjun | 28 | +----+----------+-----+
case 2) if we want to select some particular columns:
syntax: select column1, column2,... from table_name;
example: select id, name from employee;
+----+----------+ | id | name | +----+----------+ | 1 | Ravi | | 4 | Pooja |
| 6 | Arjun | +----+----------+
syntax: insert into table_name values (value1, value2, value3, ...);example: insert into employee values (1,Raju,24);+----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | Raju | 24 | +----+----------+-----+
syntax: insert into table_name (column1, column2, column3, ...) values (value1, value2, value3, ...);example: insert into employee (id, name) values (3, Tigadi);+----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | Raju | 24 | | 3 | Tigadi | null| +----+----------+-----+
and has 1st row=> 1,Ravi,30,000
and 2nd row=> 4,Pooja,37,000
and 3rd row=> 6,Arjun,28,000
syntax: update table_name set column1=value1,column2=value2...;
example: update employee salary=salary+10000;
+----+----------+--------+ | id | name | salary | +----+----------+--------+ | 1 | Raju | 34,000 | | 4 | Pooja | 47,000 | | 6 | Arjun | 38,000 | +----+----------+--------+
syntax: update table_name set column1=value1,column2=value2... where condition; example: update employee salary=salary+10000 where id<5;
(id with 1 and 4 are smaller than 5 so their salary gets increased by 10,000)
+----+----------+--------+ | id | name | salary | +----+----------+--------+ | 1 | Raju | 34,000 | | 4 | Pooja | 47,000 | | 6 | Arjun | 28,000 | <- is not effected +----+----------+--------+
It is used to delete rows /tuples.
and has 1st row=> 1,Ravi,30
and 2nd row=> 4,Pooja,37
and 3rd row=> 6,Arjun,28
syntax: delete from table_name;example: delete from employee ;+----+----------+-----+ | id | name | age | +----+----------+-----+ | | | | | | | | | | | | +----+----------+-----+
syntax: delete from table_name where condition;example: delete from employee where name='Pooja';+----+----------+-----+ | id | name | age | +----+----------+-----+ | 1 | Ravi | 30 |
| 6 | Arjun | 28 | +----+----------+-----+
No comments