You are on page 1of 6

1.

How to create database in sql


CREATE DATABASE db_name;

2. How to delete database


DROP DATABASE db_name;

3. How to use database


USE db_name;

4. Creating table in database


CREATE TABLE table_name(
col_name1 datatype constraint,
col_name2 datatype constraint
);

----------DATABASE RELATED QUERIES---------


CREATE DATABASE IF NOT EXISTS db_name;
DROP DATABASE IF EXISTS db_name;
SHOW DATABASES;
SHOW TABLES;

---------TABLE RELATED QUERIES-------------

SELECT * FROM table_name;

INSERT INTO table_name


(col1,col2)
VALUES
(col1_V1,col2_V2),
(col1_v2,col2_V2);

----------CONSTRAINTS---------
SQL CONSTRAINTS ARE USED TO SPECIFY RULES FOR DATA IN TABLE.

NOT NULL ---> columns cannot have a null value


UNIQUE -----> all values in column are diffrent
PRIMARY KEY --> all values in column are unique and not null
DEFAULT ---> set the default values of a column
ex-> salary INT DEFAULT 25000

-----PRIMARY KEY SYNTAX ----


CREATE TABLE temp(
id INT,
rollno INT NOT NULL,
PRIMARY KEY(id)
);

-----CONSTRAINTS CHECK ----


CREATE TABLE city(
id INT PRIMARY KEY,
city VARCHAR(50),
age INT,
CONSTAINT age_check CHECK (age >=20 AND city = "DELHI")

Quick Notes Page 1


CONSTAINT age_check CHECK (age >=20 AND city = "DELHI")
); ^
|
|
|
it can be ignored or it can by written

----------FORIGEN KEY CONSTRAINT-----------


CREATE TABLE temp (
cust_id INT,
FORIGN KEY (cust_id) REFERENCES customer(id)
);

-----------WHERE CLAUSE------------
TO DEFINE SOME CONDITIONS

SELECT * FROM table_name WHERE CONDITIONS;

EX- SELECT * FROM student WHERE marks > 80;


- SELECT * FROM student WHERE city = "Mumbai";

OPERATORS
AND (to check for both conditons to be true)
SELECT * FROM student WHERE marks > 80 AND city = "Mumbai";

OR (to check for one of the conditions to be true)


SELECT * FROM student WHERE marks > 90 OR city = "Mumbai";

BETWEEN(selects for a given range)


SELECT * FROM student WHERE marks BETWEEN 80 AND 90;

IN (matches any value in the list)


SELECTS * FROM student WHERE city IN("DELHI","MUMABI");

NOT IN(to negate the given condition)


SELECT * FROM student WHERE city NOT IN("DELHI", "MUMBAI")

---------------------LIMIT CLAUSE----------------
Sets an upper limit on number of (tuples) rows to be returned

SELECT * FROM student LIMIT 3;

----------------------ORDER BY CLAUSE-----------
to sort in ascending (ASC) or decending order (DESC);
SELECT * FROM student ORDER BY city ASC;

-----------------AGGREGATE FUNCTIONS-----------
aggregate function perform a calculation on a set of values, and return a single value.
COUNT()
MAX()
MIN()
SUM()

Quick Notes Page 2


SUM()
AVG()

------------GROUP BY CLAUSE----------------------
group rows that have the same values into summary rows.
it collcts data from multiple records and group the result by one or more column.

*Generally we use group by with some aggregation function.

example:-
Count number of students in each city

SELECT city,count(rollno)
FROM student
GROUP BY city;

SELECT city,name,count(rollno)
FROM student
GROUP BY city,name;

-----------------HAVING CLAUSE------------------------
similar to where i.e. applies some condition on rows.
used when we want to apply any condition after grouping.

Count number of students in each city where max marks cross 90.

SELECT count(name),city
FROM student GROUP BY city
HAVING max(marks)>90;

----------------------GENERAL ORDER--------------------

SELECT columns(s)
FROM table_name
WHERE conditon
GROUP BY columns(s)
HAVING condition
ORDER BY column(s) ASC;

example

SELECT city
FROM student
WHERE grade >= "A"
GROUP BY city
HAVING MAX(marks) >=93
ORDER BY city DESC;

Quick Notes Page 3


------------------UPDATE----------------------------------

UPDATE ( to update existing rows)

UPDATE table_namw
SET col1 = val1, col2 = val2
WHERE condition;

EXAMPLE:-
UPDATE student
SET grade = "O"
WHERE grade = "A";

UPDATE student
SET marks = marks + 1;
// NO WHERE CONDITIONS IS REQUIRED

DELETE ( to delete existing rows)

DELETE FROM table_name


WHERE conditions;

DELETE FROM student


WHERE marks < 33;

---------------REVISTING FORIGN KEY----------

CREATE TABLE dept(


id INT PRIMARY KEY,
name VARCHAR(50)
);

CREATE TABLE teacher(


id INT PRIMARY KEY,
name VARCHAR(50),

Quick Notes Page 4


name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES dept(id)
);

-------------------CASCADING IN FOREIGN KEY-------------------------

-----------------------ALTER-----------------------------------
ALTER (to change the schema)

• ADD column
ALTER TABLE table_name
ADD COLUMN column_name datatype constraint;

• DROP column
ALTER TABLE table_name
DROP COLUMN column_name;

• RENAME table
ALTER TABLE table_name
RENAME TO new_table_name;

• CHANGE Column(rename)
ALTER TABE table_name
CHANGE COLUMN old_name new_name new_datatype new _constraint;

• MODIFY Column (modify datatype/constraint)

Quick Notes Page 5


• MODIFY Column (modify datatype/constraint)
ALTER TABLE table_name
MODIFY col_name new_datatpe new_datatype;

EXAMPLE:-

-----------------------TRUNCATE---------------------------------
TRUNCATE(to delete table's data)

TRUNCATE TABLE table_name;

Quick Notes Page 6

You might also like