anuragrawat7 / java-practice

This repository is having practice code and notes of java and DSA
0 stars 0 forks source link

SQL Practice - I #1

Open anuragrawat7 opened 7 months ago

anuragrawat7 commented 7 months ago

Relational Database

image

RDBMS benefits and limitations

Here are some of the limitations of using an RDBMS:

anuragrawat7 commented 6 months ago

JOINs

  1. INNER JOIN: This type of join returns records with matching values in both tables. SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column;

  2. LEFT (OUTER) JOIN: Returns all records from the left table, and matched records from the right table. SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.matching_column = table2.matching_column;

  3. RIGHT (OUTER) JOIN: Returns all records from the right table, and matched records from the left table. SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.matching_column = table2.matching_column;

  4. FULL (OUTER) JOIN: Returns all records when either a match is found in either left (table1) or right (table2) table records. SELECT table1.column1, table2.column2... FROM table1 FULL JOIN table2 ON table1.matching_column = table2.matching_column;

  5. SELF JOIN: A self join is a join in which a table is joined with itself. SELECT a.column_name, b.column_name... FROM table_name AS a, table_name AS b WHERE condition;

  6. CARTESIAN JOIN: If WHERE clause is omitted, the join operation produces a Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. SELECT table1.column1, table2.column2... FROM table1, table2;

anuragrawat7 commented 6 months ago

Data Constraints

Data constraints in SQL are used to specify rules for the data in a table. Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table.

Types of SQL Data Constraints

  1. NOT NULL Constraint: Ensures that a column cannot have a NULL value. CREATE TABLE Students ( ID int NOT NULL, Name varchar(255) NOT NULL, Age int );

  2. UNIQUE Constraint: Ensures that all values in a column are different. CREATE TABLE Students ( ID int NOT NULL UNIQUE, Name varchar(255) NOT NULL, Age int );

  3. PRIMARY KEY Constraint: Uniquely identifies each record in a database table. Primary keys must contain UNIQUE values. Exactly the same as the UNIQUE constraint but there can be many unique constraints in a table, but only one PRIMARY KEY constraint per table. CREATE TABLE Students ( ID int NOT NULL, Name varchar(255) NOT NULL, Age int, PRIMARY KEY (ID) );

  4. FOREIGN KEY Constraint: Prevents actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, ID int, PRIMARY KEY (OrderID), FOREIGN KEY (ID) REFERENCES Students(ID) );

  5. CHECK Constraint: The CHECK constraint ensures that all values in a column satisfies certain conditions. CREATE TABLE Students ( ID int NOT NULL, Name varchar(255) NOT NULL, Age int, CHECK (Age>=18) );

  6. DEFAULT Constraint: Provides a default value for a column when none is specified. CREATE TABLE Students ( ID int NOT NULL, Name varchar(255) NOT NULL, Age int, City varchar(255) DEFAULT 'Unknown' );

  7. INDEX Constraint: Used to create and retrieve data from the database very quickly. CREATE INDEX idx_name ON Students (Name);