You are on page 1of 13

SQL

Database
A database is a collection of information that is organized so that it can be easily accessed,
managed and updated. Computer databases typically contain aggregations of data records
or files.

SQL
SQL is a database computer language designed for the retrieval and management of data in a
relational database. SQL stands for Structured Query Language.

What is SQL?

Structured Query Language (SQL) is the standard language for data manipulation in a DBMS. In
in simple words its used to talk to the data in a DBMS. Following are types of SQL Statements

1. Data Definition Language (DDL) allows you to create objects like Schemas, Tables in the
database
2. Data Control Language (DCL) allows you to manipulate and manage access rights on
database objects
3. Data Manipulation Language (DML) is used for searching, inserting, updating, and
deleting data, which will be partially covered in this SQL tutorial.

What is Query?

A Query is a set of instruction given to the database management system. It tells any database
what information you would like to get from the database. For example, to fetch the student
name from the database table STUDENT, you can write the SQL Query like this:

SELECT Student_name from STUDENT;

Why should you learn SQL?

SQL is an easy-to-learn language specifically designed to work with databases. There is a


growing demand for professionals who can handle databases. Almost every big company is using
SQL. It is widely used in various sectors like ticket booking, banking, social media platforms,
data sharing, eCommerce, etc., so there are vast opportunities available for the SQL developer.
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

CREATE DATABASE

CREATE DATABASE statement is used to create a new SQL database.

Syntax
CREATE DATABASE databasename;

The following SQL statement creates a database called "testDB":

Example
CREATE DATABASE testDB;

USE DATABASE
When you have multiple databases in your SQL Schema, then before starting your operation,
you would need to select a database where all the operations would be performed.
The SQL USE statement is used to select any existing database in the SQL schema.

Syntax
The basic syntax of the USE statement is as shown below –

USE DatabaseName;
CREATE TABLE
The SQL CREATE TABLE statement is used to create a new table.

Syntax

The basic syntax of the CREATE TABLE statement is as follows −


CREATE TABLE table_name(
column1datatype,
column2datatype,
column3datatype,
.....
columnNdatatype,
PRIMARY KEY( one or more columns )
);

The EmpId column is of type int and will hold an integer.

The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters and the maximum length for these fields is 255 characters.

The SQL INSERT INTO Statement

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

INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:


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

2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

The SQL 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.

SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you
want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;


The SQL DELETE Statement

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


DELETE Syntax
DELETE FROM table_name WHERE condition;
SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of single
or multiple columns (fields).

CREATE TABLE Student (


Roll_numberint NOT NULL PRIMARY KEY,
Name varchar(255) NOT NULL,
Batch varchar(255),
Phone_numbervarchar(255),

Citizen_IDvarchar(255)
);

The roll number attribute can never have identical and NULL value, because every student
enrolled in a university can have unique Roll_number, therefore two students cannot have same
Roll_number and each row in a table is uniquely identified with student’s roll number. So, we
can make Roll_number attribute as a primary key.

SQL ALTER TABLE Statement


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

The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
Importing SQL DATABASE TO EXCEL

 Create a new workbook in MS Excel


 Click on DATA tab
 Select from Other sources button
 Select from SQL Server as shown in the image
1. Enter the server name/IP address. For this tutorial, am connecting to localhost 127.0.0.1
2. Choose the login type. If you are on a local machine and you have windows
authentication enabled.
3. If you are connecting to a remote server, then you will need to provide user id and
password details.
4. Click on next button
Once you are connected to the database server. A window will open, you have to enter
all the details as shown in screenshot.

 Select EmployeesDB from the drop down list


 Click on employees table to select it
 Click on next button.

It will open a data connection wizard to save data connection and finish the process of
connecting to the employee's data.
 Click on OK button
 SQL data imported to Excel

You might also like