Let’s start with some definitions
Database: a place where you store data. It’s a collection of tables in terms of relational database.
Table: A data structure to store data in form of columns and rows. AKA, a relation.
A row is also called a tuple which contains a record and a column name can be called an attribute. A cell is cross section of a row and a column.
Each column stores some specific kind of data according to its datatype (ex: varchar, number…)
SQL: (Structured Query Language)
initially called SEQUEL (Structured English Query Language). SQL is a domain-specific language to interact with database.
- SQL keywords are case insensitive.
Classification of Commands
Constraints: Rules enforced on data of a table, column wise.
Create Table:
- Constraints on the attributes can be specified in create command itself or later after creating a table, using Alter table command.
- We can specify multiple foreign key constraints on single table.
create table table_name(
col_1 datatype constraint,
col_2 datatype constraint);
Or we can specify constraints after column names.
create table table_name(
col_1 datatype ,
col_2 datatype ,
constraint(col_1),
constraint (col_2) );
Select Clause:
The minimal select statement goes like this-
select <column_name>
from <table_name>
It selects the column called column_name from table called table_name.
To query multiple columns, we use
select <column1, column2 ...>
from <table_name>
To query all the columns, we use * aka asterisk
select * from emp;
Select Distinct values:
select job from emp;
Here we have many jobs repeating and to get only unique records we can use the keyword DISTINCT
select distinct job from emp;
here we get only 5 rows using Distinct. It returns unique values only.
- Distinct will return NULL as one value so If you have null values than it’ll return an empty space as well.
select distinct comm
from emp;
The ‘-’ here in the output represents a null value.