This is the MySQL code I am using on the server to create a table...
CREATE TABLE brand_names (
id int(7) unsigned NOT NULL AUTO_INCREMENT,
brand_name varchar(255) NOT NULL,
parent_company_id int(7) NOT NULL DEFAULT '0',
last_modified_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY brand_name (brand_name),
FULLTEXT KEY name (brand_name)
) ENGINE=MyISAM AUTO_INCREMENT=225 DEFAULT CHARSET=latin1
I am trying to replicate the same table in Android by using...
CREATE TABLE brand_names (
_id integer PRIMARY KEY AUTOINCREMENT,
brand_name text NOT NULL,
parent_company_id integer NOT NULL DEFAULT '0',
last_modified_on datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE SET DEFAULT
)
However, I am getting this exception...
android.database.sqlite.SQLiteException: near "ON": syntax error (code 1): , while compiling: CREATE TABLE brand_names (_id integer PRIMARY KEY AUTOINCREMENT, brand_name text NOT NULL, parent_company_id integer NOT NULL DEFAULT '0', last_modified_on datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE SET DEFAULT);
So there seems to be something wrong around the ON part. (NB - All I am trying to achieve with the ON clause is to make it so that when the row is updated, the last_modified_on datetime will be automatically set to the current time.
I've looked at this SQLite page, and it looks like my syntax is correct, but clearly something is wrong so I'm a bit stumped. Can anyone help resolve?
The correct one is,
CREATE TABLE brand_names (
_id integer PRIMARY KEY AUTOINCREMENT,
brand_name text NOT NULL,
parent_company_id integer NOT NULL DEFAULT '0',
last_modified_on datetime NOT NULL DEFAULT CURRENT_TIMESTAMP);
You are using ON UPDATE SET DEFAULT in the end which is wrong syntax.
The last_modified_on datetime will be automatically set to the current time by using DEFAULT CURRENT_TIMESTAMP.
What you have seen from the SQLite doc is totally different from what you want to achieve.
The following query will work assuming you have a parent table with id parent_company_id -
CREATE TABLE brand_names(_id integer PRIMARY KEY AUTOINCREMENT,
brand_name text NOT NULL, parent_company_id integer NOT NULL DEFAULT '0',
last_modified_on datetime NOT NULL REFERENCES parent(parent_company_id)
ON UPDATE SET DEFAULT);
This will update the parent_company_id (the parent key of the foreign key constraint) column of the parent record without breaking referential integrity between the two tables parent and brand_names.
An ON UPDATE action is only taken if the values of the parent key are modified so that the new parent key values are not equal to the old. So this is used to configure actions that take place when modifying the parent key values of existing rows (ON UPDATE). They are associated with each foreign key in an SQLite database.
Related
I am writing an Android app and need a database for it. I will have three tables but only managed to make one right now. I do them in the console to debug and to implement them in my Javacode later. The following statements were succesfull:
sqlite3 progressapp.db
CREATE TABLE Z_Type(_Z_T_ID INTEGER PRIMARY KEY NOT NULL,
Description TEXT NOT NULL, Unit TEXT NOT NULL);
But now I want to refference the PK of T_Type in my other table:
CREATE TABLE goals (_Z_id INTEGER PRIMARY KEY NOT NULL, Title TEXT NOT NULL,
Type INTEGER NOT NULL, FOREIGN KEY(Type) REFERENCES Z_Type(_Z_T_ID),
Timeframe TEXT, Goaldate INTEGER);
Is Type INTEGER NOT NULL, FOREIGN KEY(Type) REFERENCES Z_Type(_Z_T_ID) a valid SQLite Statement in Android? It says "Error: near "Timeframe": syntax error" But I simply can't find it due to lack with SQL Experience I guess.
Is there a better way to reference the FK maybe?
Try this:
CREATE TABLE goals (_Z_id INTEGER PRIMARY KEY NOT NULL, Title TEXT NOT NULL,
Type INTEGER NOT NULL,Timeframe TEXT, Goaldate INTEGER, FOREIGN KEY(Type) REFERENCES Z_Type(_Z_T_ID));
I think that the order is important.
For further documentation you could visit sqlite.org/foreignkeys.html
You can define the reference as part of the column definition
CREATE TABLE goals (_Z_id INTEGER PRIMARY KEY NOT NULL, Title TEXT NOT NULL,
Type INTEGER NOT NULL REFERENCES Z_Type(_Z_T_ID),
Timeframe TEXT, Goaldate INTEGER);
In a sqlite CREATE TABLE statement, column definitions come first and table constraints only after that.
FOREIGN KEY(Type) REFERENCES Z_Type(_Z_T_ID) is a table constraint that should go at the end.
I have a simple table. I'm trying to put a default value to a TEXT column. Here is the table query:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT '0', book_name TEXT NOT NULL);"
It creates the table but the problem occurs when i try to insert a data. I was only trying with giving a book name to book_name, as i expected that the book_id would have a default value 0 to the column. But it doesn't and it adds the null value, so the row doesn't get inserted. I have also tried with this query:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT \'0\', book_name TEXT NOT NULL);"
But the problem remains the same. I have searched the stack overflow, and got some answers but they are old and not working for me right now. So has something changed on how to set the default value to a TEXT column in sqlite. Thanks in advance :)
EDIT
Here is the insert statement:
database.insert("book", null, cv);
Here cv is the object of ContentValues which contains only the value for the column book_name.
You can specify a default value for the column when you create the table. (It doesn't appear as though you can add a default using an ALTER statement, so you'll have to recreate your table.)
CREATE TABLE your_table_name
(MainContactName TEXT NOT NULL DEFAULT '')
For Example,
CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT,book TEXT DEFAULT "abc");
now see that , default value is set to "abc"
Check sqllite documentation.
To alter table,
sqlitedbInstance.execSQL("alter table myTable add column Address TEXT DEFAULT 'ABC' ");
I'm trying to build a simple application where the user isn't allowed to insert duplicate values.
I'm trying the Primary Key as one of the constraints and NOT NULL also. But NOT NULL doesn't seem to work.
I tried my best but failed to resolve it. Please help
((ID INTEGER NOT NULL PRIMARY KEY)";)
SQLite allows NULL values in the PRIMARY KEY column except that a column is INTEGER PRIMARY KEY column or the table is a WITHOUT ROWID table or the column is defined as a NOT NULL column.
For example:
CREATE TABLE suppliers(
supplier_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
Because this column is declared as INTEGER PRIMARY KEY, it will not accept NULL values.
Try this format:
sqlite> CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);
Try to use an UNIQUE constraint on the corresponding fields.
A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints. For each UNIQUE constraint on the table, each row must contain a unique combination of values in the columns identified by the UNIQUE constraint. For the purposes of UNIQUE constraints, NULL values are considered distinct from all other values, including other NULLs.
Edit:
According to your additional info you can use a CHECK constraint to prevent blank values.
sqlite> create table foo (bar TEXT, CHECK(bar <> ''));
See this answer for more information: Not empty string constraint in SQLite
This is my SQLite table structure
CREATE TABLE tbl_district (
districtId INTEGER PRIMARY KEY not null,
districtName TEXT unique NOT NULL
);
CREATE TABLE tbl_route (
routeId INTEGER,
districtId INTEGER NOT NULL REFERENCES tbl_district (districtId) ON DELETE CASCADE ON UPDATE CASCADE,
routeName TEXT NOT NULL,
primary key (routeId , districtId)
);
CREATE TABLE tbl_city (
cityId INTEGER PRIMARY KEY,
routeId INTEGER NOT NULL REFERENCES tbl_route (routeId) ON DELETE CASCADE ON UPDATE CASCADE,
cityName TEXT NOT NULL
);
If i execute
insert into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It works as it should
But if i execute
replace into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It gives following error
Error while executing query: foreign key mismatch - "tbl_city" referencing "tbl_route"
Any idea to resolve this?
Thanks in advance!
The documentation says that
the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.
The routeId column is the parent key of the foreign key in tbl_city, but has no UNIQUE constraint.
(Your REPLACE statement is not directly related to this; it's just when the database happend to notice the error.)
I'm trying to create a table and I've tried so many times to figure this out... for some reason it won't accept this.. it's saying something about the auto_increment
create table if not exists Assignments(
id auto_increment primary key,
class_name VARCHAR(30),
assignment_name VARCHAR(30) not null,
due_date VARCHAR(30) not null,
notes VARCHAR(30));
whats the problem?
EDIT: i am trying to use SQLite eventually but this command was written on my mySQL thru WAMP
First of all, Android uses SQLite, so your mysql tag is slightly incorrect unless I'm missing something you're doing.
Secondly, you would say
CREATE TABLE ASSIGNMENTS(id INTEGER PRIMARY KEY, class_name TEXT, assignment_name TEXT NOT NULL, due_date TEXT NOT NULL, notes TEXT);
"autoincrement" is handled automatically if you set your primary key as an INTEGER type, even though under the covers SQLite uses strings for everything
reference: SQLite datatypes
further reference: INTEGER PRIMARY KEY
Even more reference: "If an INSERT statement attempts to insert a NULL value into a rowid or integer primary key column, the system chooses an integer value to use as the rowid automatically. A detailed description of how this is done is provided separately."
It is autoincrement, not auto_increment