Skip to content

Shubham3016/Sql-Server-Bootcamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Server Bootcamp

https://informativedataanalyst.blogspot.com/2025/07/data-database.html

DATA

Imagine you have a big toy box. Inside, you keep many small boxes. Each small box has different types of toys—cars, animals, blocks, etc. Let’s use this as an example to explain “data” and “database” in SQL.

  • Data: These are like your toys! Each toy is a tiny piece of information. For example, you have a red car, a blue car, and a yellow dinosaur. Each toy has colors, shapes, and types—these details are “data.”

  • Data can be anything and everything Any information or facts can be considered as data like your name, age, Date of birth, Address, mobile no all this stuff can be considered as data.

  • Data can be in any form like it can be in image format, it can be in video format or can be files and txt it can be anything and can be stored in database

Database:

DATABASE: This is like your toy box. It’s a place where you keep all your toys (data) so you can find them easily when you want to play. The box keeps everything neat and tidy.

Database can be considered as container where data or information can be stored which is electronically stored in database.

Database are systems that allows users to store and organize the data. They are useful when dealing with large amounts of data.

A database is an organized collection of data, stored and retrieved digitally from a remote or local computer system. Databases can be vast and complex, and such databases are developed using fixed design and modelling approaches.

FOR EXAMPLE. :- College Database organizes the data about the admin, staff, students and faculty etc.

FOR EXAMPLE :- Suppose we have to design college timesheet data that can be stored is staff data, college data, student’s data like student’s details, marks, timesheet any many more.

  • SQL: This is like asking your mom or dad to help you find a toy. When you say, “Can you please give me the blue car?”—that’s like using SQL to ask the database for certain information.

So:

  • Data = The toys (information).
  • Database = The toy box (where you keep all your toys).
  • SQL = The way you ask for the toy you want (asking for info).

Purpose of storing data

Purpose of storing data is that it can be easily accessed, manged, modified, protected and can be analysed.

Any information that you entered is actually stored in database. And data is shown in application actually being retrieved from database.

Using the database, you can easily retrieve, insert, and delete the information.

Types of databases

There are various types of databases used for storing different varieties of data:

1) Centralized Database

Centralized database management system is the system in which all the data is stored and managed in a single unit. This is also known as central computer database system. This system is mostly used in an organization.

It comforts the users to access the stored data from different locations through several applications

image

2) Distributed Database

Unlike a centralized database system, in distributed systems, data is distributed among different database systems of an organization. These database systems are connected via communication links. Such links help the end-users to access the data easily. Examples of the Distributed database are Apache Cassandra, HBase, Ignite, etc.

image

3) Relational Database

This database is based on the relational data model, which stores data in the form of rows(tuple) and columns(attributes), and together forms a table(relation). A relational database uses SQL for storing, manipulating, as well as maintaining the data.

Examples of Relational databases are MySQL, Microsoft SQL Server, Oracle, etc.

4) NoSQL Database

Non-SQL/Not Only SQL is a type of database that is used for storing a wide range of data sets. It is not a relational database as it stores data not only in tabular form but in several different ways.

It came into existence when the demand for building modern applications increased. Thus, NoSQL presented a wide variety of database technologies in response to the demands. We can further divide a NoSQL database into the following four types:

5) Cloud Database

A type of database where data is stored in a virtual environment and executes over the cloud computing platform. It provides users with various cloud computing services (SaaS, PaaS, IaaS, etc.) for accessing the database.

There are numerous cloud platforms, but the best options are:

Amazon Web Services(AWS) , Microsoft Azure , Kamatera , PhonixNAP , ScienceSoft , Google Cloud SQL, etc.

📦 SCHEMAS IN SQL

https://informativedataanalyst.blogspot.com/2025/07/what-are-schemas-in-sql-organizing-data.html

A schema in SQL is like a folder inside a database that helps organize your data. It holds related things like tables, views, and other objects — kind of like how a folder holds files on your computer.

Schema = A named container inside a database that organizes related tables and other objects.

Imagine this:

You have a big toy box. Inside the toy box, you keep different kinds of toys — 🚗 cars, 🧸 dolls, 🧱 blocks, 🐶 animals.

Now, you want to organize them so it’s easy to find your favorite toy.

