In SQL, the CREATE TABLE
statement is used to define a relation, which creates a new table in the database with the specified columns and data types.
To modify a relation instance (i.e., the data in a table), SQL provides several constructs:
INSERT INTO
: used to add new rows to a tableDELETE FROM
: used to remove one or more rows from a tableUPDATE
: used to modify the values in one or more rows of a table
For example, to insert a new row into a table called students
with columns name
, age
, and major
, you could use the following SQL statement:
sqlINSERT INTO students (name, age, major) VALUES ('John Doe', 22, 'Computer Science');
To delete all rows from the students
table where the major
column is 'Biology', you could use the following SQL statement:
sqlDELETE FROM students WHERE major = 'Biology';
To update the age
column for all rows in the students
table where the major
column is 'Computer Science', you could use the following SQL statement:
sqlUPDATE students SET age = age + 1 WHERE major = 'Computer Science';
In relational algebra, insertion, deletion, and updating of a database are performed using the following operators:
INSERT
: adds a new tuple to a relationDELETE
: removes a tuple from a relationUPDATE
: modifies the value of one or more attributes in a tuple
For example, to insert a new tuple into a relation Students
with attributes Name
, Age
, and Major
, you could use the following relational algebra expression:
rStudents <- Students U {(John Doe, 22, Computer Science)}
To delete all tuples from the Students
relation where the Major
attribute is 'Biology', you could use the following relational algebra expression:
scssStudents <- Students - (σ(Major='Biology')(Students))
To update the Age
attribute for all tuples in the Students
relation where the Major
attribute is 'Computer Science', you could use the following relational algebra expression:
goStudents <- Students {Age:=Age+1 | Major='Computer Science'}
In both SQL and relational algebra, these constructs allow you to manipulate the data in a relation instance, enabling you to add, remove, and modify the data as needed.