Skip to main content

Posts

Showing posts with the label RDBMS

SQL SERVER – Find Missing Identity Values

To find missing identity values in a table in SQL Server, you can use a query like the following: WITH cte AS ( SELECT MIN(id) AS min_id, MAX(id) AS max_id FROM your_table ) SELECT a.id FROM cte CROSS JOIN (SELECT a.id + 1 AS id FROM (SELECT MIN(id) AS id FROM your_table) a UNION ALL SELECT b.id - 1 FROM (SELECT MAX(id) AS id FROM your_table) b ) a LEFT JOIN your_table t ON a.id = t.id WHERE t.id IS NULL This query uses a common table expression (CTE) to determine the minimum and maximum identity values in the table, and then generates a list of all the values between those two values using a cross join and a union all. Finally, it uses a left join to find any values that are not present in the table. Note that this query will only work if the identity column is contiguous, meaning there are no gaps between the values. If there are gaps, this query will not be able to find them. You can also use this query to find missin...

SQL SERVER – Sample Script for Compressed and Uncompressed Backup

Here is a sample script that demonstrates how to create a compressed and uncompressed backup of a database in SQL Server: -- Compressed backup BACKUP DATABASE YourDatabaseName TO DISK = 'C:\Backups\YourDatabaseName.bak' WITH COMPRESSION; -- Uncompressed backup BACKUP DATABASE YourDatabaseName TO DISK = 'C:\Backups\YourDatabaseName.bak' WITH NO_COMPRESSION; In the first statement, the WITH COMPRESSION option is used to create a compressed backup of the YourDatabaseName database. In the second statement, the WITH NO_COMPRESSION option is used to create an uncompressed backup. Replace YourDatabaseName with the actual name of your database and modify the path and file name of the backup file as needed. It's also a good idea to specify the CHECKSUM option in the BACKUP statement to verify the integrity of the backup. For example: BACKUP DATABASE YourDatabaseName TO DISK = 'C:\Backups\YourDatabaseName.bak' WITH COMPRESSION, CHECKSUM; This will ensure that the ba...

Introduction to SQL - Part 4 - Data Manipulation Language

 Data Manipulation Language DML commands are used to modify the database. It is responsible for all form of CHANGES in the database. The command of DML is not auto-committed that means it can't permanently save all the changes in the database. They can be rollback. Here are some commands that come under DML INSERT UPDATE DELETE Data Manipulation Language - INSERT      INSERT:            The INSERT statement is a SQL query. It is used to insert data into the row of a table.      Syntax:                INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)           VALUES (value1, value2, value3, .... valueN);           OR           INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);      Example:           INSERT INTO XYZ (Aut...

Introduction to SQL - Part 3 - Data Definition Language

SQL Commands in Details  Data Definition Language (DDL) DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc. All the command of DDL are auto-committed that means it permanently save all the changes in the database. Here are some commands that come under DDL: CREATE It is used to create a new table in the database.  Syntax                                         CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]); Example CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE); ALTER It is used to alter the structure of the database. This change could be either to modify the characteristics of an existing attribute or probably to add a new attribute. Syntax: ALTER TABLE table_name ADD column_name COLUMN-definition; ALTER TABLE MODIFY(COLUMN DEFINITION....); Example: ALTER TABLE STU_...

Introduction to SQL - Part 2 - SQL commands

  SQL Commands SQL commands are instructions. It is used to  communicate with the database. It is also used to  perform specific tasks, functions, and queries of data. SQL can perform various tasks like create a table, add  data to tables, drop the table, modify the table, set  permission for users. Types of SQL Commands There are five types of SQL commands:  DDL DML DCL TCL  DQL

Introduction to SQL

Introduction to SQL   • SQL stands for Structured Query Language. It is used for storing and managing data in Relational Database Management System (RDBMS).  • It is a standard language for Relational Database System. It enables a user to create, read, update and delete relational databases and tables.  • All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as their standard database language.  • SQL allows users to query the database in a number of ways, using English-like statements. SQL follows the following rules:  • Structure query language is not case sensitive. Generally, keywords of SQL are written in uppercase.  • Statements of SQL are dependent on text lines. We can use a single SQL statement on one or multiple text line.  • Using the SQL statements, you can perform most of the actions in a database.  • SQL depends on tuple relational calculus and relational algebra. Introduction to  SQL Process. Wh...