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

Basic SQL Syntax Guide

The document outlines various SQL commands for managing database tables, including creating, deleting, renaming, and modifying tables. It provides examples of creating tables with primary and foreign keys, inserting data, updating records, and selecting queries. Additionally, it covers adding and dropping columns, as well as implementing constraints on tables.

Uploaded by

ansh020904
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)
4 views2 pages

Basic SQL Syntax Guide

The document outlines various SQL commands for managing database tables, including creating, deleting, renaming, and modifying tables. It provides examples of creating tables with primary and foreign keys, inserting data, updating records, and selecting queries. Additionally, it covers adding and dropping columns, as well as implementing constraints on tables.

Uploaded by

ansh020904
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

CREATE A TABLE

CREATE TABLE Employees (


EmpID INT,
Name VARCHAR(50),
DepartmentID INT
);

DELETE A TABLE
DROP TABLE Employees;

RENAME A TABLE
ALTER TABLE Employees RENAME TO Staff;

IMPORT A FOREIGN KEY


CREATE TABLE Departments (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50)
);

CREATE TABLE Employees (


EmpID INT PRIMARY KEY,
Name VARCHAR(50),
DeptID INT,
FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);

CREATE A PRIMARY KEY


CREATE TABLE Students (
RollNo INT PRIMARY KEY,
Name VARCHAR(50)
);

-- OR on existing table
ALTER TABLE Students
ADD CONSTRAINT pk_roll PRIMARY KEY (RollNo);

CREATE A CANDIDATE KEY


CREATE TABLE Users (
UserID INT,
Email VARCHAR(100),
PRIMARY KEY (UserID),
UNIQUE (Email) -- Candidate Key
);

INSERT ANY KEY


INSERT INTO Students (RollNo, Name)
VALUES (101, 'Alice');

INSERT A QUERY
INSERT INTO Backup_Students (RollNo, Name)
SELECT RollNo, Name FROM Students
WHERE Department = 'Science';

ADD COLUMN
ALTER TABLE Students ADD Age INT;

DROP COLUMN
ALTER TABLE Students DROP COLUMN Age;

UPDATE DATA
UPDATE Students SET Name = 'Bob' WHERE RollNo = 101;

DELETE DATA
DELETE FROM Students WHERE RollNo = 101;

SELECT QUERY
SELECT * FROM Students;
SELECT Name FROM Students WHERE RollNo = 101;

CREATE TABLE WITH CONSTRAINTS


CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
Quantity INT CHECK (Quantity > 0),
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

You might also like