In my yii aplication we need to create dynamic table.
so i hav used like below:
Yii::app()->db->createCommand("CREATE TABLE {$tokenid}( `column1` INT(11) NOT NULL AUTO_INCREMENT, `column2` VARCHAR(255) NOT NULL, PRIMARY KEY (`column1`) ); ");
But it's not working. Any suggestion
You should use createTable() for this. An example from the docs:
// CREATE TABLE `tbl_user` (
// `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
// `username` varchar(255) NOT NULL,
// `location` point
// ) ENGINE=InnoDB
createTable('tbl_user', array(
'id' => 'pk',
'username' => 'string NOT NULL',
'location' => 'point',
), 'ENGINE=InnoDB')
You need to execute the command,
Yii::app()->db->createCommand("CREATE TABLE {$tokenid}( column1 INT(11) NOT NULL AUTO_INCREMENT, column2 VARCHAR(255) NOT NULL, PRIMARY KEY (column1) ); ")->execute();
or look at the create table function http://www.yiiframework.com/doc/api/1.1/CDbCommand#createTable-detail
Related
So I wanted to put a sql file in a database (es_extended.sql) and it keeps giving me errors like: duplicate column name name etc. and
SQL error (1050): Table 'items' already exists
I have tried to change the names of columns and all but it keeps giving the same errors.
here is the code:
ALTER TABLE `users`
ADD COLUMN `money` VARCHAR(50) DEFAULT NULL,
ADD COLUMN `name` VARCHAR(50) NULL DEFAULT '' AFTER `money`,
ADD COLUMN `skin` LONGTEXT NULL AFTER `name`,
ADD COLUMN `job` VARCHAR(50) NULL DEFAULT 'unemployed' AFTER `skin`,
ADD COLUMN `job_grade` INT NULL DEFAULT 0 AFTER `job`,
ADD COLUMN `loadout` LONGTEXT NULL AFTER `job_grade`,
ADD COLUMN `position` VARCHAR(36) NULL AFTER `loadout`
;
CREATE TABLE `items` (
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`limit` int(11) NOT NULL DEFAULT '-1',
`rare` int(11) NOT NULL DEFAULT '0',
`can_remove` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`name`)
);
CREATE TABLE `job_grades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_name` varchar(50) DEFAULT NULL,
`grade` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`label` varchar(50) NOT NULL,
`salary` int(11) NOT NULL,
`skin_male` longtext NOT NULL,
`skin_female` longtext NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `job_grades` VALUES (1,'unemployed',0,'unemployed','Unemployed',200,'{}','{}');
CREATE TABLE `jobs` (
`name` varchar(50) NOT NULL,
`label` varchar(50) DEFAULT NULL,
PRIMARY KEY (`name`)
;
INSERT INTO `jobs` VALUES ('unemployed','Unemployed');
CREATE TABLE `user_accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`name` varchar(50) NOT NULL,
`money` double NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE TABLE `user_inventory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`identifier` varchar(22) NOT NULL,
`item` varchar(50) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
Sql databases are a way to implement the persistence layer: data that should be kept permanently or at least independently from sessions.
The error you're getting: SQL error (1050): Table 'items' already exists means that you've already created a table with the same name you're using to try and create a new table.
You do not need to recreate the table each time you try to add data to it. The table is persistent as long as you do not DROP (delete) it or the database it belongs to.
If you want to recreate the table (either because you need to change its schema or because you want to start over), you can run the command DROP TABLE items which will delete the table and all data in the table.
After dropping, you can run the CREATE TABLE items... command again.
If you don't want to delete the table, you can just run the INSERT INTO items... command to add data to the existing table.
The SQL Error (xxxx): duplicate column name 'name' error means you are trying to add a column that already exists in the table. If the users table already has a column called name, then you cannot run the command ALTER TABLE users ADD COLUMN 'name'... without an error.
If the column doesn't meet your needs anymore, you can use ALTER TABLE users ALTER COLUMN 'name'... to change the schema for the column.
I am building a wordpress plugin which, when a function is called, will create a table in the database.
I have the following code:
function student_custom_data()
{
#create custom table if not exist
$sql ="CREATE TABLE IF NOT EXISTS Wick_Custom_Student_Data) (
id INT(6) NOT NULL AUTO_INCREMENT,
user_id INT(6) NOT NULL,
target INT(6) NOT NULL,
sen VARCHAR(10) NOT NULL,
disAd VARCHAR (10) NOT NULL,
PRIMARY KEY (id)
)"; }
When the function is called the table does not get created. There is no table called Wick_Custom_Student_Data already existing in the database.
Why is the database table not being created?
Ok, so I realise that for wordpress I needed to get and set the charset_collate property to the create table statement and use dbDelta to execute the SQL properly:
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql ="CREATE TABLE IF NOT EXISTS Wick_Custom_Student_Data (
id INT(6) NOT NULL AUTO_INCREMENT,
user_id INT(6) NOT NULL,
target INT(6) NOT NULL,
sen VARCHAR(10) NOT NULL,
disAd VARCHAR (10) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
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
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.
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.