0% found this document useful (0 votes)
18 views6 pages

SQL_notes

Uploaded by

chaarvi2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

SQL_notes

Uploaded by

chaarvi2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is Database Management System:

A Database Management System (DBMS) is a software that can be used to create


and manage databases. DBMS allows users to create a database, store, manage,
update/modify and retrieve data from that database by users or application
programs. Some examples of DBMS are MySQL, Oracle, PostgreSQL, SQL Server,
Microsoft Access, MongoDB.

What is database?
A database is an organized collection of structured data stored electronically
in a computer and managed by a database management system (DBMS)

Advantages of using DBMS?


Following are the advantages of using a DBMS –
1. Reduction in Redundancy – All the data is stored at one place. There
is no repetition of the same data. This also reduces the cost of storing
data on hard disks or other memory devices.
2. Improved Consistency – The chances of data inconsistencies in a
database are also reduced as there is a single copy of data that is
accessed or updated by all the users.
3. Improved Availability – Same information is made available to
different users. This helps sharing of information by various users of the
database.
4. Improved Security – The DBA can protect the database by using
passwords and restricting users’ database access rights.
5. User Friendly – Because of its user-friendly interface, it reduces users’
dependence on computer specialists to carry out various data-related
actions in a DBMS.

Key Concepts in DBMS:


1. Database Schema:
Database Schema is the design of a database. It is the skeleton of the database that
represents the structure (table names and their fields/columns), the type of data
each column can hold, constraints on the data to be stored (if any), and the
relationships among the tables. Database Schema is also called as the visual or
logical architecture as it tells how the data are organized in a database

2. Data Constraint:
Constraints are the conditions that are put on columns for ensuring accuracy and
reliability of data being stored in a table. Some constraints are NOT NULL, UNIQUE,
PRIMARY KEY etc
3. Meta–Data or Data Dictionary:
The database schema along with various constraints on the data is stored by DBMS
in a database catalog or dictionary, called meta-data. A meta-data is data about the
data.
4. Database Instance:
After creating a database it is empty, that is without data. Further it is possible add,
remove or modify data in database. The data present in database at any state is
known as Database Instance
5. Query:
A query is a request to a database for obtaining information in a desired way. Query
can be made to get data from one table or from a combination of tables.
6. Data Manipulation:
In a database, Inserting or Deleting or Updating of data is referred as Data
Manipulation.
7. Database Engine:
It is the underlying component or set of programs used by a DBMS to create
database and handle various queries for data retrieval and manipulation.

Relational Data Model:


A Data Model gives the way of representing data. Different data models available are
Object–Oriented Data Model, Entity–Relationship Data Model, Document Model,
Hierarchical Model, Relational Data Model etc. If the data has been represented in
form of Relations (Tables) then such a model is called as Relational Data Model.
1. Relation / Table:
A two dimensional representation of data arranged in form of rows and columns is
called a Relation or Table. In the above example, the name of the relation is
STUDENT
2. Attribute:
The column names or characteristics of the table are known as Attributes or Fields.

3. Tuple:
The horizontal subset of a table is called as Tuple or Row or Record. In the above
example, there are 6 tuples.
4. Domain:
It refers to the set of values that can be provided to a column.

5. Degree:
The number of attributes in a table is called as Degree of the table.

6. Cardinality:
The number of tuples in a table is called as Cardinality of the table.
Keys in a Relational Database:
The tuples within a relation must be distinct. It means no two tuples in a table should
have same value for all attributes. Relational data model imposes some restrictions
or constraints on the values of the attributes and how the contents of one relation be
referred through another relation. A few keys used in Relational Database are as
follows

1. Candidate Key:
A Candidate key is an attribute or combination of attributes that can uniquely identify
a row. A table may have more than one candidate key.

2. Primary Key:
A Primary Key is an attribute or combination of attributes that uniquely identifies a
row and shows two characteristics called UNIQUE and NOT NULL. A Primary Key
will be one from the available candidate keys. A table may consist of only one
primary key.

3. Alternate Key:
All the Candidate keys except Primary Key are called as Alternate Keys.

4. Composite Primary Key:


If a Primary Key is having more than one attributes then such a Primary Key is called
as Composite Primary Key.

5. Foreign Key:
A Foreign key is used to link two tables with primary key and foreign key relationship
that enforces referential integrity.

Data Types:
Commonly used data types available in MySQL are:
1. INT: A normal–sized integer that can be signed or unsigned. If signed, the
allowable range is from –2147483648 to 2147483647. If unsigned, the allowable
range is from 0 to 4294967295. Each INT value occupies 4 bytes of storage
2. DECIMAL(M, D): Represents a floating point number. M is the total number of
digits, and D is the number of digits after decimal point.
3. FLOAT: Holds numbers with decimal points. Each float value occupies 4 bytes
4. DATE: A date in YYYY–MM–DD format, between 1000–01–01 and 9999–12–31
5. CHAR(M): A fixed–length string between 0 and 255 characters in length. M is the
length. Providing M is optional and its default value is 1
6. VARCHAR(M): A variable–length string between 0 and 65535 characters in
length. Providing M is compulsory
Difference between CHAR and VARCHAR:
The difference between CHAR and VARCHAR is that CHAR is of fixed–length and
VARCHAR is of variable length.
When a column is given data type as CHAR(n), then all values stored in that column
have this length, i.e. n bytes. If a value is shorter than this length n then blanks are
added, but the size of the value remains n bytes.
VARCHAR specifies a variable length string. When a column is given data type as
VARCHAR(n), then the maximum size of a value is n bytes. Each value that is stored
in this column stores exactly as given, no blanks are added if the length is shorter
than maximum length. However, if the maximum length is exceeded than an error
message will be displayed.

