Skip to main content

Posts

SQL Problem - Find 3rd highest employee salary

Recent posts

Singleton Design Pattern

  Singleton Design Pattern is one of the simplest creational design patterns. This design pattern ensures that class has only single instance initialized and by providing means to access the class instance. In the many scenarios we need to restrict clients to allow creation of multiple instances of the class. This design pattern can be used when, Creation of the object for each request is costlier.  Class has the shared state of the global variables used across the application. You don't want to have code that solve one problem that scatters across multiple classes. Few common scenarios where we can see this design pattern are followed: Create single connection to the database across application. Maintain single truth of source for user state. Global configuration of the application How to implement Singleton Desing Pattern? Restrict the creation of class instance outside the class by making constructor private. Add static private global variable in the class of the same ...

SQL Constraints In DBMS

  Do we need constraints? SQL Constraints are set of rules and restrictions which are applied to the columns or table of the database. This constraint ensures below points. Data consistency No accidental data loss by deleting reference data. Reduce data redundancy. Table Level Constraints PRIMARY KEY FORIEGN KEY PRIMARY KEY Primary Key constraint ensure that column doesn't contain NOT NULL value and unique. It will be used to identify particular row from the table. Table is by default index on the primary key column. Below sample code creates EMP_ID as primary key for EMPLOYEE table which is auto increment by one every time a new record inserted in the table. CREATE TABLE EMPLOYEE (     EMP_ID INT PRIMARY KEY AUTO INCREAMENT NOT NULL ,     NAME   VARCHAR ( 200 ),      EMAIL VARCHAR ( 200 ), AGE INT,     PHONE_NUMBER VARCHAR ( 15 )     ISACTIVE CHAR ( 1 ) ) FOREIGN KEY Foreign Key is used to relate two tables. Th...

Different Keys In DBMS

Do we need key? DBMS (Database Management System)   stores large amount of data in such a way so that it will be easy to read, write and secure in efficient manner. It uses table format to organize data and store data in column and row format. However, data is stored in unordered format and difficult to identify unique record from the table. Let us understand this concept with two simple tables which represent data about the employees and their respective department. Employee Table Code Name Email Address Dept. Code Phone Number Address 01 John Smith j.smith@example.com 3 123-456-7890 123 Main St, City, Country 02 Jane Doe j.doe@example.com 1 987-654-3210 456 Elm St, City, Country 03 David Johnson d.johnson@example.com 2 555-123-4567 789 Oak St, City, Country ...