SlideShare ist ein Scribd-Unternehmen logo
1 von 28
SQL
How to Work With SQL ?
What is database ??
Database is a systematic collection of data. Databases support storage and
manipulation of data. Databases make data management easy.
How to create a database in SQL ??
The CREATE DATABASE statement is used to create a new SQL database.
Syntax :- CREATE DATABASE databasename;
DROP - Database
The DROP DATABASE statement is used to drop an existing SQL database.
Dropping of the database will drop all database objects (tables, views, procedures
etc.) inside it.
Syntax :- DROP DATABASE databasename;
Note: Be careful before dropping a database. Deleting a database will result in loss
of complete information stored in the database!
BACKUP
The BACKUP DATABASE statement is used in SQL Server to create a full back
up of an existing SQL database.
Syntax :- BACKUP DATABASE databasename
TO DISK = 'filepath';
A differential back up only backs up the parts of the database that have changed
since the last full database backup.
Syntax :- BACKUP DATABASE databasename
TO DISK = 'filepath'
WITH DIFFERENTIAL;
CREATE TABLE
The CREATE TABLE statement is used to create a new table in a database.
Syntax :- CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE - Example
Command:- CREATE TABLE Persons(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DROP - Table
The DROP TABLE statement is used to drop an existing table in a database.
Syntax :- DROP TABLE table_name;
The TRUNCATE TABLE statement is used to delete the data inside a table, but
not the table itself.
Syntax :- TRUNCATE TABLE table_name;
ALTER - Table
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
ALTER to ADD Syntax :- ALTER TABLE table_name
ADD column_name datatype;
ALTER to DROP Syntax :- ALTER TABLE table_name
DROP COLUMN column_name;
ALTER - Table
ALTER to MODIFY Syntax :- ALTER TABLE table_name
ALTER COLUMN column_name datatype;
Or
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Or
ALTER TABLE table_name
MODIFY column_name datatype;
CONSTRAINTS
Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement.
Syntax :- CREATE TABLE table_name (column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
CONSTRAINTS
The following constraints are commonly used in SQL:
● NOT NULL - Ensures that a column cannot have a NULL value
● UNIQUE - Ensures that all values in a column are different
● PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
● FOREIGN KEY - Uniquely identifies a row/record in another table
● CHECK - Ensures that all values in a column satisfies a specific condition
● DEFAULT - Sets a default value for a column when no value is specified
● INDEX - Used to create and retrieve data from the database very quickly
CONSTRAINTS
NOT NULL - By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert
a new record, or update a record without adding a value to this field.
NOT NULL on CREATE TABLE :- CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName
varchar(255) NOT NULL,
Age int
);
CONSTRAINTS
NOT NULL on ALTER TABLE :- ALTER TABLE Persons
MODIFY Age int NOT NULL;
CONSTRAINTS
UNIQUE :- The UNIQUE constraint ensures that all values in a column are
different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
CONSTRAINTS
Syntax :- CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
CONSTRAINTS
PRIMARY KEY :- The PRIMARY KEY constraint uniquely identifies each record
in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key, which may consist of single or multiple
fields.
Create Table Syntax : - CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID));
CONSTRAINTS
Alter Table Syntax : - ALTER TABLE Persons
ADD PRIMARY KEY (ID);
Drop Table Syntax : - ALTER TABLE Persons
DROP PRIMARY KEY;
CONSTRAINTS
FOREIGN KEY :- A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
Create Table Syntax : - CREATE TABLE Orders ( OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY
(OrderID),
FOREIGN KEY
(PersonID) REFERENCES Persons(PersonID));
CONSTRAINTS
Alter Table Syntax : - ALTER TABLE Orders
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
Drop Table Syntax : - ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;
CONSTRAINTS
CHECK :- The CHECK constraint is used to limit the value range that can be
placed in a column.
If you define a CHECK constraint on a single column it allows only certain values
for this column.
If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
Create Table Syntax : - CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255)
NOT NULL,
FirstName
varchar(255),
Age int,
CONSTRAINTS
Alter Table Syntax : - ALTER TABLE Persons
ADD CHECK (Age>=18);
Drop Table Syntax : - ALTER TABLE Persons
DROP CHECK CHK_PersonAge;
CONSTRAINTS
DEFAULT :- The DEFAULT constraint is used to provide a default value for a
column.
The default value will be added to all new records IF no other value is specified.
Create Table Syntax : - CREATE TABLE Persons ( ID int NOT NULL,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT
'Sandnes' );
CONSTRAINTS
Alter Table Syntax : - ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';
Drop Table Syntax : - ALTER TABLE Persons
ALTER City DROP DEFAULT;
CONSTRAINTS
INDEX :- The CREATE INDEX statement is used to create indexes in tables.
Indexes are used to retrieve data from the database very fast. The users cannot see
the indexes, they are just used to speed up searches/queries.
Create Table Syntax : - CREATE INDEX index_name
ON table_name (column1, column2, ...);
Create Unique Index Syntax:- CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
CONSTRAINTS
Alter Table Syntax : - ALTER TABLE table_name
DROP INDEX index_name;
AUTO INCREMENT
Auto-increment allows a unique number to be generated automatically when a
new record is inserted into a table.
Often this is the primary key field that we would like to be created automatically
every time a new record is inserted.
Syntax :- CREATE TABLE Persons (ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID));
DATES
MySQL comes with the following data types for storing a date or a date/time value
in the database:
● DATE - format YYYY-MM-DD
● DATETIME - format: YYYY-MM-DD HH:MI:SS
● TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
● YEAR - format YYYY or YY
Syntax : - SELECT * FROM Orders WHERE OrderDate='2008-11-11'
VIEW
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present
the data as if the data were coming from one single table.
Syntax : - CREATE VIEW view_name AS
SELECT column1, column2, …
FROM table_name
WHERE condition;

