i created a data base 'test' with table table 'user', inside the table there are 'id' and 'name' columns
CREATE TABLE `test`.`user` (`id` INT NOT NULL AUTO_INCREMENT , `name` TEXT NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
now i want to add a constraint so no digits allowed in the 'name' and i don't know how to do it. i'm pretty sure i need to add 'name' NOT LIKE '%[^0-9]%' but i just can't get it right
i tried ALTER TABLE `test`.`user` ADD CONSTRAINT no_digit_check CHECK ('name' NOT LIKE '%[^0-9]%'); but it gives me an error above 'CHECK' says a new statement was found but no delimiter
thanks
Use backtics, not apostrophes:
CHECK ( `name` ...)
instead of
CHECK ( 'name' ...)
Related
I would like to create a view that looks like the original tree_prototype table.
In this view the name should be set to the new_name value from the tree_prototype_conversion table if there is a matching old_name. If there is no match, the original name should stay.
I think I could do this in a case if I join on the name and old_name fields but that doesn't seem like the right approach. I feel like I am missing the obvious, and a CASE isn't it.
These are the tables in question:
CREATE TABLE IF NOT EXISTS tree
(
tree_id INTEGER PRIMARY KEY AUTOINCREMENT,
tree_prototype_id INTEGER,
FOREIGN KEY (tree_prototype_id) REFERENCES tree_prototype (tree_prototype_id)
);
CREATE TABLE IF NOT EXISTS tree_prototype
(
tree_prototype_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL COLLATE NOCASE,
height REAL NOT NULL DEFAULT(10) CHECK (height > 0)
);
CREATE TABLE IF NOT EXISTS tree_prototype_conversion
(
old_name TEXT UNIQUE NOT NULL COLLATE NOCASE,
new_name TEXT UNIQUE NOT NULL COLLATE NOCASE
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_tree_prototype
ON tree_prototype (name);
What I would like is a view that is both of these in one:
CREATE VIEW IF NOT EXISTS tree_prototype_converted AS
SELECT tree_prototype_id,
name,
height
FROM tree_prototype
WHERE name NOT IN (SELECT old_name FROM tree_prototype_conversion);
CREATE VIEW IF NOT EXISTS tree_prototype_converted AS
SELECT tree_prototype_id,
new_name as name,
height
FROM tree_prototype_conversion
JOIN tree_prototype as conversion on old_name=name;
After writing the CASE variant, my IDE suggested this:
CREATE VIEW IF NOT EXISTS tree_prototype_converted AS
SELECT tree_prototype_id,
coalesce(new_name, name) as name,
height
FROM tree_prototype
LEFT OUTER JOIN tree_prototype_conversion as conversion on old_name = name;
That looks right. Is it?
I'm trying to create a table in sqlite3 that will store user field selections and make each combination of user_id, table, and name_id unique. However running the following gets me "ERROR: near "table": syntax error"
CREATE TABLE preftest (
'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'user_id' INTEGER NOT NULL,
'table' TEXT NOT NULL,
'name_id' INTEGER NOT NULL,
CONSTRAINT userpref UNIQUE(user_id, table, name_id))
Is there some issue with combining text and integer columns in a unique constraint?
table is a SQLite keyword.
You need to use 'table' instead of table in the constraint:
CREATE TABLE preftest (
'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
'user_id' INTEGER NOT NULL,
'table' TEXT NOT NULL,
'name_id' INTEGER NOT NULL,
CONSTRAINT userpref UNIQUE(user_id, 'table', name_id))
I'm trying to use peewee's migration module to rename a column from name to title in my page table. However I'm running this confusing error:
peewee.OperationalError: table page__tmp__ has no column named FOREIGN
My guess is that it has something to do with the need to create an intermediary table when using sqlite.
Current Model:
Full source is here https://github.com/csytan/textmug/blob/master/db.py
class User(BaseModel):
id = peewee.CharField(primary_key=True, max_length=20)
class Page(BaseModel):
id = peewee.PrimaryKeyField()
name = peewee.TextField(unique=True, null=True)
user = peewee.ForeignKeyField(User, null=True, related_name='pages')
Migration Script:
from playhouse import migrate
my_db = migrate.SqliteDatabase('database')
migrator = migrate.SqliteMigrator(my_db)
with my_db.transaction():
migrate.migrate(
migrator.rename_column('page', 'name', 'title')
)
I ended up doing it manually with the sqlite3 command line tool:
BEGIN;
ALTER TABLE "page" RENAME TO "page_tmp";
CREATE TABLE "page" ("id" INTEGER NOT NULL PRIMARY KEY, "title" TEXT, "user_id" TEXT, "created" DATETIME NOT NULL, "text" TEXT NOT NULL, "public" SMALLINT NOT NULL, "encrypted" SMALLINT NOT NULL, FOREIGN KEY ("user_id") REFERENCES "user" ("id"));
INSERT INTO "page" SELECT "id","name","user_id","created","text","public","encrypted" FROM "page_tmp";
DROP TABLE "page_tmp";
COMMIT;
I am trying to convert the table that was dumped from mysql. Following is the code I have from mysql for creating the table:
CREATE TABLE "tbl_profession_attributes" (
"id" bigint(20) NOT NULL,
"tbl_profession_attribute_id" bigint(20) DEFAULT '0',
"code" varchar(10) NOT NULL,
"name" varchar(100) NOT NULL,
"keyword" text NOT NULL,
"tbl_profession_list_id" bigint(20) NOT NULL,
PRIMARY KEY ("id"),
KEY "tbl_passion_attribute_id" ("tbl_profession_attribute_id"),
KEY "tbl_passion_list_id" ("tbl_profession_list_id")
);
When I run this query for sqlite, I get the following error:
Query Error: near "KEY": syntax error Unable to execute statement
Can someone please help me resolve this.
Any help is much appreciated.
First, MySQL keyword KEY is synonym for INDEX. So this is not about foreign key at all.
Second, SQLite does not support creating non-primary index in CREATE TABLE statement. You should specify separate statement for create index, something like:
CREATE INDEX tbl_passion_attribute_id_idx
ON tbl_profession_attributes(tbl_profession_attribute_id)
I have one Auto Increment Field, rest are Integer,Text and Datetime field. How do I fix it out?
The Table Structure is given below:
CREATE TABLE "q1" (
"sb_id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"sb_title" text(100,0) NOT NULL,
"sb_details" text(300,0) NOT NULL,
"sb_image" text(30,0) NOT NULL,
"sb_type" integer(4,0) NOT NULL DEFAULT '1',
"sb_date" datetime NOT NULL
)
It could be because in your insert command
connection.execute("INSERT INTO q1(sb_title,sb_details)VALUES(?,?)",a,b);
you didn't insert any values for sb_image or sb_date, both of which are NOT NULL and have no default defined. SQLite doesn't know what to put in there. You should either take away the NOT NULL constraint on those columns, define a default for the columns, or insert something explicitly.