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

SQL Table Management and Data Manipulation

The document provides an overview of SQL commands for creating, altering, and dropping tables, as well as inserting, updating, and deleting rows. It includes specific syntax for each command and practical examples using a 'Studnt' table. The document illustrates various operations such as adding and modifying columns, inserting records, and managing data constraints.

Uploaded by

Saranya
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)
15 views3 pages

SQL Table Management and Data Manipulation

The document provides an overview of SQL commands for creating, altering, and dropping tables, as well as inserting, updating, and deleting rows. It includes specific syntax for each command and practical examples using a 'Studnt' table. The document illustrates various operations such as adding and modifying columns, inserting records, and managing data constraints.

Uploaded by

Saranya
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

Creation, altering and dropping of tables and inserting rows into a table (use constraints while

creating tables) examples using SELECT command.

1. CREATE: Creates a database object such as a table, index, function, view, procedure, or trigger.

Syntax: CREATE TABLE table_name;

2. DROP: Deletes objects from the database such as tables, views, or procedures.

Syntax: DROP TABLE table_name;

3. ALTER: Modifies the structure of an existing database table.

Syntax: ALTER TABLE table_name ADD column_name datatype;

ALTER TABLE table_name MODIFY column_name datatype;

ALTER TABLE table_name DROP COLUMN column_name;

ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

ALTER TABLE table_name RENAME TO new_table_name;

4. TRUNCATE: Removes all records from a table but retains the structure for future use.

Syntax: TRUNCATE TABLE table_name;

5. INSERT: Inserts new data into a table.


Syntax: INSERT INTO table_name VALUES (value1, value2, value);

INSERT INTO table_name (column1, column2, column3) VALUES ( value1, value2, value);

2. UPDATE: Modifies existing data in a table.


Syntax: UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;

3. DELETE: Deletes specific records from a table.


Syntax: DELETE FROM table_name WHERE some_condition;

DELETE FROM table_name;

STUDENT TABLE CREATION


CREATE TABLE Studnt(SId INT PRIMARY KEY, FirstName VARCHAR(15) NOT NULL, LastName
VARCHAR(15), Email VARCHAR(20) UNIQUE, DOB DATE, Course CHAR(7), Age INT CHECK(Age>=18),
Branch VARCHAR(10) DEFAULT 'DS');

INSERT INTO Studnt(SId, FirstName, LastName, Email, DOB, Course, Age, Branch) VALUES(1,'Dennis',
'Ritchie ', 'DennisRe@[Link]', DATE '2002-03-15', 'BTECH', 21, 'AI');

INSERT INTO Studnt VALUES(2,'Ken', 'Thompson ', 'KThompson@[Link]', DATE '2003-04-16',


'BTECH', 22, 'ML');

INSERT INTO Studnt VALUES(3,'James', 'Gosling', 'Jamesg@[Link]', DATE '2004-05-17', 'BTECH',


19, 'DS');

INSERT INTO Studnt VALUES(4,'Guidovan', 'Rossum', 'GuidoV@[Link]', DATE '2005-07-19',


'BTECH', 18, 'AIML');
INSERT INTO Studnt VALUES(5,'Larry', 'Ellison', 'LarryE@[Link]', DATE '1999-11-19', 'MTECH', 24,
'IT');

INSERT INTO Studnt VALUES(6,'Wilson', 'Bentley', 'WBentley@[Link]', DATE '2001-08-20',


'MTECH', 22, 'CSE');

INSERT INTO Studnt(SId, FirstName, LastName, Email, DOB, Course, Age) VALUES(7,'Michael',
'Brown', 'MBrown@[Link]', DATE '1998-09-20', 'MTECH', 25);

SELECT *FROM Studnt;

ALTER TABLE Studnt ADD Marks INT;

SELECT *FROM Studnt;

ALTER TABLE Studnt MODIFY Course VARCHAR(10);

SELECT *FROM Studnt;

ALTER TABLE Studnt RENAME COLUMN Marks TO TotalMarks;

SELECT *FROM Studnt;

ALTER TABLE Studnt DROP COLUMN TotalMarks;

SELECT *FROM Studnt;

UPDATE Studnt SET DOB = DATE '2001-04-16' WHERE SId = 2;

SELECT *FROM Studnt;

UPDATE Studnt SET Course = 'MTECH', Age=22 WHERE FirstName = 'Dennis';

SELECT *FROM Studnt;

UPDATE Studnt SET Course = 'BTECH';

SELECT *FROM Studnt;

DELETE FROM Studnt WHERE LastName = 'Gosling';

SELECT *FROM Studnt;

DELETE FROM Studnt;

SELECT *FROM Studnt;

TRUNCATE TABLE Studnt;

SELECT *FROM Studnt;


DROP TABLE Studnt;

SELECT *FROM Studnt;

You might also like