Lecture 1 Overview: Two Things in Backend
Lecture 1 Overview: Two Things in Backend
Programming language
Database
Data base
Mongo DB
SQL
SQL
Structured Query Language, Command Base language, Server-side language for every line we need
server.
LAMP
Linux By Default
APACHE Server
MYSQL database
PHP
Tomcat: Also static webpage server, but less efficient then apache, open source
Download it from File hippoInstall in C ( continue install if error or notification occur) turn off Skype
during installation and loading as well run the file for C/Xampp folder/xamp control-exe(run this)--.
Start sql and Apache
Database name : phpmyadmin
Localhost/phpmyadmin
All the Relational Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their standard database language.
To Built Website That show data from Database, Four things required
Relational Database Management system, in this data stored in object called table consists of data
entries each entry consist of rows and column.
Table broken into small entities called field which contain information about every record
SQL Constraint
Constraint are rule that enforce on data column on table, either be column or table level ensure
accuracy and reliability.
SELECT
UPDATE
INSERT
CREATE
ALERT
DROP
DELETE
USE
SHOW
Numeric
Datetime
Smalldatetime
Date
Time
Character String
Others
Arithmetic
(+ / * - %)
Comparison
Logical
Another Method
);
Export Database
1. Select database
2. Select table
3. Click export tab
4. Name file
5. Format sql
6. Dump some or dump all rows (your selection)
7. First data base
CREATE TABLE IF NOT EXITS
If you create that that is already exist the it makes an error to avoid this use query IF NOT EXISTS
);
Check Constraint
salary INT NOT NULL CHECK (salary >= 3000 AND salary <= 10000),
Default concept
Adding Constraint
Removing Column
Rename table
SELECT STATEMENT
Fetch data from data from table and show the result in form of table
Update
UPDATE "table_name"
SET "column_1" = [new value]
WHERE "condition";
Delete
select
Where statement allow LIKE ,BETWEEN, IN OPERATORS
LIKE:
Wildcard
Is used to match string pattern
Order by
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
UPDATE table_name
SET column1_name = ‘value1’, column2_name =’ value2’,...
WHERE condition;
Delete Data
Drop
Permanently remove data base or table
SQL - Group By
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause.
EXAMPLE IS group by class in school, group same repletion name (city, country, class etc)
The MAX() function returns the largest value of the selected column.
AVG()
SUM()
COUNT()
MINUS()
GroupBY:
More than one column can be specified in the GROUP BY clause, and
more than one function can be included.
SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name;
The SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
The HAVING clause is used to filter the result set based on the result of an
aggregate function. It is typically located near or at the end of the SQL
statement.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Select---from---where----having----orderby
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
Above statement select the column1 and clumn2 from table 1 and column1 and column 2 from table 2
then ON query check the matc h t1 dep wit t2 dep inner join only shows thos department which are
matches.
Left Join: show all data of left table and match data of right table, left table data
that do not match column represent Null
Right Join show all data of right table and match data of left table, right table data
that do not match column represent Null
Full join show all data of right and left tables, data that do not match column
represent Null
SQL - Indexes
Indexes are special lookup tables that the database search engine can use to speed up
data retrieval.
User cannot see the indexes they just used to speed up searches.
Non-Clustered
To drop index
SELECT * FROM tablename USE INDEX (index name) WHERE column name LIKE
‘salman%’
Constraint:
Not Null; ensure value is not null
Foreign key: same column in other table, cannot delete data of first table,
make relation between table
ALTER TABLE tablename ADD FOREIGN KEY (keyname) REFERENCE tablename (colname)
Check Constraint (name= salman)--- check the condition other wise erro
Union:
United tables but after first table entries show the second table
entries
SQL - Views
View same sql query
Act as virtual table ( change apply on view effect the original table)
Used for security purpose to show the specific column and row to the
user/employee.
---------COLUMN SECURITY-----------
CREATE VIEW vwname2 AS SELECT A.*, B.id, B.nam FROM firstdetail AS A INNER
JOIN seconddetail AS B ON B.id=A.Rollno;
ALTER VIEW vwname2 AS SELECT A.*, B.id, B.eml FROM firstdetail as A INNER
JOIN seconddetail AS B ON B.id=A.Rollno; // alter the views
-----------ROW SECURITY---------
CREATE VIEW vwname2 AS SELECT A.*, B.id, B.nam FROM firstdetail AS A INNER
JOIN seconddetail AS B ON B.id=A.Rollno WHERE age=”31”; // where hide rows
--------INSERT IN VIEW-------------
Work as condition
SELECT * FROM tablename WHERE col-id IN (SELECT col-id FROM tablename where
COL>1000/)
Case Statement
EXISTS is a Boolean operator used in a subquery to test whether the inner
query returns any row. If it does, then the outer query proceeds. If not, the
outer query does not execute, and the entire SQL statement returns nothing.
SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE "condition");
Case Statement
SELECT Name, Email, Address, Age , CASE WHEN name='salman' THEN'hello salman'
ELSE 'hello' END AS 'greeting' FROM firstdetail
Identity concepty
(Userid int PRIMARY KEY IDENTITY(2,1),
The CAST function in SQL converts data from one data type to another. For
example, we can use the CAST function to convert numeric data into
character string data.
CAST (field name AS [data type]) from table