SQL Syntax Flashcards
CREATE DATABASE
To store data within a database, you first need to create it. This is necessary to individualize the data belonging to an organization.
CREATE DATABASE sampleDB
USE
Once the database is created, it needs to be used in order to start storing the data accordingly.
USE sampleDB;
DROP DATABASE
If a database is no longer necessary, you can also delete it.
DROP DATABASE sampleDB;
CREATE TABLE
In an SQL driven database, the data is stored in a structured manner, i.e. in the form of tables.
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
…..
columnN datatype,
PRIMARY KEY( one or more columns )
);
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)
);
INSERT INTO
used to insert data into database tables.
INSERT INTO table_name( column1, column2….columnN)
VALUES ( value1, value2….valueN);
INSERT INTO CUSTOMERS VALUES
(1, ‘Ramesh’, 32, ‘Ahmedabad’, 2000.00 ),
(2, ‘Khilan’, 25, ‘Delhi’, 1500),
(3, ‘kaushik’, 23, ‘Kota’, 2000),
(4, ‘Chaitali’, 25, ‘Mumbai’, 6500),
(5, ‘Hardik’, 27, ‘Bhopal’, 8500),
(6, ‘Komal’, 22, ‘Hyderabad’, 4500),
(7, ‘Muffy’, 24, ‘Indore’, 10000);
SELECT
retrieve the result-sets of the stored data from a database table.
SELECT column1, column2….columnN FROM table_name;
UPDATE
When the stored data in a database table is outdated and needs to be updated without having to delete the table
UPDATE table_name
SET column1 = value1, column2 = value2….columnN=valueN
[ WHERE CONDITION ];
UPDATE CUSTOMERS SET ADDRESS = ‘Pune’ WHERE ID = 6;
DELETE
Without deleting the entire table from the database, you can also delete a certain part of the data by applying conditions.
DELETE FROM table_name WHERE {CONDITION};
DELETE FROM CUSTOMERS WHERE ID = 6;
DROP TABLE
To delete a table entirely from a database when it is no longer needed
DROP TABLE table_name;
TRUNCATE TABLE
delete the data of the table but not the table itself.
TRUNCATE TABLE table_name;
ALTER TABLE
alter the structure of a table. For instance, you can add, drop, and modify the data of a column using this statement.
ALTER TABLE table_name
{ADD|DROP|MODIFY} column_name {data_type};
ALTER TABLE CUSTOMERS ADD SEX char(1);
used to change the name of a table as well
ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE CUSTOMERS RENAME TO NEW_CUSTOMERS;
DISTINCT
used to identify the non-duplicate data from a column.
SELECT DISTINCT column1, column2….columnN FROM table_name;
WHERE
used to filter rows from a table by applying a condition
SELECT column1, column2….columnN
FROM table_name
WHERE CONDITION;
AND/OR
used to apply multiple conditions in the WHERE clause
SELECT column1, column2….columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;
IN
used to check whether the data is present in the column or not
SELECT column1, column2….columnN
FROM table_name
WHERE column_name IN (val-1, val-2,…val-N);