Monday, 20 February 2017

sql select statement

Query all columns for a city in CITY with the ID 1661.
Input Format
The CITY table is described as follows:
query:
select *
from CITY
where ID=1661;

Query for select statement

Query all columns for all American cities in CITY with populations larger than 100000. The CountryCode for America is USA.
Input Format
The CITY table is described as follows:CITY.jpg
query:
select*
from CITY
where countrycode='USA' and population>100000;

Different Keys in SQL

Types of Keys
Primary Key:

The values in the column should Uniquely define the Record/Tuple in that Table.

ID
STUDENT_NAME
CLASS
MARKS
PHONE


Consider the above Table.Here the column ID can Uniquely Define each student in the system.Hence ID is the Primary Key.

Foreign Keys:

When the values in the column of the Referencing table contains a value of the Primary Key of the Reference Table, that column is known as Foreign Key of the table.

eg.
Consider that the Reference Table is:
Supplier_ID
Name
Phone
        1
      ABC
7846
        2
      DEF
7585
        3
      GHI
7886

And Referencing Table is:
Item_id
Item_name
Supplier
      45
  pencil
     1
       76
  eraser
      1
       89
  scale
      2

Then the Supplier in the Referencing Table is the Foreign Key as it refers to the Supplier_ID in the Reference Table.

Note: Foreign Key can have repeated values as it refers to Primary Key of  reference table.


Candidate Key: It is defined as the minimal number of attributes required to identify a Record/Tuple.

eg.Consider the following table where the Primary key is Reg_no. and exam_seat_no.

Reg_no
Student_name
 class
Exam_seat_no
DOB

Now, the Reg_no , Student_name and Eaxm_seat_no can uniquely define a student.
Similarly,  Reg_no and Exam_seat_no can uniquely define a student but it's not minimal .
Only  Reg_no or Exam_seat_No can Uniquely define a student hence Reg_no  and Exam_seat_no separately are Candidate keys.

Super Keys:
In above example, Reg_no and Student_name together or Reg_no and Exam_seat_no together or all the three together are called Super key.
So, Super Key  is basically the  group of columns  which can Uniquely define a Record/Tuple ,and not necessarily be minimal.


Note:
           EVERY CANDIDATE KEY IS A SUPER KEY  BUT A SUPER KEY MAY BE / MAY NOT             BE   A  CANDIDATE KEY.






Saturday, 18 February 2017

Tutorials for mysql

I have created some videos for beginners trying to learn MySQL database.
Here are some videos which would help you to learn how to create tables, adding columns, modify columns, inserting data in  MySQL database.

Tutorial 1: Creating Tables In MySQL



Tutorial 2: Inserting Data in MySQL



Tutorial 3: Altering Table in MySQL



I'll keep uploading more videos so keep checking this post.



Monday, 30 January 2017

Removing middle element from stack


import java.io.*;
import java.util.*;
class Stack
{
Object stk[];

int tos;
Stack(int capacity)
   {
stk=new Object[capacity];

tos=-1;

}
void push(Object ob)
{
stk[++tos]=ob;
if(tos==stk.length)
{
System.out.println("stack overflow");
System.exit(0);
}
}

Object pop()
{
return(stk[tos--]);
}
void display()
{
for(int i=tos;i>=0;i--)
{System.out.print(stk[i]+" ");}
}
 
}
class Demo
{
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("enter number of elements to be pushed");
int c=sc.nextInt();
Stack s=new Stack(c);
Stack s1=new Stack(c);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

for(int j=0;j<c;j++)
{
System.out.println("enter element");
Object ob=br.readLine();
s.push(ob);
}
int mid=c/2;

System.out.println("displaying stack");
s.display();
System.out.println("");
System.out.println("poping middle  elements from stack");
if(c%2==0)
{
while(s.tos!=mid)
{
Object t=s.pop();
s1.push(t);
}
Object t1=s.pop();
System.out.println(t1);
Object t2=s.pop();
System.out.println(t2);
}
else
{
while(s.tos!=mid)
{
Object t=s.pop();
s1.push(t);
}
Object t1=s.pop();
System.out.println(t1);



}
while(s1.tos!=-1)
s.push(s1.pop());
System.out.println("displaying stack");
s.display();

}
}




OUTPUT:(for odd number of elements)
enter number of elements to be pushed
9
enter element
1
enter element
2
enter element
3
enter element
4
enter element
5
enter element
6
enter element
7
enter element
8
enter element
9
displaying stack
9               8               7               6               5               4               3               2             1
        
poping middle  elements from stack
5
displaying stack
9               8               7               6               4               3               2               1


OUTPUT:(for even number of elements)note:in even number middle two elements are poped
enter number of elements to be pushed
4
enter element
1
enter element
2
enter element
3
enter element
4
displaying stack
4               3               2               1
poping middle  elements from stack
3
2
displaying stack
4               1

Saturday, 28 January 2017

Hello World program in Java

Watch the below video for running your first Hello World program in Java.



Wednesday, 25 January 2017

To Find The Absolute Difference of the sums of two diagonals of the square matrix

Question:

To Find The Absolute Difference of the sums of two diagonals of the
square matrix

source code:


import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(); long sum1=0,sum2=0;
        int arr[][]=new int[n][n];
        for(int i=0;i<n;i++)
            {
            for(int j=0;j<n;j++)
                {
                arr[i][j]=sc.nextInt();
                if(i==j)
                {sum1+=arr[i][j];}
                if(j==n-1-i)
                { sum2+=arr[i][j];}
         
            }
        }
     
     
        System.out.println(Math.abs(sum1-sum2));
     
    }
}


In example given below the sum of first diagonal is 17 and second diagonal is 63.
hence printed result would be |sum1-sum2|.