So you say:

  • Cars go in Box 1
  • Dolls go in Box 2
  • Blocks go in Box 3

Each box has a name and holds one kind of toy.


💡 In SQL terms:

  • The big toy box = Database
  • Each box inside = Schema
  • Inside each schema, we have Tables (the toys), which contain Rows (the actual toy data)

So it's like:

Database ➡️ Schema ➡️ Table ➡️ Rows

DBMS

DBMS is pronounced as Database Management System

Lot of people consider database as DBMS but it is not correct. Database is act as container that is use to stored data on other hand DBMS is nothing but Database management system is a software which is used to manage the database.

For example: MySQL, Oracle, SQL SERVER etc are a very popular commercial database which is used in different applications.

DBMS provides an interface to perform various operations like database creation, storing data in it, updating data, creating a table in the database and a lot more.

It provides protection and security to the database In the case of multiple users, it also maintains data consistency.

RDBMS

RDBMS stands for Relational Database Management System.

All modern database management systems like SQL, MS SQL Server, IBM DB2, ORACLE, My-SQL, and Microsoft Access are based on RDBMS.

It is called Relational Database Management System (RDBMS) because it is based on the relational model introduced by E.F. Codd.

  • Data is represented in terms of tuples (rows) in RDBMS.
  • A relational database is the most commonly used database. It contains several tables, and each table has its primary key.
  • Due to a collection of an organized set of tables, data can be accessed easily in RDBMS.

image

Table/Relation

Everything in a relational database is stored in the form of relations. The RDBMS database uses tables to store data. A table is a collection of related data entries and contains rows and columns to store data.

Each table represents some real-world objects such as person, place, or event about which information is collected. The organized collection of data into a relational table is known as the logical view of the database.

image

Row

A row of a table is also called a record or tuple. It contains the specific information of each entry in the table. It is a horizontal entity in the table. For example, The above table contains 5 records.

image

Column/attribute

A column is a vertical entity in the table which contains all information associated with a specific field in a table. For example, "name" is a column in the above table which contains all information about a student's name.

image

Difference between DBMS and RDBMS

DBMS and RDBMS both are used to store information in physical database but there are some remarkable differences between them.

image

SQL

SQL is a short-form of the Structured Query Language, and it is pronounced as S-Q-L or sometimes as See-Quell.

SQL is a programming language which is used to interact with relational database or RDBMS

It is an database language that is mainly designed for maintaining the data in relational database management systems (RDBMS). It is a special tool used by data professionals for handling structured data

Using SQL commands, you can read data from a relational database or write data into a relational database. You can also create, modify and delete database. SQL can also be used to analyze and protect data.

Almost all of the RDBMS available today such as Oracle, MySQL, Microsoft SQL Server, PostgreSQL, IBM DB2, SQLite, MariaDB, Teradata, Hive etc uses SQL to manage data.

SQL Server provides two types of database:

  • System databases
  • User Databases

System Databases:

The system databases are created automatically while installing the ANY RDBMS like my sql, sql server on our system. It plays an essential role in the server such database run perfectly.

The following are the list of system databases in SQL Server:

  • Master
  • Model
  • MSDB
  • Tempdb

User Databases:

The user databases are created by database users like DBAs and testers who can access a database also.

SQL Commands

SQL commands are instructions. It is used to communicate with the database. It is also used to perform specific tasks, functions, and queries of data. SQL can perform various tasks like create a table, add data to tables, drop the table, modify the table, set permission for users. SQL Server provides two types of database:

Types of SQL Commands

There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.

image

Data Definition Language (DDL)

DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc.

Used to define the structure of database objects such as tables, views, functions etc. Using DDL commands, we can create, modify and drop any database objects. The commands include:

All the command of DDL are auto-committed that means it permanently save all the changes in the database.

Here are some commands that come under DDL:

  • CREATE
  • ALTER
  • DROP
  • TRUNCATE

CREATE

create command is used to Create a new database object such a tables, views or functions etc.

SYNTAX

To run tests, run the following command

 Create database database_name;

In the above syntax, the database_name indicates the name of a new database.

Example

Let us understand how to create a database in SQL through an example. To do this, first, open the RDBMS Here we had taken RDBMS sql server after that connect with the database engine by giving the server address and the authentication details. Then, click a new query from the standard bar.

