0% found this document useful (0 votes)
18 views3 pages

SQL CRUD Operations Guide

SQL_CRUD_Reviewer

Uploaded by

Frances Cia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

SQL CRUD Operations Guide

SQL_CRUD_Reviewer

Uploaded by

Frances Cia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ SQL CRUD Operations Reviewer

■ 1) INSERT Examples (Create)


-- Add new users
INSERT INTO users (username, password) VALUES ("john", "1234");
INSERT INTO users (username, password) VALUES ("mary", "abcd");
INSERT INTO users (username, password) VALUES ("peter", "hello");

-- Add with email column


INSERT INTO users (username, password, email) VALUES ("anna", "pass1", "anna@[Link]"
INSERT INTO users (username, password, email) VALUES ("mark", "pass2", "mark@[Link]"

-- Add multiple rows at once


INSERT INTO users (username, password) VALUES ("test1", "1111"), ("test2", "2222");

-- Insert with current date


INSERT INTO users (username, password, created_at) VALUES ("sam", "qwerty", NOW());

-- Insert a student
INSERT INTO students (student_id, fullname, course) VALUES (1, "Juan Dela Cruz", "BSIT"
INSERT INTO students (student_id, fullname, course) VALUES (2, "Maria Santos", "BSA");

■ 2) SELECT Examples (Read)


-- Get all users
SELECT * FROM users;

-- Get only username column


SELECT username FROM users;

-- Get user with condition


SELECT * FROM users WHERE username="john";
SELECT * FROM users WHERE username="mary" AND password="abcd";

-- Using LIKE for partial search


SELECT * FROM users WHERE username LIKE "m%"; -- starts with m
SELECT * FROM users WHERE username LIKE "%a"; -- ends with a
SELECT * FROM users WHERE username LIKE "%an%"; -- contains "an"

-- Using ORDER BY
SELECT * FROM users ORDER BY username ASC;
SELECT * FROM users ORDER BY id DESC;

-- Using LIMIT
SELECT * FROM users LIMIT 5;
SELECT * FROM users LIMIT 3 OFFSET 5;

-- Selecting with BETWEEN


SELECT * FROM students WHERE student_id BETWEEN 1 AND 10;

-- Selecting with IN
SELECT * FROM students WHERE course IN ("BSIT", "BSA");

■ 3) UPDATE Examples (Update)


-- Change password
UPDATE users SET password="newpass" WHERE username="john";

-- Change multiple fields


UPDATE users SET password="xyz", email="new@[Link]" WHERE username="anna";

-- Update all users to same password (not safe, but example)


UPDATE users SET password="12345";

-- Increase student_id by 1
UPDATE students SET student_id = student_id + 1 WHERE course="BSIT";

-- Change course
UPDATE students SET course="BSCS" WHERE fullname="Juan Dela Cruz";

-- Update multiple conditions


UPDATE users SET password="reset123" WHERE username="peter" AND email="peter@[Link]"

-- Use LIKE in update


UPDATE users SET password="default" WHERE username LIKE "test%";

-- Set NULL value


UPDATE users SET email=NULL WHERE username="mark";

-- Update with LIMIT


UPDATE users SET password="trial" LIMIT 2;

-- Update date field


UPDATE users SET created_at=NOW() WHERE username="mary";

■ 4) DELETE Examples (Delete)


-- Delete one user
DELETE FROM users WHERE username="john";

-- Delete by id
DELETE FROM users WHERE id=5;
-- Delete all test accounts
DELETE FROM users WHERE username LIKE "test%";

-- Delete with multiple condition


DELETE FROM users WHERE username="anna" AND password="pass1";

-- Delete first 3 rows (MySQL only)


DELETE FROM users ORDER BY id ASC LIMIT 3;

-- Delete students in BSA course


DELETE FROM students WHERE course="BSA";

-- Delete rows with NULL email


DELETE FROM users WHERE email IS NULL;

-- Delete all users


DELETE FROM users;

-- Delete using IN
DELETE FROM students WHERE course IN ("BSIT", "BSCS");

-- Delete using BETWEEN


DELETE FROM students WHERE student_id BETWEEN 1 AND 5;

Common questions

Powered by AI

