Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Introduction to SQL Server :

1. What is relational database?


As people started using computers, there were many challenges they faced while s
toring and using data. They started using computers for storing piles of data, b
ut some of the common problem that they faced are:
-- Data Integrity
-- A scientist named Dr. E.F Codd analyzed all the problems in which the users a
re running into and came up with a set of rules which the database implementator
s need to follow. He came up with 13 rules and the database that implements thes
e rules is referred to as a relational database which totally elliminated all th
ose inconsistemcy issues.
In relational databases, tables are used to store entity which can be any real w
orld example. More presicely they are used to store information about an entity
and each row represents the entity. Columns store something about that entity an
d that something is a piece of information that we call attribute. Table stores
one kind of entity.
An entity is unique and it can appear in a table on once. The attribute or a set
of attributes makes an entity unique which is referred to as a primary key. In
a releational database a table is required to have a primary key. There are two
implications of this:
-- It helps in searching the data in a database.
-- It helps in keeping an entity unique in a table which avoids inconsistency in
data.
Earlier the data is stored in form of binaries and also retrived in the form of
binaries from a table. But Codd proposed that the data should be stored in a tex
t based lanaguage and alos be retrived liked data.
Tables store scalar values. They are very easy to sort as compared to non scalar
values.
RDBMS is relational database management system that manages the access to the fi
les in which the data is stored with the purpose to protect the data and serve t
he clients as well at the same time.
Relational databases were responses to the data integrity issues. Relational dat
abases follow Codd's rules:
-- Table filled with entities
-- Table has Primary Key
-- Text based language
-- Scalar values
-- RDBMS
2. SQL Server Data Manipulation language
This language can be written using SQL and T-SQL or Transact-SQL is the microsof
t's implementation of SQL.
SQL Data manipulation language -- select,insert,update and delete.
The entire SQL is based on set theory and is nothing but mathematics.
Select -- Specifies that you want some data from some entity.
select lastname from employee where departmentid = 3
select specifies that you want some data, lastname specifies that what data you
want, from employee specifies that from where you want the data and departmentid
specifies that what critirea you want to apply on the data.
Insert -- Adds data to the table or adds values of the attributes.
insert into people values ("Gopesh", "Vijayvergiya","gray")
so into people specify that where you want to add and the values specify that wh
at values you want to add.
At times you don't want to add value of some attributes so you provide NULL to t
hat place.
insert into people values ("Gopesh", "Vijay",NULL)
or when you want to just add the defalut value for an attribute, you provide DEF
AULT.
insert into people values ("Gopesh","Vijay",DEFAULT)
Update -- this is used to change the value of an attribute of an entity. This op
eration is applied to a set.
update people set fav color = "GRAY"
Delete -- removes an entity from the table. Like update, this is also applied to
a set.
delete people where fav color = "Gray"
This statement removes all te entities from the table where fav color is gray.
Aggregate function count(exp) -- only counts the number of records but it doesn'
t include null values. You can include any type of expression inside this count
function.
In cross join -- select * from first table cross join second table, each row in
the second table will repeat the entire first table.
Insert command is used to add the values of attributes to an entity in the order
in which they appear.
insert into people values ("--------");
but we don't use this kind of statement in production code because there can be
a possiblity that the table structure has been modified and we don't know that s
o that will create a mess. So its better to enumerator the attributes while addi
ng them.
insert into people (firstname, lastname, favorite color) values ("--","--","--")
;
There are time when we don't know some value to insert or we don't care about th
at value, in that case we add null.
insert into people (firstname, lastname, favorite color) values ("--","--",null)
;
But what if we don't want to insert null as well and we don't even care for that
aattribute, then we can add default.
insert into people (firstname, lastname, favorite color) values ("--","--",defau
lt);
while creating table we can specify what defalut value we need to put for a part
icular attribute of entity. If we don't specify that and than also trying the de
falut keyword, then we will get null for that attribute value.
If we want to insert multiple values at a time then we can use the below techniq
ue:
INSERT INTO People VALUES
('Chico','Marx','Orange'),
('Harpo','Marx','green'),
('Groucho','Marx',null),
('Gummo','Marx','blue'),
('Zeppo','Marx','LightBlue')
Then there is a into clause that creates a new table and insert the result of a
select statment from some table to the newly created one.
select * into table2 from table1;
Here table2 is the newly created table and we are copying the value of table1 to
table2.
But the new table i.e.. table2 doesn't have the primary key constraint as it was
present in table1.
This into clause will run only when the new table is not present. If the table2
already exists then it will give an error.
This into clause doesn't insert the record from one table to the other. For this
we have to use the combination of insert and select statements. For example:
INSERT INTO [marx brother] ([first name],[last name],[favorite color])
SELECT [first name],[last name],[favorite color] FROM People where [last name]='
carrey'
Then there is an update statement that is used to modify the value of attribute
for an entity in a table or for a set of entities in a table. We can use a combi
nation of update and select as shown below:
update People2 SET [favorite color]=
(
SELECT [favorite color] from People WHERE
People2.[first name] = People.[first name] AND
People2.[last name] = People.[last name]
);
Then there comes the most destructive statement, the delete that is used to remo
ve an entity or a set of entities from a table.
delete tablename;
This only deletes the content of the table.
Data Defination lanaguage :
DDL is a part of the text based lanaguage that is used to work with the database
s. The SQL sees everything as an object, table as object, your login to ssms as
object. So DDL is used to create, describe and manage these objects. Here we are
going to talk in terms of object.
We can create a table with minimal information as shown below:
CREATE table Point
(
[x coord] float,
[y coord] float
);
Now it doesn't have any primary key but according to Codd's rules every entity i
n a relational database should have a primary key.
So now we are going to create a table with the primary key which is managed by s
ql server only.
CREATE table [points table]
(
[position] int identity primary key,
[x coord] float,
[y coord] float
);
Her we have defined the primary key position as identity because in this case we
don't have to pass the value for this attribute but sql server will manage it f
or us by every time incrementing it by one.
If we mark an attribute as primary key then it will automatically become non nul
lable and this goes with identity as well.
The complete defination for creating a table is as below:
create table People
(
[first name] varchar(40) not null,
[last name] varchar(40) not null,
[favorite color] varchar(15) null default 'Willow blue',
constraint people_pk primary key ([first name],[last name])
)
Alter command is used to change the defination of a table i.e.. by adding a new
column or by removing a column or by adding constraint.
If we run the below command:
alter table people4 add [favorite food] varchar(25) null default 'hot dog', we w
ill get a new column by all the other values in the new column be null. but if w
e do like this
alter table people4 add [favorite food] varchar(25) null default 'hot dog' WITH
values;
Then the defalut value will be added to the entire table.
Drop command is used to remove an object from the database, if used with a table
it will throw away the table along with its data.
drop table tablename;

You might also like