0% found this document useful (0 votes)
15 views28 pages

SQL Update

This document discusses SQL database concepts including creating and managing databases and tables, performing queries, and applying constraints. It covers how to create, select, and drop databases. It also covers how to create tables with various data types and constraints, perform queries, alter and delete tables, and apply constraints like primary keys, foreign keys, and checks. Temporary tables are also discussed as well as cloning, renaming, truncating and dropping tables.

Uploaded by

Duggi vamshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
15 views28 pages

SQL Update

This document discusses SQL database concepts including creating and managing databases and tables, performing queries, and applying constraints. It covers how to create, select, and drop databases. It also covers how to create tables with various data types and constraints, perform queries, alter and delete tables, and apply constraints like primary keys, foreign keys, and checks. Temporary tables are also discussed as well as cloning, renaming, truncating and dropping tables.

Uploaded by

Duggi vamshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 28

Topics

1 SQL Database
2 SQL Table
3 SQL Queries
4 SQL Views
5 SQL Operators and Clauses
6 SQL Joins
7 SQL Keys
8 SQL Functions
1.1 Create databases
Syntax
CREATE DATABASE DatabaseName;
Example
CREATE DATABASE testDB;
List databases
SHOW DATABASES;
1.2 Drop Database
DROP DATABASE DatabaseName;

Example:
DROP DATABASE testDB1;

Deleting Multiple Databases


DROP DATABASE testDB3, testDB4;
1.3 Select databases
Syntax
CREATE DATABASE DatabaseName;
Example
CREATE DATABASE testDB;
List databases
SHOW DATABASES;
Select databases
USE tsetDB;
1.4 Rename Databases
Synatx:
RENAME DATABASE OldDatabaseName TO NewDatabaseName;

Example:
RENAME DATABASE testDB TO Test;
1.5 Show databases
SHOW DATABASES;
Database
performance_schema
information_schema
mysql
testDB

SHOW DATABASES LIKE 'test%’;


Database (test%)
testDB
1.6 Backup databases

Syntax
BACKUP DATABASE database_name
TO DISK = 'filepath’
GO

SQL> BACKUP DATABASE testDB


TO DISK = 'D:\testDB.bak'
GO
2.1 Create Table
Syntax:

CREATE TABLE IF NOT EXISTS table_name( CREATE TABLE IF NOT EXISTS CUSTOMERS(
column1 datatype, ID INT NOT NULL,
column2 datatype, NAME VARCHAR (20) NOT NULL,
column3 datatype, AGE INT NOT NULL
..... ADDRESS CHAR (25),
columnN datatype, SALARY DECIMAL (18, 2),
PRIMARY KEY( one or more columns ) PRIMARY KEY (ID)
); );
2.2 Show Tables
Syntax:
USE testDB;
SHOW TABLES;

Output:

Tables_in_testDB
CALENDAR
CUSTOMERS
COMPANIES
SALARY
2.3 Rename table
Syntax:
RENAME TABLE table_name TO new_table_name;

Example:
RENAME TABLE CUSTOMERS to BUYERS;
2.4 Truncate Table
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE CUSTOMERS;
Verification:
SELECT * FROM CUSTOMERS;
Output
Empty set (0.00 sec)
Differences Between Delete , Truncate and Drop Commands

The DELETE command in SQL


removes one or more rows SQL's TRUNCATE command is The DROP command in SQL
from a table based on the used to remove all of the removes an entire table from
conditions specified in a rows from a table, regardless a database including its
WHERE Clause of whether or not any definition, indexes,
conditions are met. constraints, data etc.

It deletes rows one at a time It removes all of the The table space is completely
and applies same criteria to information in one go. freed from the memory.
each deletion.
2.5 Clone tables –
• There may be a situation when you need an exact copy of a table with the
same columns, attributes, indexes, default values and so forth.
• Instead of spending time on creating the exact same version of an existing
table, you can create a clone of the existing table.

Syntax
CREATE TABLE new_table SELECT * FROM original_table;

Example
CREATE TABLE NEW_CUSTOMERS SELECT * FROM CUSTOMERS;

Verification:
SELECT * FROM NEW_CUSTOMERS;
2.6 Temporary tables
• Temporary tables are pretty much what their name describes: they are the
tables which are created in a database to store temporary data.
• We can perform SQL operations similar to the operations on permanent tables
like CREATE, UPDATE, DELETE, INSERT, JOIN, etc. But these tables will be
automatically deleted once the current client session is terminated.
• In addition to that, they can also be explicitly deleted if the users decide to
drop them manually.
Syntax:
CREATE TEMPORARY TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

DROP Temporary Table : Use #


DROP TABLE #Customers;
2.7 Alter table
• The SQL ALTER TABLE command is a part of Data Definition Language (DDL)
and modifies the structure of a table.
• The ALTER TABLE command can add or delete columns,
• create or destroy indexes, change the type of existing columns, or
• rename columns or the table itself.
• Syntax
ALTER TABLE table_name [alter_option ...];
• ALTER TABLE − ADD Column
ALTER TABLE table_name ADD column_name datatype;
• Example
ALTER TABLE CUSTOMERS ADD Gender char(1);
ALTER TABLE − DROP COLUMN
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE CUSTOMERS DROP COLUMN Gender;
ALTER TABLE − ADD PRIMARY KEY
Syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
PRIMARY KEY (column1, column2...);
Example
ALTER TABLE EMPLOYEES
ADD CONSTRAINT MyPrimaryKey
PRIMARY KEY(ID);
ALTER TABLE − DROP PRIMARY KEY
Syntax:
ALTER TABLE table_name DROP PRIMARY KEY;
Example:
ALTER TABLE EMPLOYEES DROP PRIMARY KEY;

ALTER TABLE − RENAME COLUMN


Syntax:
ALTER TABLE table_name
RENAME COLUMN old_column_name to new_column_name;
Example:
ALTER TABLE CUSTOMERS RENAME COLUMN name to full_name;
ALTER TABLE − MODIFY DATATYPE
Syntax
ALTER TABLE table_name ALTER COLUMN column_name datatype;
Example:
ALTER TABLE CUSTOMERS MODIFY COLUMN ID DECIMAL(18, 4);
2.8 Drop Tabels
Syntax:
DROP TABLE table_name;

Describe Tables
DESC Table

Example:

DROP TABLE CUSTOMERS;

DROP TEMPORARY TABLE TEMP_TABLE;


2.9 Delete Table
Syntax:
DELETE FROM table_name;

Example:
DELETE FROM CUSTOMERS WHERE NAME=‘Suresh’;

Deleting rows based on multiple conditions


DELETE FROM table_name
WHERE condition1 AND condition2 OR ... conditionN;

Example
DELETE FROM CUSTOMERS
WHERE NAME='Komal' OR ADDRESS='Mumbai';
Deleting all the records in a table
DELETE FROM CUSTOMERS;
2.10 SQL Constraints
NOT NULL Constraint
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2)
);
UNIQUE Key Constraint
CREATE TABLE CUSTOMERS (
ID INT NOT NULL UNIQUE,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2)
);
DEFAULT Value Constraint
CREATE TABLE CUSTOMERS (
ID INT NOT NULL UNIQUE,
NAME VARCHAR (20) DEFAULT 'Not Available',
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2)
);
PRIMARY Key Constraint
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
FOREIGN Key Constraint
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT FOREIGN KEY REFERENCES CUSTOMERS(ID),
AMOUNT DECIMAL,
PRIMARY KEY (ID)
);
CHECK Value Constraint
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK(AGE>=18),
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

You might also like