SQLite database creating through script - sql

There is the following script:
CREATE TABLE IF NOT EXISTS `location_cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`location_region_id` int(11) NOT NULL DEFAULT '0',
`location_district_id` int(11) DEFAULT NULL,
`location_country_id` int(11) NOT NULL DEFAULT '0',
`lon` float(11,8) NOT NULL DEFAULT '0.00000000',
`lat` float(11,8) NOT NULL DEFAULT '0.00000000',
`prefix` varchar(50) DEFAULT NULL,
`name` varchar(128) NOT NULL,
`size` int(3) NOT NULL DEFAULT '0' COMMENT 'Размер города',
`tz_name` varchar(128) DEFAULT NULL,
`timezone` varchar(100) NOT NULL DEFAULT '+00:00',
`timezone2` varchar(100) NOT NULL DEFAULT '+00:00',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=12393 ;
When I try to execute it using 'sqlite3 db/development.sqlite3 < alogist.sql' I got error 'Error: near line 27: near "AUTO_INCREMENT": syntax error' (27 - the line with "CREATE_TABLE ...". So, what's the trouble? How can I fix it? Thanks!

In sqlite, an autoincrement column is specified as
INTEGER PRIMARY KEY AUTOINCREMENT
Replace your int(11) NOT NULL AUTO_INCREMENT with that.
Further problems:
COMMENT is not supported. Remove COMMENT 'Размер города'
Remove PRIMARY KEY (ID) - the primary key has already been specified.
Remove the MySQL-specific ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=12393.

SQLite syntax:
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL

Related

Foreign key constraint is incorrectly formed when import table

I have some question about database structure, laravel orm and constraints.
So first I have some tables(this code is from my DB dump):
CREATE TABLE `person` (
`person_id` int(10) UNSIGNED NOT NULL,
`first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`middle_name` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker` (
`worker_id` int(10) UNSIGNED NOT NULL,
`hire_date` date NOT NULL,
`person_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker_contract` (
`worker_contract_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci,
`worker_id` int(10) UNSIGNED NOT NULL,
`salary` decimal(10,2) NOT NULL,
`worker_id_boss` int(10) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `worker_specialty` (
`worker_specialty_id` int(10) UNSIGNED NOT NULL,
`worker_id` int(10) UNSIGNED NOT NULL,
`notes` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `describe` (
`describe_id` int(10) UNSIGNED NOT NULL,
`person_id` int(10) UNSIGNED DEFAULT NULL,
`describe` text COLLATE utf8mb4_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `person`
MODIFY `person_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=47;
ALTER TABLE `worker`
MODIFY `worker_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=45;
ALTER TABLE `worker_contact`
MODIFY `worker_contact_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
ALTER TABLE `describe`
MODIFY `describe_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
ALTER TABLE `person`
ADD PRIMARY KEY (`person_id`);
ALTER TABLE `worker`
ADD PRIMARY KEY (`worker_id`),
ADD KEY `person_id_FK` (`person_id`) USING BTREE;
ALTER TABLE `worker_contract`
ADD PRIMARY KEY (`worker_contract_id`),
ADD KEY `worker_id_FK` (`worker_id`) USING BTREE,
ADD KEY `person_id_boss_foreign` (`person_id_boss`);
ALTER TABLE `describe`
ADD PRIMARY KEY (`describe_id`);
ALTER TABLE `worker_contract`
ADD CONSTRAINT `worker_id_boss_foreign` FOREIGN KEY (`worker_id_boss`) REFERENCES `person` (`person_id`) ON DELETE CASCADE;
ALTER TABLE `describe`
ADD CONSTRAINT `person_id_tag_foreign` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`) ON DELETE SET NULL;
Here is my problem, when I delete person table from databasa and I want to import it without checking foregin key I have #1005 cannot create 'db'.'person' (Errno: 150 "Foreign key constraint is incorrectly formed").
I create migration with Laravels ORM. All id field are the same type and there no mistakes in name fields.
When I remove constraint from DB it works and phpMyAdmin didn't show the relations between models. When I import db dump there is no errors.
When I have describet models in php code, in which place ORM use relations from model class? Only in app place?
When I remove constraints the db efficiency will will get smaller? And is it need?
What I should fix or what here is wrong?
First, disable FOREIGN_KEY_CHECKS
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); // disable
//do your import logic
DB::statement('SET FOREIGN_KEY_CHECKS=1;'); //enable

Sql file is throwing errors when importing, but used to work fine

I was woring on a project last year, and was exporting and importing fine, but recently I decided to import my sql file and I cannot import due to lots of errors in each table.
I deleted xampp once I stopped working on it, and now decided to download it again, but am stuck on import, here is my full file:
http://pastebin.com/mddVUU1i
My error is the following:
CREATE TABLE company_type ( company_type_id int(11) NOT NULL,
company_type varchar(50) NOT NULL, company_type_date datetime
NOT NULL DEFAULT CURRENT_TIMESTAMP, company_type_enabled
varchar(15) NOT NULL DEFAULT 'enabled' ) ENGINE=InnoDB DEFAULT
CHARSET=latin1 MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '-----------------------------------------
CREATE TABLE company_type ( `comp' at line 1
But when I delete that, I get another error:
CREATE TABLE employees ( employees_id int(11) NOT NULL,
employees_page int(11) NOT NULL, employees_page_type varchar(15)
NOT NULL, employees_user int(11) NOT NULL, employees_date
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, employees_manage
varchar(15) NOT NULL DEFAULT 'sent' ) ENGINE=InnoDB DEFAULT
CHARSET=latin1 MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '-----------------------------------------
------------------------------------' at line 1
How can I fix this? I have ran the sql through checkers online, and it state'sthe sql is fine.
Your table creation is wrong, you are misplacing datetime data-type to timestamp values.
Here is your first erroneous table
CREATE TABLE company_type ( company_type_id int(11) NOT NULL,
company_type varchar(50) NOT NULL, company_type_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, company_type_enabled varchar(15) NOT NULL DEFAULT 'enabled' ) ENGINE=InnoDB DEFAULT CHARSET=latin1
The problem here is
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
You are using datetime datatype to timestamp values.
if you want to use timestamp datatype then do it this way
CREATE TABLE employees ( employees_id int(11) NOT NULL,
employees_page int(11) NOT NULL, employees_page_type varchar(15) NOT NULL, employees_user int(11) NOT NULL,
employees_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
employees_manage varchar(15) NOT NULL DEFAULT 'sent' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
if you want to use datetime datatype then do it this way
CREATE TABLE employees1 ( employees_id int(11) NOT NULL,
employees_page int(11) NOT NULL, employees_page_type varchar(15) NOT NULL, employees_user int(11) NOT NULL,
employees_date datetime NOT NULL,
employees_manage varchar(15) NOT NULL DEFAULT 'sent' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Sql developer error, invaild character. Creating Youtube database

I am having trouble running a sql database, I wanted to create a youtube like database. I got this error.
CREATE TABLE `Like` ( `video_id` int(5) NOT NULL DEFAULT '0', `customer_id` int(6) NOT NULL DEFAULT '0', `rating` int(1) NOT NULL DEFAULT '0', `date` date NOT NULL DEFAULT '0000-00-00', PRIMARY KEY (`video_id`,`customer_id`), KEY `date` (`date`), KEY `video_id` (`video_id`), KEY `customer_id` (`customer_id`), KEY `rating` (`rating`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1
Error report -
SQL Error: ORA-00911: invalid character
00911. 00000 - "invalid character"'
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
CREATE TABLE `movies` ( `id` int(5) NOT NULL DEFAULT '0', `year` int(4) DEFAULT '0', `title` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `Channel` ( `movie_id` int(5) NOT NULL DEFAULT '0', `customer_id` int(6) NOT NULL DEFAULT '0', KEY `movie_id` (`movie_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `qualifying` ( `customer_id` int(6) NOT NULL DEFAULT '0', `date` date NOT NULL DEFAULT '0000-00-00', `movie_id` int(5) NOT NULL DEFAULT '0', KEY `movie_id` (`movie_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `Like` ( `video_id` int(5) NOT NULL DEFAULT '0', `customer_id` int(6) NOT NULL DEFAULT '0', `rating` int(1) NOT NULL DEFAULT '0', `date` date NOT NULL DEFAULT '0000-00-00', PRIMARY KEY (`video_id`,`customer_id`), KEY `date` (`date`), KEY `video_id` (`video_id`), KEY `customer_id` (`customer_id`), KEY `rating` (`rating`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Your main problem is that you are trying to run a SQL script with MySQL syntax on an Oracle database (you are clearly on an Oracle database because you are getting the ORA-00911 error).
Either you write your script using Oracle syntax, or change databases and use MySQL.
From your Error message and script it is clear that, You are trying to run Mysql table scripts in Oracle Database which is why you are getting error.
Here are few changes that you need to make in table scripts to wokr in Oracle database
Replace back-ticks with double quotes(")
Replace int(N) datatype with Number(P)
Example
CREATE TABLE "like"
  ( 
     "video_id"   Number(10) NOT NULL DEFAULT '0', 
     "customer_id"Number(10) NOT NULL DEFAULT '0', 
     "rating"     Number(10) NOT NULL DEFAULT '0', 
     "date"       DATE NOT NULL DEFAULT '0000-00-00', 
CONSTRAINT video_customers_pk PRIMARY KEY ("video_id", "customer_id")
     KEY "date"("date"),  -- Not sure how to change this in oracle
     KEY video_id("video_id"), 
     KEY customer_id("customer_id"), 
     KEY rating("rating") 
  ) 
Note : I am not sure about the equivalent of this KEY video_id("video_id") in Oracle

Gii CRUD generator and related tables

I am using Yii framework and I have got a problem with CRUD generator.
I have got two tables called Users and news with the following structures:
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` varchar(1000) COLLATE utf8_persian_ci DEFAULT NULL,
`user_id` tinyint(3) unsigned NOT NULL,
`title` varchar(100) COLLATE utf8_persian_ci DEFAULT NULL,
`body` varchar(1000) COLLATE utf8_persian_ci DEFAULT NULL,
`publishedat` date DEFAULT NULL,
`state` tinyint(1) unsigned DEFAULT NULL,
`archive` tinyint(1) unsigned DEFAULT NULL,
`last_modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `news_FKIndex1` (`keyword`(255)),
KEY `news_FKIndex2` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(128) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastvisit_at` timestamp NULL DEFAULT NULL,
`is_disabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `status` (`is_disabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
when I generate a CRUD using Gii for my news table I cannot see the fields for users table. Instead of user_id I want to see the username in the table created by CRUD generator. How can I make a change in the code to get the result as above?
First, user_id needs to be a foreign key field not just a key field.
Second, gii will not generate the field as you require by default. For such functionality an extension such as Giix might help. However, since a relation exists you could always use relationName.username to display the username in a grid view or a list view.

How to insert into a table join other table using Activerecord in CodeIgniter?

I'm new to CodeIgniter and ORM, I hope you guys can help me with this.
The question table:
CREATE TABLE `question` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL DEFAULT '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The answer table:
CREATE TABLE `answer` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) unsigned NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`),
KEY `question_id` (`question_id`),
CONSTRAINT `answer_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `question` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The equivalent SQL is:
INSERT INTO answer(content, question_id)
VALUES('Ironman', (select id
from question
where title ='favourite characters'
and content = 'Who is your favourite characters in Avanger?'));
Anyone can tell me how to achieve the same thing but using CodeIgniter Activerecord?
Don't do that, instead use the primary key (id) to insert directly into the base table.