views:

868

answers:

3

I have already created a table to the database.

Table is something like following (I am using sqlite on mac for iPhone dev)

create table
(
attendanceDate   varchar,
studentNo        integer,
absentDetail     varchar
);

Now I have already committed this statements. What I need is the following:

  • Add a composite primary key (attandanceDate,studentNo)
  • add a foreign key studentNo

I don't know the alter table syntax in sqlite3.

So, I need help.

Thanks in advance.

+1  A: 

SQLITE3 does not have syntax to do what you want.

From the SQLITE documentation at http://www.sqlite.org/lang_altertable.html

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a colum, remove a column, or add or remove constraints from a table.

You will need to recreate the table with the composite primary key and foreign key defined.

David
sugar
+1  A: 

When you create the table you need to create the primary key then. SQLLite3 doesn't support foreign key constraints as far as I know. Check out this link for more information. I hope this helps.

Jon
A: 

I got the solution, By R&D.

I think, I should drop my table & recreate the table by following syntax,


create table stuAttendance ( attenDate varchar,
stuNo integer,
absentDesc varchar,
primary key(attenDate,stuNo)
foreign key(stuNo) references stuClass(stuNo)
)

That will work as what I needed.

sugar