Once the connection becomes successful, execute the below statement to create a new database named Practice:

 Create database Practice;

Whether, you create a database graphically using the designer or, using a query, the following 2 files gets generated.

.MDF file (Master data File)-

Data File (Contains actual data)

.LDF file (Log Data File)

Transaction Log file (Used to recover the database)

https://github.com/Shubham3016/Sql-Server-Bootcamp/blob/main/1.%20Databases%20in%20Sql%20server.sql

CREATE TABLE

A table enables the user to store data and display information in the database. It arranges the data in rows and columns.

It can have several rows and columns, and each column has a data type associated with it that specifies the type of data it can store, such as numbers, strings, or temporal data.

CREATE TABLE Student(    
    Id INT IDENTITY PRIMARY KEY,    
    Name VARCHAR(65) NOT NULL,    
    Gender VARCHAR(20),    
    Age INT,    
    Marks INT   
 )  

In the above statement, we have created a 'Student' table.

This table contains five columns Id, Name, Gender, Age, and Marks.

Here, we have defined the id column as a primary key. The IDENTITY data type generates integer numbers that automatically begin with one and increase by one for each new row.

While creating tables there are 2 major components that plays the main role that is DATA TYPES and constraints

Lets start with DATA TYPES SQL Server supports the following data type’s categories:

Data Types Comparison Across RDBMS

Data Type Category MySQL PostgreSQL Oracle MSSQL SQLite
Integer TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT SMALLINT, INTEGER, BIGINT NUMBER(precision), INTEGER TINYINT, SMALLINT, INT, BIGINT INTEGER
Decimal/Floating DECIMAL, NUMERIC, FLOAT, DOUBLE NUMERIC, DECIMAL, REAL, DOUBLE PRECISION NUMBER(p,s), FLOAT, BINARY_FLOAT, BINARY_DOUBLE DECIMAL, NUMERIC, FLOAT, REAL REAL, NUMERIC
Character CHAR, VARCHAR, TEXT CHAR, VARCHAR, TEXT CHAR, VARCHAR2, CLOB CHAR, VARCHAR, TEXT TEXT
Date/Time DATE, DATETIME, TIMESTAMP, TIME DATE, TIME, TIMESTAMP, INTERVAL DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE DATE, DATETIME, DATETIME2, SMALLDATETIME DATE, DATETIME, TIMESTAMP (stored as TEXT)
Boolean BOOL, BOOLEAN (alias for TINYINT) BOOLEAN NUMBER(1) or use custom types BIT No native BOOLEAN, uses INTEGER 0/1
Binary BINARY, VARBINARY, BLOB BYTEA BLOB, RAW BINARY, VARBINARY, IMAGE BLOB
UUID/GUID CHAR(36), BINARY(16) (manual) UUID RAW(16) UNIQUEIDENTIFIER No native support (stored as TEXT)
JSON JSON, JSONB (MySQL 5.7+) JSON, JSONB CLOB with JSON validation NVARCHAR(MAX) with JSON support TEXT (no native JSON)
  • Exact numeric: bit, tinyint, smallint, int, bigint, decimal, numeric, money and smallmoney
  • Approximate numeric: Read and float
  • Date and time: date, DateTime, datetime2, datetimeoffset, smalldatetime, time
  • Character strings: char, varchar, text
  • Unicode character strings: Nchar, Nvarchar, Ntext
  • Binary strings: Binary, image and varbinary
  • Other data types: Cursor, hierarchyid, sql_variant, table, rowversion, uniqueidentifier, XML, Spatial and geography

image

https://github.com/Shubham3016/Sql-Server-Bootcamp/tree/main/Data%20Types

## ALTER 

- Alter is used to modify the structure of an existing table. 
- Alter can be used to rename a table or rename a column. 
- Alter can also be used to add new column or change the data type of an existing column. 
- Using Alter we can also add new constraints or remove a constraint from a table.

#### SYNTAX

