SQL Edit
SQL Edit
Low performance with huge volumes of data Easily work with huge volumes of data
Good for one time analysis, quick charts Can automate tasks
RDBMS
Example
•
•
•
•
•
•
•
•
•
•
•
• Data type of a column defines what value the
column can store in table
• Defined while creating tables in database
• Data types mainly classified into three categories +
most used
oString: char, varchar, etc
oNumeric: int, float, bool, etc
oDate and time: date, datetime, etc
• int: used for the integer value
• float: used to specify a decimal point number
• bool: used to specify Boolean values true and false
• char: fixed length string that can contain numbers, letters, and special
characters
• varchar: variable length string that can contain numbers, letters, and
special characters
• date: date format YYYY-MM-DD
• datetime: date & time combination, format is YYYY-MM-DD hh:mm:ss
• A Primary key is a unique column we set in a table to easily identify
and locate data in queries
• A table can have only one primary key, which should be unique and
NOT NULL
• Example
CREATE TABLE customer
(
CustID int8 PRIMARY KEY,
CustName varchar(50) NOT NULL,
Age int NOT NULL,
City char(50),
Salary numeric
);
Insert, Update, Delete
Values in Table
The INSERT INTO statement is used to insert new records in a table
• Syntax
INSERT INTO TABLE_NAME
(column1, column2, column3,...columnN)
VALUES
(value1, value2, value3,...valueN);
• Example
INSERT INTO customer
(CustID, CustName, Age, City, Salary)
VALUES
(1, ‘Sam’, 26, ‘Delhi’, 9000),
(2, ‘Ram’, 19, ‘Bangalore’, 11000),
(3, ‘Pam’, 31, ‘Mumbai’, 6000),
(4, ‘Jam’, 42, ‘Pune’, 10000);
The UPDATE command is used to update existing rows in a table
• Syntax
UPDATE TABLE_NAME
SET “Column_name1” = ‘value1’, “Column_name2” = ‘value2’
WHERE “ID” = ‘value’
• Example
UPDATE customer
SET CustName = 'Xam’, Age= 32
WHERE CustID = 4;
The ALTER TABLE statement is used to add, delete, or modify columns
in an existing table
• ALTER TABLE - ADD Column Syntax
ALTER TABLE table_name
ADD COLUMN column_name ;
• ALTER TABLE - DROP COLUMN Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
• ALTER TABLE - ALTER/MODIFY COLUMN Syntax
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
The DELETE statement is used to delete existing records in a table
• Syntax
DELETE FROM table_name WHERE condition;
• Example
DELETE FROM customer
WHERE CustID = 3;
The DROP TABLE command deletes a table in the database
• Syntax
DROP TABLE table_name;
The TRUNCATE TABLE command deletes the data inside a table, but
not the table itself
• Syntax
TRUNCATE TABLE table_name;
SELECT & WHERE CLAUSE
CREATE TABLE classroom
(
rollno int8 PRIMARY KEY,
name varchar(50) NOT NULL,
house char(12) NOT NULL,
grade char(1)
);
• Example
SELECT name FROM classroom
WHERE grade=‘A’;
The SQL reserved words and characters are called operators, which are
used with a WHERE clause in a SQL query
Most used operators:
1. Arithmetic operators : arithmetic operations on numeric values
Example: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%)
2. Comparison operators: compare two different data of SQL table
• Example: Equal (=), Not Equal (!=), Greater Than (>), Greater Than Equals to (>=)
3. Logical operators: perform the Boolean operations
• Example: ALL, IN, BETWEEN, LIKE, AND, OR, NOT, ANY
4. Bitwise operators: perform the bit operations on the Integer values
• Example: Bitwise AND (&), Bitwise OR(|)
END