Jump to content
Sign in to follow this  
SvekkeHomie

Add constraint problem

Recommended Posts

I just started with SQL and thus following the online W3Schools course.

Unfortunately now stuck with adding a constraint to an already created table...

http://www.w3schools.com/sql/sql_unique.asp

 

After creating a table with...

CREATE TABLE MyTest(MyVar1 int,MyVar2 varchar(255))

and trying to put a unique constraint on the MyVar1 column with...

ALTER TABLE MyTestADD UNIQUE (MyVar1)

I get "Error 1: could not prepare statement (1 near "UNIQUE"; syntax error)".

 

Anyone ?

Share this post


Link to post
Share on other sites

My problems with adding constraints using the Tryit editor continue ...

 

Adding a table with...

CREATE TABLE PersonTest(PersonID int PRIMARY KEY,Name varchar(255)) 

no problem, but then subsequently adding another table...

CREATE TABLE OrderTest(OrderID int PRIMARY KEY,OrderDesc varchar(255),PersonID int FOREIGN KEY REFERENCES PersonTest(PersonID)) 

Gives me a similar "Error 1: could not prepare statement (1 near "FOREIGN"; syntax error)".

 

The course content becomes hard to test this way :facepalm:

 

Share this post


Link to post
Share on other sites

Yeah, that's difficult to test. If I click on the question mark icon near the database then it looks like it is using the browser's WebSQL API to create the database, so it's actually going to be dependent on what your browser supports.

 

It sounds like WebSQL is based on an older version of SQLite that doesn't support foreign keys, so that's probably the issue. You might want to download and install MySQL in order to get a full-featured database to test with. Access might be another option if you already own it. You can probably find a free web host that will give you a hosting account with MySQL where you could test also.

Share this post


Link to post
Share on other sites

That indeed probably explains my issues and why they don't put a "Try it yourself" button after these code snippets !

 

Uh ... W3Schools ... maybe a good idea to mention that not all code in the course can be tested with the built-in Tryit Editor ?

 

Losing valuable time this way :Shock:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×