Weitere ähnliche Inhalte

Was ist angesagt? (19)

Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
SQL-Alter Table, SELECT DISTINCT & WHERE
SQL-Alter Table, SELECT DISTINCT & WHERESQL-Alter Table, SELECT DISTINCT & WHERE
SQL-Alter Table, SELECT DISTINCT & WHERE
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Les10
Les10Les10
Les10
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Sql basics
Sql  basicsSql  basics
Sql basics
 
mySQL and Relational Databases
mySQL and Relational DatabasesmySQL and Relational Databases
mySQL and Relational Databases
 
Sql
SqlSql
Sql
 
SQL
SQLSQL
SQL
 
Sql12
Sql12Sql12
Sql12
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL
 
1 ddl
1 ddl1 ddl
1 ddl
 
SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2
 
MY SQL
MY SQLMY SQL
MY SQL
 
Sql alter table statement
Sql alter table statementSql alter table statement
Sql alter table statement
 
vFabric SQLFire Introduction
vFabric SQLFire IntroductionvFabric SQLFire Introduction
vFabric SQLFire Introduction
 
Sql – Structured Query Language
Sql – Structured Query LanguageSql – Structured Query Language
Sql – Structured Query Language
 

Ähnlich wie Sql basics

SQL Data types and Constarints.pptx
SQL Data types and Constarints.pptxSQL Data types and Constarints.pptx
SQL Data types and Constarints.pptxjaba kumar
 
Avinash database
Avinash databaseAvinash database
Avinash databaseavibmas
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginnersISsoft
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleFarhan Aslam
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating databaseMukesh Tekwani
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Relational database management system
Relational database management systemRelational database management system
Relational database management systemPraveen Soni
 
MySQL.pptx comuterscience from kvsbbsrs.
MySQL.pptx comuterscience from kvsbbsrs.MySQL.pptx comuterscience from kvsbbsrs.
MySQL.pptx comuterscience from kvsbbsrs.sudhasuryasnata06
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01sagaroceanic11
 
Creating, altering and dropping tables
Creating, altering and dropping tablesCreating, altering and dropping tables
Creating, altering and dropping tablespunu_82
 

Ähnlich wie Sql basics (20)

Sql commands
Sql commandsSql commands
Sql commands
 
SQL Data types and Constarints.pptx
SQL Data types and Constarints.pptxSQL Data types and Constarints.pptx
SQL Data types and Constarints.pptx
 
Introduction to SQL..pdf
Introduction to SQL..pdfIntroduction to SQL..pdf
Introduction to SQL..pdf
 
Avinash database
Avinash databaseAvinash database
Avinash database
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Relational database management system
Relational database management systemRelational database management system
Relational database management system
 
MySQL.pptx comuterscience from kvsbbsrs.
MySQL.pptx comuterscience from kvsbbsrs.MySQL.pptx comuterscience from kvsbbsrs.
MySQL.pptx comuterscience from kvsbbsrs.
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Module 3
Module 3Module 3
Module 3
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
ADV PPT 2 LAB.pptx
ADV PPT 2 LAB.pptxADV PPT 2 LAB.pptx
ADV PPT 2 LAB.pptx
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Creating, altering and dropping tables
Creating, altering and dropping tablesCreating, altering and dropping tables
Creating, altering and dropping tables
 

Mehr von Aman Lalpuria

Mehr von Aman Lalpuria (6)

SWIFT - Clearing and Settlement
SWIFT - Clearing and Settlement SWIFT - Clearing and Settlement
SWIFT - Clearing and Settlement
 
Functors, applicatives & monads
Functors, applicatives & monadsFunctors, applicatives & monads
Functors, applicatives & monads
 
iPython
iPythoniPython
iPython
 
Git & Github
Git & GithubGit & Github
Git & Github
 
Sql
SqlSql
Sql
 
Payment
PaymentPayment
Payment
 

Kürzlich hochgeladen

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 