```bash
Alter database DatabaseName Modify Name = NewDatabaseName

To run tests, run the following command

Alter database Practice Modify Name = Demo

Alternatively, you can also use system stored procedure

Execute sp_renameDB 'Practice','Demo'

DROP

Drop command is use to Delete or Drop a database.

When we remove the database, it will also Permanently delete all the tables, indexes, and constraints.

User have to be very careful while dropping the database because it will lose all the data available in the database.

DROP database Practice 

Dropping a database, deletes the LDF and MDF files.

TRUNCATE

TRUNCATE command is used to removes all rows from the table or specified partition without removing the table structure.

TRUNCATE TABLE TABLE_NAME 

RDBMS Operators —

A detailed reference of all major types of operators used in Relational Database Management Systems (RDBMS) such as MySQL, Oracle, SQL Server, PostgreSQL, and SQLite. This guide covers explanations, practical SQL examples, and theoretical relational algebra operators.

Table of Contents

1. Arithmetic Operators

Perform mathematical calculations on numeric data.

Operator Description Example
+ Addition SELECT price + tax AS total FROM sales;
- Subtraction SELECT balance - fee AS net FROM account;
* Multiplication SELECT quantity * price AS total FROM order;
/ Division SELECT salary / 12 AS monthly FROM employee;
% Modulo (remainder) SELECT 10 % 3 AS result;

2. Comparison (Relational) Operators

Compare values and filter data.

Operator Description Example
= Equal to WHERE status = 'Active'
<> or != Not equal to WHERE city <> 'Delhi'
< Less than WHERE age < 30
> Greater than WHERE salary > 50000
<= Less than or equal to WHERE age <= 18
>= Greater than or equal to WHERE fee >= 100

3. Logical Operators

Combine or invert multiple conditions.

Operator Description Example
AND All conditions true WHERE dept = 'HR' AND age > 30
OR Any condition true WHERE dept = 'Sales' OR dept = 'IT'
NOT Reverse condition WHERE NOT city = 'Mumbai'

4. Bitwise Operators

Operate at the binary level (mostly relevant in SQL Server, less so in MySQL/Postgres).

Operator Description Example
& Bitwise AND WHERE permissions & 1 = 1
| Bitwise OR WHERE flags | 2 = 2
^ Bitwise XOR SELECT 5 ^ 3 AS result;
~ Bitwise NOT SELECT ~8 AS result;
<< Left Shift SELECT 1 << 2 AS shifted_value;
>> Right Shift SELECT 8 >> 2 AS shifted_value;

5. Compound Operators (Assignment)

Perform operation and assign result (common in SQL Server and Oracle).

Operator Description Example
+= Add and assign SET @balance += 100
-= Subtract and assign SET @count -= 1
*= Multiply and assign SET @total *= 2
/= Divide and assign SET @avg /= 5

CONSTRAINTS

SQL constraints are rules that help keep your database clean, correct, and safe. They make sure your data follows certain rules and prevent mistakes like missing values or duplicates.

If you want to make sure that wrong data is not inserted into your table then these kind of sanity checks can be applied by using CONSTRAINTS.

Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

Constraint What it does
NOT NULL Makes sure a column cannot have missing (NULL) values
UNIQUE Makes sure all values in a column are different from each other
PRIMARY KEY Uniquely identifies each row—no duplicates and no NULL values
FOREIGN KEY Connects a column in one table to a column in another table
CHECK Makes sure values meet a certain rule (e.g., numbers must be positive)
DEFAULT Automatically gives a column a value if none is provided
INDEX Makes searching and getting data faster (not a data rule, for performance)

NOT NULL CONSTRAINT

By applying NOT NULL constraint on a column, you make sure that this column will never have NULL or empty values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

SQL NOT NULL on CREATE TABLE

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);

To add Age column using alter and add NOT NULL CONSTRAINTS

ALTER TABLE Persons
ALTER COLUMN Age int NOT NULL;

UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

Unique constraints are used to make sure that values inserted into a column across all the rows have unique or distinct values. It can help you to eliminate any duplicate data in a column.

NULL values are allowed in a UNIQUE constraint column. And two NULL values are not same hence multiple rows with NULL values are allowed.

CREATE TABLE Persons (
    ID int NOT NULL UNIQUE,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int
);

SQL UNIQUE Constraint on ALTER TABLE

ALTER TABLE Persons
ADD UNIQUE (ID);
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

DROP a UNIQUE Constraint

ALTER TABLE Persons
DROP CONSTRAINT UC_Person;

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published