AIM Theory: Design Database and Create Tables For Example Student, Employee Etc
AIM Theory: Design Database and Create Tables For Example Student, Employee Etc
AIM Theory: Design Database and Create Tables For Example Student, Employee Etc
THEORY :
Database : A database is an organized collection of structured information, or
data, typically stored electronically in a computer system.
Table : A table is a collection of related data held in a table format within a
database. It consists of columns and rows.
Commands :
1): ‘Create’ command : it is used to create a table or database.
Syntax :
create table table_name
( Column1 datatype,
Column2 datatype,
Column3 datatype
);
Query:
create table employee
( emp_id integer,
emp_name char (20),
emp_salary integer,
emp_grade char(1)
);
Syntax :
Describe table_name;
or
Desc table table_name;
Syntax :
insert into table_name (column1, column2, column3, …)
values (val1, val2, val3, …);
Query :
insert into student(enNo, name, gender, grade, phone)
values(104,’Anurag’,’M’,’a’,8852851);
4): ’Select’ command : it’s used to retrieve or fetch data from database.
We can fetch either the entire table or according to some specific rule.
Syntax:
(1). select * From table_name;
(2). select * From table_name where col_4=102;
(3). select col_1, col_2 from table_name;
(4). select col_1, col_2 from table_name where col_3>500;
Query :
select * from student;
4. Viewing specific record and also specific column using select command
(2) Unique Constraint : This constraint ensures that all values in a column are
unique.
Syntax:
fieldname datatype NOT NULL UNIQUE
(3) Primary Key Constraint : This Constraint uniquely identify each record in a
table. Key must contain unique values and can’t contains null values.
Syntax:
fieldname datatype NOT NULL,
PRIMARY KEY (fieldname),
(4) Default Constraint : use to provide a default value to column. Default value
will be added to all new records if not specified.
Syntax:
fieldname datatype DEFAULT ‘b’
(5) Check Constraint : It is used to limit value range that can be placed in
column.
Syntax:
fieldname datatype check(fieldname>18),
(6) Foreign Key Constraint : when key/Field in a table playing role of primary
key and in another table, it is non primary key then non primary key will be
called as foreign key.
Syntax:
foreign key (fieldname)
references table_name(fieldname)
QUERIES :
THEORY :
COMMANDS :
1) : Alter Command :
a. This command is used to add, delete, or modify column in existing table.
b. It is also used to add and remove constraints and rename column or table
name.
c. It is also used to modify data type and data type length.
Syntax :
Queries :
2) : Update Command :
Syntax :
update table_name
set column_name=value
where [condition];
Queries :
• update student
set grade ‘B’
where rollno=101;
• update employee
set emp_salary=emp_salary+1000 and emp_grade=’A’
where emp_salary<5000;
Query :
Delete from student
Where rollno=101;
1. use alter command to add new attribute in student table
❖ SQL Softwares
❖ Applications
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
❖ View Definition
It includes commands for defining views.
❖ Transaction Control
It includes commands for specifying beginning and ending along with commands to
have a control over transaction procession.
❖ Authorization
It includes commands for specifying access right to relation and views.
❖ Integrity
Integrity is ensuring that the logical and physical data stored in SQL Server are
structurally sound and consistent.
AIM : Write SQL statements for implementing functions MAX(), MIN(),
SUM(), AVG(), COUNT().
THEORY :
FUNCTIONS :
1) MAX() Function
The MAX() function returns the largest value of the selected column.
Syntax :
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Query :
Select max(bonus) from employee_72;
2) MIN() Function
The MIN() function returns the smallest value of the selected column.
Syntax :
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Query :
Syntax :
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Query :
4) COUNT() Function
The COUNT() function returns the number of rows that matches a specified criterion.
Syntax :
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Query :
5) SUM() Function
Syntax :
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Query :