Professional Documents
Culture Documents
1. Basic terminologies
2. Types of SQL
3. SQL constraints
4. Functions in SQL
5. SQL joins
Why we need data base?
1. Traditionally, data was organized in file formats but data fetching
and writing would not be so fast and easy with those type of
systems.
2. Managing large amount of data is a problem
3. Using spreadsheets you can handle but it affects efficiency
4. DB provides flexibility
5. Provides security
What is DBMS?
• Data Base Management System is a software used to store and manage
data or we can say it is a collection of programs which enables users to
access database, manipulate data, report/ represent data.
• DBMS stores data in such a way that it becomes easier to retrieve,
manipulate, and produce information.
• DBMS was first implemented in 1960s.
• Charles Bachman’s Integrated Data Store(IDS) is considered as the
first DBMS in history.
What is SQL?
• Structured Query Language – lang to communicate with db.
• RDBMS – type of db.
• In this data is stored in tables, in the form of rows and columns.
• It stores data over a number of tables instead of one table.
• It is used to modify and access data or information from storage area called
database.
• Few features are :
• Provides data to be stored in tables.
• Tables are created using SQL.
• We can retrieve data from tables using SQL.
MySQL Database
• MySQL is a fast, easy-to-use RDBMS being used for many small and
big businesses. MySQL is developed, marketed and supported by
MySQL AB, which is a Swedish company. MySQL is becoming so
popular because of many good reasons −
• MySQL is released under an open-source license. So you have nothing to pay to use it.
• MySQL works on many operating systems and with many languages including PHP,
PERL, C, C++, JAVA, etc.
• MySQL works very quickly and works well even with large data sets.
• MySQL supports large databases, up to 50 million rows or more in a table. The default
file size limit for a table is 4GB, but you can increase this (if your operating system can
handle it) to a theoretical limit of 8 million terabytes (TB).
Basic technologies :
1. Table :
Data in database is stored in the tables. Table consists of rows and
columns.
eg,.,
Person
ID Name Age Address Salary
drop delete
truncate
SQL Constraints :
• Constraints are simply restrictions placed on one or more columns of a
table to limit the type of values that can be stored in the column.
• Few constraints are :
1. NOT NULL
2. Primary key
3. Unique Key
1. not null
• It specifies that column does not accept null value.
e.g.,
create table student(id int not null, name varchar(55));
-> table created successfully..
insert into student values(1, ‘James’);
-> Record inserted successfully..
insert into student values(null, ‘Bond’);
-> Column ‘id’ can not be null..
2. Primary key :
• It is a special column assigned to uniquely identify all table records.
• No two rows of a table can have the same primary key value.
• Also you can not insert null value in a primary key.
• In simple words it restricts duplicate values.
e.g., create table student(id int primary key, name varchar(55));
-> table created successfully..
insert into student values(1, ‘James’);
-> Record inserted successfully..
insert into student values(1, ‘James’);
-> Duplicate entry ‘1’ for key primary..
insert into student values(null, ‘Bond’);
-> Column ‘id’ can not be null..
• Only one column can be set as primary key.
e.g., create table student(id int primary key, name varchar(55) primary key);
-> Multiple primary key defined..
3. Unique key :
• It also ensures data uniqueness like primary key.
• Only differences are that,
• E.g.,
create table student(id int primary key, name varchar(55) unique key,
salary int unique key);
-> table created successfully..
insert into student values(1, ‘James’, 10000);
-> Record inserted successfully..
insert into student values(2, ‘Bond’, null);
-> Record inserted successfully..
SQL functions :
• Commonly used functions are as follows :
1. AVG()
2. MAX() Student table :
3. MIN() Rollno Name Marks
1 James 50
4. SUM() 2 Robert 50
5. COUNT() 3 Harry 80
1. Inner join
2. Left join
3. Right join
4. Full join
Two rules for SQL joins :
1. Both the tables should have at least one common column between
them
2. Make sequence, i.e., in which order you want to join both the tables,
decide that first.
• Consider following two tables, we will perform join operations on
these two tables,
• Id from stud, name, surname, marks, age, id from stud_info
Student : id name marks
1 James 50
same column name 2 Harry 60
3 Emma 80
Different id
Student_info :
same column name id surname age
1 Bond 56
2 Potter 25
4 ABC 21
Different id
1. Inner join :
• Common elements of both tables.
2. Left join :
• All elements from left side table.
3. Right join :
• All elements from right side table.
4. Full join :
• All the elemnts in both the tables.
• Now first lets decide sequence,
id, name, surname, marks, age, id
id -> student table
name -> student table
surname -> student_info table
marks -> student table
age -> student_info table
id -> student_info table
Inner join query :
• select student.id, student.name, student_info .surname, student.marks,
student_info .age, student_info .id
from student join student_info
on student.id = student_info.id;