0% found this document useful (0 votes)
23 views4 pages

SQL Queries: SELECT, INSERT, UPDATE, DELETE

The document provides an overview of basic SQL queries including SELECT, INSERT, UPDATE, DELETE, and ADD CONSTRAINT. It explains how to retrieve, add, modify, and delete data in a database, as well as the importance of constraints for data integrity. Each query type is illustrated with examples for clarity.

Uploaded by

ninhntu2001
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)
23 views4 pages

SQL Queries: SELECT, INSERT, UPDATE, DELETE

The document provides an overview of basic SQL queries including SELECT, INSERT, UPDATE, DELETE, and ADD CONSTRAINT. It explains how to retrieve, add, modify, and delete data in a database, as well as the importance of constraints for data integrity. Each query type is illustrated with examples for clarity.

Uploaded by

ninhntu2001
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

1. SELECT Query:
The SELECT statement is used to retrieve data from one or more tables in a
database. It allows you to specify which columns you want to view, and you can
filter results using WHERE clauses.
Example:

SELECT * FROM students

SELECT indicates that you want to retrieve data.

* means all columns in the table.

FROM students specifies the table students .

If you want to get specific columns:

SELECT name, age FROM students WHERE age > 18;

This query fetches the name and age of students who are older than 18.

2. INSERT Query:
The INSERT statement allows you to add new rows of data to a table.
Example:

INSERT INTO students (name, age, grade)


VALUES ('John Doe', 20, 'A');

INSERT INTO students specifies that you want to insert new data into the students

table.

SQL 1
(name, age, grade) specifies the columns into which the values will be inserted.

VALUES ('John Doe', 20, 'A') specifies the actual data being added to the table.

This will insert a new student named John Doe, aged 20, with a grade of 'A'.

3. UPDATE Query:
The UPDATE statement allows you to modify existing data in a table.
Example:

UPDATE students
SET grade = 'B'
WHERE name = 'John Doe';

UPDATE students specifies the table you are updating.

SET grade = 'B' updates the grade column to 'B'.

ensures that only the student named John Doe gets


WHERE name = 'John Doe'

updated. Without the WHERE clause, it would update all records in the table.

4. DELETE Query:
The DELETE statement allows you to remove rows from a table.

Example:

DELETE FROM students


WHERE age < 18;

DELETE FROM students specifies that you want to delete records from the students

table.

WHERE age < 18 restricts the deletion to only those students younger than 18.
Again, without the WHERE clause, it would delete all rows in the table.

SQL 2
5. ADD CONSTRAINT Query:
Constraints are rules applied to columns in a table to ensure data integrity. The
most common constraints are primary keys, foreign keys, unique constraints, and
not-null constraints.

Adding a Primary Key Constraint:


A primary key uniquely identifies each record in the table. You can add a primary
key constraint after creating the table.

Example:

ALTER TABLE students


ADD CONSTRAINT pk_student_id PRIMARY KEY (student_id);

ALTER TABLE students modifies the existing students table.

ADD CONSTRAINT pk_student_id PRIMARY KEY (student_id) adds a primary key constraint
on the student_id column.

Adding a Foreign Key Constraint:


A foreign key enforces a link between two tables, ensuring that the data in one
table corresponds to valid entries in another.

Example:

ALTER TABLE students


ADD CONSTRAINT fk_course_id FOREIGN KEY (course_id)
REFERENCES courses(course_id);

This statement ensures that the course_id in the students table must match a
valid course_id in the courses table.

SQL 3
Summary of Key Concepts:
SELECT: Retrieves data from the database. You can use WHERE to filter the
results.

INSERT : Adds new rows to the table.

UPDATE : Modifies existing data. WHERE is used to limit which rows are updated.

DELETE : Removes rows from the table. WHERE specifies which rows to delete.

ADD CONSTRAINT : Ensures data integrity by applying rules (e.g., primary keys,
foreign keys) to the data structure.

SQL 4

You might also like