Creating Tables A basic create statement for a simple student table would look like the following. create table Student (SID varchar(10), FirstName varchar(30), LastName varchar(30), EmailAddress varchar(30)); The above example creates a table with all text fields. For other data types refer to the Oracle Data Types handout. Now we want to make sure that null values are not inserted into this table. create table Student (SID varchar(10) not null, FirstName varchar(30) not null, LastName varchar(30) not null, EmailAddress varchar(30)); Now if you want to make SID as a primary key you could do it in two ways either Add the constraint at the time of creation Alter the existing table and add the primary key constraint At the time of creation The following SQL statement will add the constraint at the time of the creation create table Student (SID varchar(10) not null primary key, FirstName varchar(30) not null, LastName varchar(30) not null, EmailAddress varchar(30)); If you want to name your constraint the syntax will look like the following create table Student (SID varchar(10) not null constraint pkstudent primary key, FirstName varchar(30) not null, LastName varchar(30) not null, EmailAddress varchar(30)); For an existing Table For an existing table, you could add constraints by using the alter table syntax Alter table student add constraint pkstudent primary key(sid) Page 1
Now if you want to remove constraints you would use some thing like this as shown below alter table student drop constraint pkstudent Lets say we create an another table called StudentScores that references the SID in Student as a foreign key. You could create the table with the following syntax Create table StudentScores(SID varchar(10) not null references Student(SID), Score number(10)) You can also name the constraint so that you could drop it in the future for any reason. Create table StudentScores(SID varchar(10) not null constraint fkstudent references Student(SID), Score number(10)) Generally the syntax for each of the columns in a database is like this. [constraint <constraint_name>] [not] null check (< condition >) unique primary key refereneces <table_name>[(column_name>)] [on delete cascade] [on delete restrict] [on delete set default] [on update cascade] [on update restrict] [on update set default] CASCADE Policy - If a record that is being referenced is DELETED then delete the referencing record as well. If a Primary Key is UPDATED (changed) then update the corresponding Foreign Key as well. RESTRICT Policy - A record can not be DELETED if there is a Foreign Key referencing it. A Primary Key can not be UPDATED if there is a Foreign Key referencing it. A record with a Foreign Key can not be INSERTED (restrict insert) if it has a Foreign Key not referencing an exiting Primary Key. Page 2
DEFAULT Policy - If a record that is being referenced is DELETED then set the corresponding Foreign Key to some default value, usually NULL. If a Primary Key is UPDATED then set the corresponding Foreign Key to a default value, usually to NULL. Adding columns to existing tables Some times it is necessary to add new columns to the database, For this you could use the Alter table syntax as follows alter table StudentScores add ExamName varchar(10) Delete Tables If you want to remove all the data in the Table, you could use drop table syntax drop table StudentScores This is useful if you want to recreate the table. Be careful with the drop statements as you will not be able to retrieve the data. Delete Rows Sometimes you might need to delete some specific Rows in a table. For this purpose you could use the delete command in SQL. Lets say we just want to delete all the rows in the Students table, your SQL would look something like this. delete from students; After you execute this statement all the data in the students is gone, also all the data in the child tables might be also deleted if we have used the cascade policy when creating the tables. Lets say we just want to delete the student with 1111111111 as his ID, the SQL would look something like this delete from students where SID= 1111111111 ; Page 3
So basically you could use the similar where clause as you use in select statements to perform even delete queries. Truncate Tables If you want to delete all the data in a table, but keep the structure intact, you could use truncate instead of delete. Truncate will eliminate fragmentation of the tablespace. Example, truncate table students; Creating tables from Other Tables You could create tables from other tables by using select statements. So it means you could store the output of any of your select statements as another table. For example Create table Scores as (Select student.fname,student.lname,studentscores.score from student,studentscores where student.sid=studentscores.sid) In the above example, the results of the join between Student and StudentScores is stored in an another table called Scores. The problem with this approach is if data is changed or added to the student or the StduentScores tables, the Scores table would not to updated. But if you want the Scores table to be updated automatically, you could use Views which you will see in the following sections. In most cases this syntax is used for a quick backup for example, before running a stored procedure, you want to backup the student table into an another table called student_bak, the SQL would be Create table student_bak as(select * from student) Views In the previous section, we have seen how we could use select statements to create other tables, but these tables are not dynamically updated. So an alternative to this is to create a view. A view acts like a table but it does not contain any data. When you query a view, it dynamically fetches the data for itself and then runs your query against that data. For example lets say we have a view called Scores. You could create the view using the following syntax Page 4
Create View Scores as (Select student.fname,student.lname,studentscores.score from student,studentscores where student.sid=studentscores.sid) Once this is created, we can query this view like the following Select * from Scores where Score >100 One advantage of using a view is that if there are any changes to either the Student table or the Scores table, the data is up to date when you query the Scores View. Also views are used to protect some columns from certain users. For example a DBA might want a certain user to see only certain attributes in a table. He could create a view with those attributes and then give permission to the user to that view. This will prevent the user to see all the columns in the parent table. Inserting New Data Usually data in tables is inserted by external applications, that connect to the database. But sometimes you might want to pre populate a table with data. The easiest way to do this in SQL is by using INSERT statements. A simple insert statement would look something like this Insert into Student values( 1111111, John, Smith, jsmith@hotmail.com ); Notice I am enclosing all the fields with single quotes, that is because all the fields in the student table are of type character. But if there were a field of type number you should not enclose that in quotes. In the above statement I am assuming the order in which the columns are present in the database(meaning SID,FIRSTNAME,LASTNAME,EMAILADDRESS), but this might not be the case, so a better way to do the above statement would be Insert into Student (SID,firstname,lastname,emailaddress)values( 1111111, John, Smith, j smith@hotmail.com ); This will make sure, proper data is being inserted into the proper columns in the database.also by using this technique you need not enter all the columns in the database. Insert into Student (SID,firstname,lastname)values( 1111111, John, Smith ); Page 5
By executing the above statement you will create a record but the email field would be null. Also many people forget to put the keyword values when writing insert statements. So if SQL Query generates an error when performing an INSERT see if you have the values keyword first. Updating existing data You might need to update some data in the database. You could accomplish this task by using UPDATE statements. Let say we want to make all the email addresses in our database null, then the corresponding SQL statement would look like this. Update Student set EmailAddress=null Notice the Set keyword in the above statement. Now lets say we want to change the last name and also email address for the student with SID= 1111111111 Update Student set LastName= Doe, EmaiAddress= jdoe@liu.edu where SID= 111111111 So basically you could use the same where clause syntax as in SELECT statements to do updates also. Page 6