0% found this document useful (0 votes)
19 views2 pages

SQL queries

The document outlines various SQL commands for database management, including SELECT, UPDATE, DELETE, and CREATE TABLE statements. It explains the concept of FOREIGN KEY constraints, which maintain relationships between tables by linking a child table to a parent table. Additionally, it provides examples of SQL queries to filter data based on specific conditions.

Uploaded by

r190867
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)
19 views2 pages

SQL queries

The document outlines various SQL commands for database management, including SELECT, UPDATE, DELETE, and CREATE TABLE statements. It explains the concept of FOREIGN KEY constraints, which maintain relationships between tables by linking a child table to a parent table. Additionally, it provides examples of SQL queries to filter data based on specific conditions.

Uploaded by

r190867
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
You are on page 1/ 2

2. SELECT DISTINCT column1, column2, ...

FROM table_name;
3. SELECT column1, column2, ... FROM table_name WHERE condition;
4. UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
5. DELETE FROM table_name WHERE condition;
6. DROP DATABASE databasename;
7. CREATE TABLE table_name ( column1 datatype, column2
datatype,....);
8. DROP TABLE table_name;
9. ALTER TABLE table_name ADD column_name datatype;
10. CREATE TABLE Persons ( ID int NOT NULL,
LastName varchar(255) NOT NULL, FirstName varchar(255),
Age int, PRIMARY KEY (ID) );
11. CREATE TABLE Persons ( ID int NOT NULL,
LastName varchar(255) NOT NULL, FirstName
varchar(255) NOT NULL,
Age int);
12. ALTER TABLE Persons ALTER COLUMN Age int NOT NULL;
13. CREATE TABLE Persons ( ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL, FirstName varchar(255),
Age int );

The FOREIGN KEY constraint is used to prevent 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.

The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.

Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.

The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.

The FOREIGN KEY constraint prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the
parent table.

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Customer (Customet Id, Name, Contact , address, city, code,


country)

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
SELECT * FROM Customers
WHERE NOT Country='Germany';

You might also like