0% found this document useful (0 votes)
14 views

SQL Notes-Week - 1

SQL notes

Uploaded by

bhoomikanekal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SQL Notes-Week - 1

SQL notes

Uploaded by

bhoomikanekal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL NOTES

Data:
Data is a collection of a distinct small unit of information. It can be used in a variety of forms like
text, numbers, media, bytes, etc. it can be stored in pieces of paper or electronic memory, etc.

Database:
A database is an organized collection of data, so that it can be easily accessed and managed.

You can organize data into tables, rows, columns, and index it to make it easier to find relevant information.

Types of Databases:
• Relational Database - SQL
• Non-Relational Database – NO SQL

Difference Between Relational Database and Non-Relational Database

SQL:SQL is a standard language for accessing and manipulating databases.


• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases

What Can SQL do?


• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views

Database Management System[DBMS]


o Database management system is a software which is used to manage the database. For
example: MySQL, Oracle, etc are a very popular commercial database which is used in
different applications.

Types of DBMS:

• RDMS – MySQL, Oracle, SQL Server, PostgreSQL etc.


• NoSQL DBMS-MongoDB, Cassandra etc.
CREATE DATABASE Statement
Syntax
CREATE DATABASE databasename;

DATABASE LIST Statement


Syntax
SHOW DATABASES;

SELECT A DATABASE FROM THE DATABASE LIST Statement


Syntax
USE databasename ;

TO SEE THE PRESENT DATABASE Statement


Syntax
SELECT DATABASE();

The SQL DROP DATABASE Statement


Syntax
DROP DATABASE databasename;
The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a new table in a database.

Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

The SQL DROP TABLE Statement


The DROP TABLE statement is used to drop an existing table in a database.

Syntax
DROP TABLE table_name;

SQL TRUNCATE TABLE


The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table
itself.

Syntax
TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;
ALTER TABLE - RENAME COLUMN
To rename a column in a table, use the following syntax:

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

ALTER TABLE - ALTER/MODIFY DATATYPE


To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_name


Modify COLUMN column_name datatype;

SQL Constraints: SQL constraints are used to specify rules for data in a
table.

• NOT NULL - Ensures that a column cannot have a NULL value


• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row
in a table
• DEFAULT - Sets a default value for a column if no value is specified

AUTO INCREMENT Field


Auto-increment allows a unique number to be generated automatically when a new
record is inserted into a table.

Example:

CREATE TABLE emp_info(

Emp_id int PRIMARY KEY AUTO_INCREMENT,

firstname varchar(30) NOT NULL ,

lastname varchar(20),

email_id varchar(25) UNIQUE KEY,

Age int ,

salary int NOT NULL,

location varchar(50) DEFAULT "Bangalore"

);
Difference Between DROP TRUNCATE and DELETE

INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.

Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE Statement
The DELETE statement is used to delete existing records in a table.

Syntax
DELETE FROM table_name WHERE condition;

SELECT Statement
The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

Syntax
SELECT column1, column2, ...
FROM table_name;
To get all data,
SELECT * FROM tablename;

You might also like