You are on page 1of 5

DATABASE: A database is a collection of inter related data that is organized so that its

contents can easily be accessed, managed, and updated. It is an electronic file system

TABLES: a table is a set of data elements (values) that is organized using rows and
columns.

Data Manipulation Language (DML) :

→ Select: The SELECT statement is used to retrieve the data from a database

→ Insert: The INSERT INTO statement is used to insert new records in a table

→ Update: The UPDATE statement is used to update existing records in a table

→ Delete: The DELETE statement is used to delete records in a table

→ Merge:

Data Definition Language (DDL) :

→ Create: The CREATE statement is used to create a database/Table.

→ Alter: The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table/DB

→ Drop: Indexes, tables, and databases can easily be deleted/removed with the DROP
statement

→ Rename:

→ Truncate: if we only want to delete the data inside the table, and not the table itself

Transaction Control Language (TCL):

→ Commit,

→ Rollback,

→ Savepoint

Data Control Language(DCL) :


→ Grant,

→ Revoke

NULL: the data value for the column is nothing

Constraints: used to enforce the integrity of the data in the columns

→ NOT NULL: If it is imposed on a column that column will not allow null values into it.
It will not occupy any space in the memory. It will not restrict duplicate values.

→ UNIQUE: If it is imposed on a column they will not allow duplicate values. Even it
restricts duplicate values it will allow single null value into the column

→ Primary Key: if we want to restrict null values as well as duplicate values we need o
use primary key. in a table only one PK is allowed

→ Foreign Key: establish a relationship between the data in two tables. FK is created
on the child table. To create FK, the master table should have PK defined on the
common column of the master table. We can have more than 1 FK in a given table

→ Check: if we want to check the values present in a column to be according to a


specified value we use this constraint.

→ Default: the default value for a column if a not null constraint is not presenting on it
is “NULL”, which can be changed by using the ‘default’.
INDEX

DATABASE

1. To create DATABASE

2. To display ALL DATABASES present in a server

3. To display ALL DATABASES with details present in a server

4. To select specific DATABASE

5. To Alter(Renaming) DATABASE NAME

6. To Delete DATABASE

DATABASE:

To create DATABASE

create database DB_Name

To display ALL DATABASES present in a server

SELECT name
FROM sys.databases

To display ALL DATABASES with details present in a server

sp_helpdb

To select specific DATABASE

USE DB_Name

To Alter(Renaming) DATABAS NAME

ALTER DATABASE DB_Name1 MODIFY NAME = DB_Name2

To Delete DATABASE
drop database DB_Name

TABLES: a table is a set of data elements (values) that is organized using rows and
columns.

To create TABLE

Create table Bank(Custid int ,CName varchar (50),Bal decimal (7,0))

To display TABLES present in a DATABASE

SELECT name
FROM sys.tables

To display ALL TABLES with Details present in a DATABASE

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

SELECT * FROM MYDB.INFORMATION_SCHEMA.COLUMNS

USE mydb
GO
EXEC sp_help
GO

SELECT * FROM INFORMATION_SCHEMA.TABLES

select * from sys.tables

To display Specific TABLE with details present in a DATABASE

sp_help emp

USE mydb
GO
EXEC sp_help 'emp'
GO

To copy Specific TABLE from one DB to another DB


To display 1st HIGHEST SAL FROM EMP Table

SELECT TOP 1 sal


FROM emp ORDER BY sal desc

select max(sal) from emp

To display Lowest SAL FROM EMP Table

SELECT TOP 1 sal


FROM emp ORDER BY sal asc

select min(sal) from emp

To display 3rd HIGHEST SAL FROM EMP Table

SELECT TOP 1 sal


FROM (SELECT DISTINCT TOP 3 sal FROM emp ORDER BY sal desc)a
ORDER BY sal

SELECT min(sal) FROM emp WHERE


sal IN (SELECT distinct TOP 3 sal FROM emp ORDER BY sal DESC)

To display TOP 3 HIGHEST SALS FROM EMP Table

SELECT distinct TOP 3 sal FROM emp ORDER BY sal desc

To display TOP 3 LOWEST SALS FROM EMP Table

SELECT distinct TOP 3 sal FROM emp ORDER BY sal asc

You might also like