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

SQL Table Management and Queries Guide

The document provides SQL commands for creating, altering, and managing database tables, including examples for creating tables, adding and dropping columns, and defining primary and foreign keys. It also includes commands for truncating tables and applying check constraints. Additionally, it outlines a programmatic approach to create and manipulate a student table with various operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

SQL Table Management and Queries Guide

The document provides SQL commands for creating, altering, and managing database tables, including examples for creating tables, adding and dropping columns, and defining primary and foreign keys. It also includes commands for truncating tables and applying check constraints. Additionally, it outlines a programmatic approach to create and manipulate a student table with various operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Queries:

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Creating table using another table:

CREATE TABLE TestTable AS


SELECT customername, contactname
FROM customers;

Drop Table:

DROP TABLE persons;


SQL TRUNCATE TABLE:

TRUNCATE TABLE table_name;

Alter table:
ALTER TABLE Customers
ADD Email varchar(255);

ALTER TABLE Customers


DROP COLUMN Email;

ALTER TABLE - ALTER/MODIFY DATATYPE

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Example:
ALTER TABLE Persons
ADD DateOfBirth date;

ALTER TABLE Persons


ALTER COLUMN DateOfBirth year;

ALTER TABLE Persons


DROP COLUMN DateOfBirth;

Select

SELECT *

FROM Customers

WHERE last_name = 'Doe';

SELECT *

FROM Customers

WHERE age > 25;

SELECT *

FROM Customers

WHERE last_name = 'Doe' AND country = 'USA';

Program1:

Create student table with columns [Link], rollno, name, address,


mark1,mark2,mark3;. Insert values in the table.

1. Display the table values

2. Display the description of the table.

3. alter table add total column;

4. insert the values for total column.

5. Display the record using select and where clause


PRIMARY kEY

CREATE TABLE Persons (


ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

Adding primary key for already created table:

ALTER TABLE Persons


ADD PRIMARY KEY (ID);

Drop Primary key

ALTER TABLE Persons


DROP CONSTRAINT PK_Person;

Foreign Key

CREATE TABLE Orders (


OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);

ALTER TABLE Orders


ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

Drop key

ALTER TABLE Orders


DROP CONSTRAINT FK_PersonOrder;

Check Constraint:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);

Multiple Check

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);

ALTER TABLE Persons


ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');

ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;

You might also like