DATABASE CONCEPTS
1. Database - A database is collection of interrelated data; a database system is basically a
computer record keeping system.
2. Properties of database
1. Databases Reduces Redundancy
2. Database Controls Inconsistency
3. Database facilitate Sharing data
4. Database ensures Security
5. Database maintains Integrity
6. Database enforces Standard
3. Common Databases (RDBMS)
1. Oracle, SQL Server, DB2, Foxpro etc – These are proprietary RDBMS
2. MySQL, SQLite, PostgreSQL etc – these are open source RDBMS
4. Definitions
Database. - It refers to collection of logically related data.
Data Redundancy. - The duplication of data is known as data redundancy.
Data Inconsistency. - Multiple mismatching copies of same data represent data inconsistency.
Data Security. - Protection of data against accidental or intentional disclosure to unauthorized
persons, or unauthorised modification or destruction.
Data Privacy. - Rights of individuals and organizations to determine for themselves when,
how and to what extent information about them is to be transmitted to others.
Relational Data Model. The data model wherein the data is organized into tables called
relations. Relationship among multiple tables is established on the basis of common column.
Attribute.- A column in a relation is called attribute.
Tuple - A row in a relation is called tuple.
Degree.- Number of attributes in a relation is called its degree.
Cardinality.- Number of tuples in a relation is called its
cardinality.
Domain.- A pool of values where from a field can draw values is called domain.
Relation. A table having non-empty atomic values with unordered rows and columns is
a relation. SQL. Structured Query Language. A non-procedural UGL used for querying
upon relational databases. Tuple. A row in a relation is called tuple.
CONSTRAINTS
● NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a
column is specified as NOT NULL then we will not be able to store null in this particular
column any more.
● PRIMARY KEY: A set of one or more attributes that can uniquely identify tuples within
the relation. The Primary Key column can not contain null value.
● FOREIGN KEY: A non-key attribute whose values are derived from the primary key of
some other tables.
● Candidate Key: All attribute combinations inside a relation that can serve as primary key.
● Unique Key - This constraint when specified with a column, tells that all the values in the
column must be unique. That is, the values in any row of a column must not be repeated.
● CHECK - This constraint helps to validate the values of a column to meet a particular
condition. That is, it helps to ensure that the value stored in a column meets a specific
condition.
● DEFAULT: This constraint specifies a default value for the column when no value is specified
by the user.
STRUCTURED QUERY LANGUAGE(SQL)
SQL (Structured Query Language) is a language that is used to manage data that is held
in a relational database management system. It uses tables to manipulate and retrieve
information from databases for analysis.
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
DATA TYPES
Data types are means to identify the type of data and associated operations for handling it.
MySQL data types are divided into three categories:
● Numeric
● Date and time
● String types
Numeric Datatype
1. int– used for number without decimal.
2. decimal (m, d) – used for floating/real numbers. m denotes the total length of number
and d is number of decimal digits.
Date and Time Datatype
1. date–used to store date in YYYY-MM-DD format.
2. time–used to store time in HH:MM: SS format.
String Datatype
1. char(m)–used to store a fixed length string, m denotes max. number of characters.
2. varchar(m)–used to store a variable length string, m denotes max. no. of characters.
Q. Are NULL values are same as a zero or a blank space?
Ans No, A NULL value isn’t the same as a zero or a blank space. A zero is a legal numeric value
and a blank space is a legal character value, whereas NULL is a legal empty value that
cannot be accessed or compared with other values.
Q. What is Difference between DDL and DML
DDL DML
It stands for Data Definition Language. It stands for Data Manipulation Language.
It basically defines the column (Attributes) of the It add or updates the row of the table. These rows
table. are called tuple.
Basic command present in DDL are CREATE, BASIC command present in DML
DROP, ALTER etc. are UPDATE, INSERT etc.
DDL is used to define the structure of a DML is used to manipulate the data within the
database. database.
Q. What is Difference between RDBMS and DBMS.
DATABASE COMMANDS
1. VIEW EXISTING DATABASE
To view existing database names, the command is: SHOW DATABASES;
2. CREATING DATABASE IN MYSQL
For creating the database in MySQL, we write the following command:
CREATE DATABASE <databasename>;
3. ACCESSING A DATABASE
For accessing already existing database, we write:
USE <databasename>;
4.
5.
VIEWING TABLE IN DATABASE
6.
In order to view tables, present in currently accessed database, command is:
SHOW TABLES;
5. DELETING DATABASE
For deleting any existing database,the command is:
DROP DATABASE <databasename>;
6. CREATING TABLES IN MYSQL
Syntax of CREATE TABLE command is:
CREATE TABLE <table-name>(<colname> datatype, <colname> datatype,…);
7. Inserting Data into Table:
8. Syntax:
Insert into <tablename> values(<v1>,<v2>,…);
9. Select Command:
It helps to display the records as per our requirement.
10. ELIMINATING REDUNDANT DATA
The distinct keyword is used to eliminate duplicate records from the table.
11. USING COLUMN ALIASES
The columns that we select in a query can be given a different name, i.e. column alias
name for output purpose.
Syntax: SELECT <columnname> AS column alias,
<columnname> AS column alias …..FROM <tablename>;
12. CONDITION BASED ON A RANGE
The BETWEEN operator defines a range of values that the
column values must fall into make the condition true. The range
include both lower value and upper value.
13. CONDITION BASED ON A LIST
The in operator is used to display records based on a list of values.
14. CONDITION BASED ON PATTERN MATCHES
LIKE operator is used for pattern matching in SQL. Patterns are described
using two special wildcard characters: % and _ (underscore)
15. Relational Operators
To compare two values, a relational operator is used. The result of the
comparison is true or false. Relational Operators recognized by SQL: =, >, <, <=,
>=, <> (not equal or !=)
Logical Operators- (OR, AND, NOT)
16. Sorting Results- ORDER BY clause
Results of SQL query can be sorted in a specific order using ORDER BY clause. The
ORDER BY clause allows sorting of query results by one or more columns. The sorting
can be done either in ascending or descending order.
17. DELETE Command
This command removes rows from a table.
Syntax: DELETE FROM <tablename> [WHERE <cond>];
18. UPDATE Command
Update command specifies the rows to be changed using the WHERE clause and
the new data using the SET keyword.
19. ALTER TABLE
ALTER TABLE command is used to change the structure of the existing table. It
can be used to add or drop new columns or modify the existing columns of table.
Eg. 1. Alter table Employee Add comm int;
ALTER TABLE Emp MODIFY (ename varchar(60));
Alter table emp drop comm;
20. DROP TABLE:
DROP TABLE command allows to remove a table from database. Once the
DROP command is issued, the table will no longer be available in the database.