SQL Update
SQL Update
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;
Example:
RENAME DATABASE testDB TO Test;
1.5 Show databases
SHOW DATABASES;
Database
performance_schema
information_schema
mysql
testDB
Syntax
BACKUP DATABASE database_name
TO DISK = 'filepath’
GO
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
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 )
);
Describe Tables
DESC Table
Example:
Example:
DELETE FROM CUSTOMERS WHERE NAME=‘Suresh’;
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)
);