Constraints:
Constraints are certain types of restrictions on the data values that an attribute can
have. They are used to ensure the accuracy and reliability of data.
1. NOT NULL: Ensures that a column cannot have NULL values where NULL means
missing/ unknown/not applicable value.
2. UNIQUE: Ensures that all the values in a column are distinct / unique.
3. DEFAULT: A default value specified for the column if no value is provided.
4. PRIMARY KEY: The column which can uniquely identify each row or record in a
table.
5. FOREIGN KEY: The column which refers to value of an attribute defined as
primary key in another table.

Categories of SQL Commands:


Data Definition Language (DDL) Commands:
The SQL commands used for creating a database, deleting a database, providing
keys such as primary key, foreign key etc on tables are known as DDL commands.
A few examples for DDL commands in SQL are:
1. CREATE DATABASE – Creates a new database
2. CREATE TABLE – Creates a new table
3. ALTER TABLE – Modifies a table
4. DROP TABLE – Deletes a table
These commands will work with table structure not on table data directly (indirectly
may act on table data)

Data Manipulation Language (DML) Commands:


The Query and Update commands on tables are referred as DML commands. A few
examples for DML commands in SQL are:
1. SELECT – Extracts data from a table
2. UPDATE – Updates data in a table
3. DELETE – Deletes data from a table
4. INSERT INTO – Inserts new data into a table
Changing Structure of a Table Using ALTER
Command:
An existing table’s structure can be changed by using ALTER TABLE command.
Adding an attribute to a Table:
Syntax : ALTER TABLE <TableName> ADD <ColumnName> DataType;
Ex : ALTER TABLE Student ADD Grade CHAR(1);

Removing an attribute of a Table:


Syntax : ALTER TABLE <TableName> DROP <ColumnName>;
Ex : ALTER TABLE Student DROP DOB;

Modifying datatype of an attribute of a Table:


Syntax : ALTER TABLE <TableName> MODIFY <ColumnName> <New_Definition>;
Ex : ALTER TABLE Student Class VARCHAR(4);

Add Primary Key constraint to a Table:


Syntax : ALTER TABLE <TableName> ADD PRIMARY KEY(ColumnName);
Ex : ALTER TABLE Student ADD PRIMARY KEY(RollNo);

Removing Primary Key from a table:


Syntax : ALTER TABLE <TableName> DROP PRIMARY KEY;
Ex : ALTER TABLE Student DROP PRIMARY KEY;

Adding Foreign Key constraint to a Table:


Syntax : ALTER TABLE <TableName> ADD FOREIGN KEY(ColumnName)
REFERENCES
<Referenced_TableName>(ColumnName);
Ex : ALTER TABLE Student ADD FOREIGN KEY(RollNo)
REFERENCES Marks(RollNo);

Adding UNIQUE constraint to a Table:


Syntax : ALTER TABLE <TableName> ADD UNIQUE(ColumnName);
Ex : ALTER TABLE Student ADD UNIQUE(RollNo);

Adding DEFAULT constraint to a Table:


Syntax : ALTER TABLE <TableName> MODIFY ColumnName DATATYPE
DEFAULT
Default_Value;
Ex : ALTER TABLE Student MODIFY Gender VARCHAR(10) DEFAULT ‘Male’;
Adding NOT NULL constraint to a Table:
Syntax : ALTER TABLE <TableName> MODIFY ColumnName DATATYPE NOT
NULL;
Ex : ALTER TABLE Student MODIFY Gender VARCHAR(10) NOT NULL;
Relational data model
A database is a collection of tables, and data can be organized in a database
using one or more tables. In the relational data model, data is stored in
structured tables, and relationships can be created between these tables
using primary and foreign keys. This connection between two tables is known
as a relation.
 Attribute: Attributes are the properties of entities or objects stored in a
database. In easy words, you can say that it is a column heading. For
example, in a “Student” table, attributes could include “Name,” “Age,” or
“Grade.”
 Tuple: A tuple is also known as a record; it is a single row in a table.
For example, Rajesh Kumar, 24, a
 Domain: It is a set of values or attributes that share a common meaning
or purpose. For example, the “Grade” attribute could be “A,” “B,” “C,”
“D,” or “E.”
 Degree: Degree refers to the number of columns or attributes in a table.
For example, in a student table, there are three attributes: name, age,
and grade. It means that table has a degree of 3.
 Cardinality: Cardinality refers to the number of rows in a table. For
example, if a student table has 10 records, it means that its cardinality is
10.

Keys in database
Keys are important in DBMS; keys help to make each record uniquely in the
table. Keys are also important for making relations between tables and for
data integrity. There are basically four different types of keys:
 Primary key: A primary key or simply a key is a field that uniquely
identifies a row in a table.
 Candidate key: All the field values that are eligible to be the primary
key are the candidate keys for that table.
 Alternate key: Out of the candidate keys, one or two are made as
primary keys. The others are the alternate keys.
 Foreign key: If a field or a combination of fields of one table can be
used to uniquely identify records of another table, then that particular
field is known as the foreign key.

You might also like