Database Concepts

A database is a container that holds tables and other SQL structures related to those tables.

A DBMS (Database Management System) is a software used to create and manage databases. MySQL, Oracle, SQL Server, Microsoft Access are some examples of DBMSs.

Database Schema means the design of the database. It represents the database structure.

Data Constraint are restrictions on data to ensure accuracy and reliability of data.

Data Dictionary (Metadata) is the data that describes other data.

Database Instance is the state or snapshot of the database at any given time.

A query is a request to a database to obtain desired information.

Data Manipulation means inserting, deleting, or updating data in a database.

The Database Engine is a set of programs to create and manage the database.

A data model describes the structure of the database. RDBMS (Relational DBMS) is the most commonly used data model.

In an RDBMS, the tables are called relations. The rows in a table are called tuples or records. The columns in a table are called attributes or fields.

A domain is the set of possible values for a given column.

Degree is the number of columns in a table.

Cardinality is the number of rows in a table.

Each column in a relation has a unique name.
The arrangement of the columns in a relation has no significance.
Each row in a relation should be unique.
The arrangement of the rows in a relation has no significance.
All values of a column must have the same data type.
All values in a column must be atomic (indivisible) in nature.
A special value NULL is entered for unknown values.

Each column that is capable of identifying a row uniquely in a table is a candidate key.

The column chosen to uniquely identify a row in a table is the primary key.

If multiple columns are chosen to identify a row uniquely in a relation, it is known as the composite primary key.

A foreign key is used to represent the relationship between two tables. It is a column whose values are derived from the primary key of another table.

SQL (Structured Query Language) is the standard language for DBMSs. It is easy to learn.

MySQL is an open-source RDBMS that can be downloaded from mysql.com website.

SQL commands are not case-sensitive.
SQL commands always end with a semicolon.

Commonly used Data Types in SQL

Data TypeDescription
char(n)To store fixed-length character strings (0 to 255 characters).
varchar(n)To store variable-length character strings (0 to 65535 characters).
intTo store integer values (4 bytes storage space). The bigint data type occupies 8 bytes and thus has a bigger range.
floatTo store numbers with decimal point (4 bytes storage space).
dateTo store dates in YYYY-MM-DD format.

SQL Commands

To create a database:
create database student;

To list the existing databases:
show databases;

To use a database:
use student;

To list the tables:
show tables;

To create a table:
create table students(roll int, name varchar(20), dob date, ac char(12), primary key(roll));

To view the structure of a table:
describe students;
or
desc students;

To add a primary key in a table:
alter table students add primary key(roll);

To add a foreign key in a table:
alter table students add foreign key(ac) references guardian(ac);

To add a unique constraint:
alter table teacher add unique(phone);

To add a new column:
alter table teacher add salary float;

To change the data type of a column:
alter table teacher modify salary int;

To add a constraint not null to a column in a table:
alter table teacher modify salary float not null;

To add default value to a column:
alter table teacher modify salary float default 50000.0;

To remove a table:
alter table students drop ac;

To remove primary key from a table:
alter table students drop primary key;

To delete a table:
drop table students;

To delete a database:
drop database student;

To add a row:
insert into students values(101, 'SUNDAR PICHAI', '2003-07-26');

To retrieve data from all columns in a table:
select * from students;

To retrieve data from specific columns:
select name, dob from students;

To rename columns while displaying the output:
select name as 'full name', dob as birth from students;

To retrieve column values without duplicates:
select distinct course from students;

To retrieve data from columns that satisfies certain conditions:
select name from students where course = 'MCA';
select * from teachers where salary > 80000 and dept = 'CS';
select * from teachers where not dept = 'CS';
select * from teachers where salary between 50000 and 80000;
select * from teachers where dept = 'CS' or salary > 50000;
select * from teachers where dept in('CS', 'COMMERCE', 'ARTS');
select * from teachers order by salary;
select * from teachers order by salary desc;
select * from students where ac is null;
select * from students where ac is not null;

Leave a Reply

Your email address will not be published. Required fields are marked *