Ej: CREATE TABLE Enrolled (sID Char (20), cID Char (20), Grade Char (2) NOT

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

SQL Language

Creation:
CREATE <relation name>(<attributes>);
Ej: CREATE TABLE Enrolled (sID char(20), cID char(20), grade char(2) NOT
NULL);

Modification:
Adding tuples
INSERT INTO <relation name>(<attribute names>)
VALUES (<attribute values>);
Ej: INSERT INTO Students (sID, name, user, age,gpa)
VALUES (5400, Pedro Gomez, pgomez, 35, 5.1),(55235, Javier
Rios,jrios,28,5.8);
Deleting tuples
DELETE FROM <relation name>
WHERE <condition>
Ej: DELETE FROM Students
WHERE user = fpinto

Querying:
SELECT <attribute names>
FROM <relation names>
WHERE <condition>

Update:
UPDATE <relation name>
SET <new value assignments>
WHERE <condition>;
Ej: UPDATE Students
SET= gpa*1,1,age=age+1
WHERE sID <53670
Destroying:
DROP TABLE <relation name>;
Ej: DROP TABLE Students;

Altering Relations (Adding attributes):


ALTER TABLE <relation name>
ADD <Attribute>;
Ej: ALTER TABLE Students
ADD firstYear INTEGER;
Primary keys and Candidate keys
CREATE TABLE Enrolled
(sID CHAR(20),
cID CHAR(20),
grade CHAR(2),
PRIMARY KEY (sID,cID),
UNIQUE (cid, grade) );

Foreign Keys in SQL


CREATE Enrolled
(sID CHAR(20),
cID CHAR(20),
grade CHAR(2),
PRIMARY KEY (sID,cID)
FOREIGN KEY (sID) REFERENCES Students,
FOREIGN KEY (cID) REFERENCES Courses);

You might also like