Kürzlich hochgeladen (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 

Sql basics

  • 1. SQL How to Work With SQL ?
  • 2. What is database ?? Database is a systematic collection of data. Databases support storage and manipulation of data. Databases make data management easy. How to create a database in SQL ?? The CREATE DATABASE statement is used to create a new SQL database. Syntax :- CREATE DATABASE databasename;
  • 3. DROP - Database The DROP DATABASE statement is used to drop an existing SQL database. Dropping of the database will drop all database objects (tables, views, procedures etc.) inside it. Syntax :- DROP DATABASE databasename; Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!
  • 4. BACKUP The BACKUP DATABASE statement is used in SQL Server to create a full back up of an existing SQL database. Syntax :- BACKUP DATABASE databasename TO DISK = 'filepath'; A differential back up only backs up the parts of the database that have changed since the last full database backup. Syntax :- BACKUP DATABASE databasename TO DISK = 'filepath' WITH DIFFERENTIAL;
  • 5. CREATE TABLE The CREATE TABLE statement is used to create a new table in a database. Syntax :- CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
  • 6. CREATE TABLE - Example Command:- CREATE TABLE Persons( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
  • 7. DROP - Table The DROP TABLE statement is used to drop an existing table in a database. Syntax :- DROP TABLE table_name; The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself. Syntax :- TRUNCATE TABLE table_name;
  • 8. ALTER - Table The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER to ADD Syntax :- ALTER TABLE table_name ADD column_name datatype; ALTER to DROP Syntax :- ALTER TABLE table_name DROP COLUMN column_name;
  • 9. ALTER - Table ALTER to MODIFY Syntax :- ALTER TABLE table_name ALTER COLUMN column_name datatype; Or ALTER TABLE table_name MODIFY COLUMN column_name datatype; Or ALTER TABLE table_name MODIFY column_name datatype;
  • 10. CONSTRAINTS Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement. Syntax :- CREATE TABLE table_name (column1 datatype constraint, column2 datatype constraint, column3 datatype constraint, .... );
  • 11. CONSTRAINTS The following constraints are commonly used in SQL: ● NOT NULL - Ensures that a column cannot have a NULL value ● UNIQUE - Ensures that all values in a column are different ● PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table ● FOREIGN KEY - Uniquely identifies a row/record in another table ● CHECK - Ensures that all values in a column satisfies a specific condition ● DEFAULT - Sets a default value for a column when no value is specified ● INDEX - Used to create and retrieve data from the database very quickly
  • 12. CONSTRAINTS NOT NULL - By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field. NOT NULL on CREATE TABLE :- CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int );
  • 13. CONSTRAINTS NOT NULL on ALTER TABLE :- ALTER TABLE Persons MODIFY Age int NOT NULL;
  • 14. CONSTRAINTS UNIQUE :- The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
  • 15. CONSTRAINTS Syntax :- CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, UNIQUE (ID) );
  • 16. CONSTRAINTS PRIMARY KEY :- The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only one primary key, which may consist of single or multiple fields. Create Table Syntax : - CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID));
  • 17. CONSTRAINTS Alter Table Syntax : - ALTER TABLE Persons ADD PRIMARY KEY (ID); Drop Table Syntax : - ALTER TABLE Persons DROP PRIMARY KEY;
  • 18. CONSTRAINTS FOREIGN KEY :- A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. Create Table Syntax : - CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(PersonID));
  • 19. CONSTRAINTS Alter Table Syntax : - ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID); Drop Table Syntax : - ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;
  • 20. CONSTRAINTS CHECK :- The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. Create Table Syntax : - CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int,
  • 21. CONSTRAINTS Alter Table Syntax : - ALTER TABLE Persons ADD CHECK (Age>=18); Drop Table Syntax : - ALTER TABLE Persons DROP CHECK CHK_PersonAge;
  • 22. CONSTRAINTS DEFAULT :- The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified. Create Table Syntax : - CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, City varchar(255) DEFAULT 'Sandnes' );
  • 23. CONSTRAINTS Alter Table Syntax : - ALTER TABLE Persons ALTER City SET DEFAULT 'Sandnes'; Drop Table Syntax : - ALTER TABLE Persons ALTER City DROP DEFAULT;
  • 24. CONSTRAINTS INDEX :- The CREATE INDEX statement is used to create indexes in tables. Indexes are used to retrieve data from the database very fast. The users cannot see the indexes, they are just used to speed up searches/queries. Create Table Syntax : - CREATE INDEX index_name ON table_name (column1, column2, ...); Create Unique Index Syntax:- CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
  • 25. CONSTRAINTS Alter Table Syntax : - ALTER TABLE table_name DROP INDEX index_name;
  • 26. AUTO INCREMENT Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted. Syntax :- CREATE TABLE Persons (ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID));
  • 27. DATES MySQL comes with the following data types for storing a date or a date/time value in the database: ● DATE - format YYYY-MM-DD ● DATETIME - format: YYYY-MM-DD HH:MI:SS ● TIMESTAMP - format: YYYY-MM-DD HH:MI:SS ● YEAR - format YYYY or YY Syntax : - SELECT * FROM Orders WHERE OrderDate='2008-11-11'
  • 28. VIEW In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table. Syntax : - CREATE VIEW view_name AS SELECT column1, column2, … FROM table_name WHERE condition;