The SELECT statement with the BETWEEN condition enhances data filtration by allowing queries to specify a range of values efficiently, optimizing data searches and operations. For example, `SELECT * FROM students WHERE student_id BETWEEN 1 AND 10;` filters records to only include students with IDs within this specified range. This method is beneficial in scenarios where you need to obtain data segments, such as enrolling students within a particular age range or batch year, maintaining data relevance and accuracy .

Using the DELETE operation with a WHERE clause and an ORDER BY clause, such as `DELETE FROM users ORDER BY id ASC LIMIT 3;`, allows for more controlled and specific data deletion. This technique ensures that only the targeted rows are deleted based on specified conditions and orders, reducing the risk of accidental data loss. The ORDER BY clause can dictate which rows to delete first, and the LIMIT clause confines the number of deletions, enhancing data management safety .

Using an UPDATE query with a LIMIT clause, such as `UPDATE users SET password="trial" LIMIT 2;`, can lead to incomplete or unintended data updates by only affecting a limited subset of the dataset. This scenario could arise in a situation where database administrators intend to update all user records meeting certain criteria but inadvertently limit the update to only a portion of those records, missing others that also match the conditions. This leads to inconsistent data states where only some relevant data entries are updated, potentially violating data constraints or business rules .

The benefits of using the INSERT operation with the NOW() function, as shown in `INSERT INTO users (username, password, created_at) VALUES ("sam", "qwerty", NOW());`, include automatic timestamping of records, which simplifies tracking when each entry is made. However, a potential downside is that it relies on database server time, which may not always match the desired timezone or be synchronized with other systems. It's crucial to ensure that server time is accurately set to maintain the integrity of the data. Moreover, the use of NOW() assumes all records should be timestamped at insertion, which might not be necessary for all database entries .

The use of conditional statements like WHERE in conjunction with LIKE, such as in the query `SELECT * FROM users WHERE username LIKE "m%";`, allows for refined data retrieval through pattern matching, improving the efficiency of database searches. This approach is particularly beneficial for searching large datasets where exact matches are not necessary, such as autocomplete suggestions, filtering user input for input prediction, and implementing search functionality in applications where partial matches can provide more comprehensive results .

Conditional DELETE operations using the IN operator, such as `DELETE FROM students WHERE course IN ("BSIT", "BSCS");`, enhance data security and management by allowing for targeted deletions. This method prevents blanket deletions by focusing on specific datasets or groups, reducing the risk of accidental data loss. It simplifies managing large datasets by efficiently removing unnecessary or outdated information without compromising the integrity of critical data .

Using a generalized UPDATE query to change passwords for all users, such as `UPDATE users SET password="12345";`, poses significant security risks. This practice can lead to vulnerabilities, including easy unauthorized access since all users have the same weak password, which might also be well-known or easily guessable. Additionally, it compromises data integrity by not accounting for the users' specific password requirements or preferences, leading to potential compliance issues with data protection regulations that mandate the use of strong, unique passwords .

Using wildcard characters with the LIKE operator, as in `SELECT * FROM users WHERE username LIKE "%an%";`, can severely impact query performance, especially in large-scale SQL databases. These characters cause the query engine to perform more extensive scan operations over the dataset to match patterns rather than indexed lookups. This method is resource-intensive and can lead to longer execution times, escalating with database size and complexity. Optimizing queries by limiting wildcard use, indexing frequently searched columns, or employing full-text search capabilities where possible, is crucial for maintaining performance .

Executing a DELETE query without a restricting WHERE clause, such as `DELETE FROM users;`, results in the deletion of all data from a table, representing a severe security risk. This action can lead to total data loss if not caught and reversed immediately, affecting database integrity and operational continuity. It also leaves the system vulnerable to malicious actions if exploited, where unauthorized users could erase critical data using broad delete commands. Therefore, it is essential to use DELETE with specific conditions to minimize such risks .

JOIN operations increase SQL queries' complexity and efficiency by allowing the combination of data from multiple tables based on related columns. In interconnected databases, this can enhance data retrieval efficiency, providing comprehensive records without redundancy. However, JOINs can also slow down queries due to large datasets, especially if not indexed properly or over-complicated with multiple conditions. Structuring JOIN operations to prevent unnecessary data fetching and optimize execution plans is crucial for maintaining query performance, particularly in normalized databases with extensive relations [no direct source].

You might also like