Wednesday, 5 November 2014

How to use/call Query String value in Javascript

In this article I am explain, How to use/call Query String value in Javascript


function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


Use above function to get query string value


var prodId = getParameterByName('querystringid');


Fullsource Code:



<script type="text/javascript">
       function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var prodId = getParameterByName('querystringid');

    </script>

If you enjoyed this post, please like (or) share this article.

Delete record from parent table when there is Foreign Key relation between parent and child tables

In this article i will explain how to delete record from parent table when there is Foreign Key relation between parent and child tables in SQL Server.


Before reading this article, read article on "FOREIGN KEY Constraint"

delete from Department where DeptNo = 101


After executing the above statement, we got the following error:
The DELETE statement conflicted with the REFERENCE constraint "FK_DeptNo". The conflict occurred in database "TestDB", table "dbo.Employee", column 'DeptNo'.
To avoid this, before creating FOREIGN KEY on child table we will put some additional conditions like:
  • on delete cascade
  • on update cascade
  • on delete set null
  • on update set null

on delete cascade:

By this the child table records which are related to the parent table are deleted when the particular record is deleted from parent table.
Now i'm creating Department, and Employee tables.
Department:
-- Create 'Department' table
Create table Department
(
    DeptNo int primary key identity(101,1),
    DeptName varchar(50)
)

-- Insert data into 'Department' table.
insert into Department values('Finance')
insert into Department values('Production')
insert into Department values('Marketing')

-- Select data from 'Department' table.
select * from Department
Output:
Employee:
-- Create 'Employee' table
Create table Employee
(
    EmpNo int primary key identity(1,1),
    EmpName varchar(50),
    EmpSalary decimal,
    DeptNo int constraint FK_DeptNo references Department(DeptNo)
    on delete cascade
)

-- Insert data into 'Employee' table.
insert into Employee values('AAA', 1000, 101)
insert into Employee values('BBB', 2000, 102)

-- Select data from 'Employee' table.
select * from Employee
Output:
Delete from Department where DeptNo = 101. Here all the 101 records will be deleted from both tables(parent and child).
delete from Department where DeptNo = 101
select * from Department
select * from Employee
Output:

on delete set null:

By this the child table records which are related to the parent table are updated with NULL when the particular record is deleted from parent table.
Now i'm re creating Department, and Employee tables.
Department:
-- Create 'Department' table
Create table Department
(
    DeptNo int primary key identity(101,1),
    DeptName varchar(50)
)

-- Insert data into 'Department' table.
insert into Department values('Finance')
insert into Department values('Production')
insert into Department values('Marketing')

-- Select data from 'Department' table.
select * from Department
Output:
Employee:
-- Create 'Employee' table
Create table Employee
(
    EmpNo int primary key identity(1,1),
    EmpName varchar(50),
    EmpSalary decimal,
    DeptNo int constraint FK_DeptNo references Department(DeptNo)
    on delete set null
)

-- Insert data into 'Employee' table.
insert into Employee values('AAA', 1000, 101)
insert into Employee values('BBB', 2000, 102)

-- Select data from 'Employee' table.
select * from Employee
Output:
Delete from Department where DeptNo = 101. Here all the 101 records will be deleted from parent table but not from child table. DeptNo will be updated with NULL.
delete from Department where DeptNo = 101
select * from Department
select * from Employee
Output:


If you enjoyed this post, please like (or) share this article.

FOREIGN KEY Constraint in Sql Server



In this article i will explain what is FOREIGN KEY Constraint in SQL Server.
FOREIGN KEY Constraint:
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables. You can create a foreign key by defining a FOREIGN KEY constraint when you create or modify a table.
In a foreign key reference, a link is created between two tables when the column or columns that hold the primary key value for one table are referenced by the column or columns in another table. This column becomes a foreign key in the second table.
Example:
Department Table:
This is our parent table. In this DeptNo is Primary Key. There are three rows present in that table.
Employee Table:
This is our child table. In this EmpNo is Primary Key, and DeptNo is Foreign Key. Here we can insert data for EmpNo 1 and 2. When we try to insert data for EmpNo 3 we get error. DeptNo = 105 can not be inserted into Employee(child) table becauseDeptNo = 105 not present in the Department(parent) table. These operations are done by Foreign Key.
Syntax:
Department:
-- Create 'Department' table
Create table Department
(
    DeptNo int primary key identity(101,1),
    DeptName varchar(50)
)

-- Insert data into 'Department' table.
insert into Department values('Finance')
insert into Department values('Production')
insert into Department values('Marketing')

-- Select data from 'Department' table.
select * from Department
Output:
Employee:
-- Create 'Employee' table
Create table Employee
(
    EmpNo int primary key identity(1,1),
    EmpName varchar(50),
    EmpSalary decimal,
    DeptNo int constraint FK_DeptNo references Department(DeptNo)
)

-- Insert data into 'Employee' table.
insert into Employee values('AAA', 1000, 101)
insert into Employee values('BBB', 2000, 102)

-- Select data from 'Employee' table.
select * from Employee
Output:
insert into Employee values('CCC', 3000, 105)
DeptNo = 105 can not be inserted into Employee(child) table because DeptNo = 105 not present in the Department(parent) table. We will get following error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_DeptNo". The conflict occurred in database "Test", table "dbo.Department", column 'DeptNo'.
Note:
  1. We can enter the values in to the child table which are present in parent table only.
  2. The data type of both the columns is same.
  3. The column of parent table should be a PRIMARY KEY or UNIQUE KEY.
  4. We can't delete/update a record from parent table which has reference in the child table.
In the next article we will see how to delete record  from parent table.


If you enjoyed this post, please like (or) share this article.