CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
How can I remove FULLTEXT line from MySQL dump above and comma on the line before so it looks something like this:
CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The removal should be easy using sed but I'm not sure how to remove this comma on the line above so the dump is successfully imported:
sed -i '/FULLTEXT KEY.*/d' dump.sql
Sometimes there is also more columns with FULLTEXT index:
CREATE TABLE `entity` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `company_name` (`company_name`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Sed script:
#!/bin/bash
sed -e '/FULLTEXT/d' |
sed -ne '
/ENGINE=InnoDB/!{H}
/ENGINE=InnoDB/{x; s/,[ \t]*$//; p; }
${g;p;}
'
Input:
CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `entity` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `company_name` (`company_name`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Sample Run:
/home/user> ./1.sed < input
CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `entity` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
sed is for simple substitutions on individual lines, that is all. For anything else you should use awk for clarity, brevity, portability, efficiency, robustness and most other desirable qualities of software. All of the sed constructs to do anything other than s, g, and p (with -n) became obsolete in the mid-1970s when awk was invented and exist today just for mental exercise.
Given this input:
$ cat file
CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `entity` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `company_name` (`company_name`),
FULLTEXT KEY `description` (`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
With GNU awk for multi-char RS:
$ awk -v RS=',\\s*FULLTEXT[^\n]*)' -v ORS= '1' file
CREATE TABLE `transaction` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `entity` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Since you are using GNU sed (for -i) I assume you will have no problem using GNU awk and if you need inplace editing add -i inplace at the start.
awk -vRS=";" 'NF{gsub(/),\n +FULLTEXT.*)/,")\n)",$0);$0=$0";"}1' file
Related
I think I'm using the right syntax for MariaDB, but my foreign key constraint is not being created.
Here's the create table DDL:
CREATE TABLE items (
id INT auto_increment primary key,
description TEXT NOT NULL
);
CREATE TABLE item_events (
id INT NOT NULL,
calendar_event_guid TEXT(255) NOT NULL,
foreign key item_events_id_fk (id) REFERENCES items (id)
);
Then, when I ask MariaDB to show me what I created, I get this:
+-------------+-----------------
| Table | Create Table +-------------+-----------------
| item_events | CREATE TABLE `item_events` (
`id` int(11) NOT NULL,
`calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------------+-----------------
or, just showing the DDL:
CREATE TABLE `item_events` (
`id` int(11) NOT NULL,
`calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Notice that it only created a "KEY", not a foreign key. The items table is correctly created.
Surely, this is really simple :)
Let's say I have a MySQL dump which creates a lot of tables.
Example:
CREATE TABLE `my_table` (
`id` bigint(20) NOT NULL,
`REVTYPE` tinyint(4) DEFAULT NULL
`some_other_column` varchar(255)
);
What whould be a valid regular expression to find the following:
All lines which start with "CREATE TABLE" and which contains "my_" in the table name
Then extracting the line containing "tinyint"
So the result would look like:
CREATE TABLE `my_table` (
`REVTYPE` tinyint(4) DEFAULT NULL
This regex seems to work:
^((CREATE.*my_.*\n)|(\s+.*tinyint.*\n)|(\s+.*(?!tinyint)\n))
CREATE TABLE `my_table` (
`id` bigint(20) NOT NULL,
`id` bigint(22) NOT NULL,
`REVTYPE` tinyint(4) DEFAULT NULL,
`id` bigint(20) NOT NULL,
`REVTYPE` tinyint(5) DEFAULT NULL,
`some_other_column` varchar(255)
);
becomes (replace with $2$3) :
CREATE TABLE `my_table` (
`REVTYPE` tinyint(4) DEFAULT NULL,
`REVTYPE` tinyint(5) DEFAULT NULL,
);
[I assume the OP wants the ); at the end -advise if not true.]
.
See regex101 link:
the following query should only return all cities starting with "Ö" (German umlaut).
letter = 'Ö'
City.objects.filter(name__istartswith=letter)
But it returns cities starting with O and Ö.
I use django 1.11 and mariadb.
I allready set COLLATE on that table to utf8_bin but this haven't changed the behavior within django.
This is the simplified SQL query
SELECT `cities_city`.`name` FROM `cities_city` WHERE `cities_city`.`name` LIKE "Ö%";
and here the SHOW CREATE TABLE output:
SHOW CREATE TABLE `cities_city`
-> ;
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| cities_city | CREATE TABLE `cities_city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) CHARACTER SET utf8 NOT NULL,
`slug` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`name_std` varchar(200) CHARACTER SET utf8 NOT NULL,
`location` point NOT NULL,
`population` int(11) NOT NULL,
`elevation` int(11) DEFAULT NULL,
`kind` varchar(10) CHARACTER SET utf8 NOT NULL,
`timezone` varchar(40) CHARACTER SET utf8 NOT NULL,
`country_id` int(11) NOT NULL,
`region_id` int(11) DEFAULT NULL,
`subregion_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cities_city_country_id_2f07e352_uniq` (`country_id`,`region_id`,`subregion_id`,`id`,`name`),
KEY `cities_city_b068931c` (`name`),
KEY `cities_city_16c3f481` (`name_std`),
KEY `cities_city_region_id_0227cdac_fk_cities_region_id` (`region_id`),
KEY `cities_city_subregion_id_9fbab97d_fk_cities_subregion_id` (`subregion_id`),
CONSTRAINT `cities_city_country_id_779ae117_fk_cities_country_id` FOREIGN KEY (`country_id`) REFERENCES `cities_country` (`id`),
CONSTRAINT `cities_city_region_id_0227cdac_fk_cities_region_id` FOREIGN KEY (`region_id`) REFERENCES `cities_region` (`id`),
CONSTRAINT `cities_city_subregion_id_9fbab97d_fk_cities_subregion_id` FOREIGN KEY (`subregion_id`) REFERENCES `cities_subregion` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11468436 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
The problem is a subtle one.
The clue is here:
SELECT * FROM information_schema.`COLUMNS` WHERE table_name = 'cities_city';
The explanation...
`name` varchar(200) CHARACTER SET utf8 NOT NULL,
is COLLATE utf8_general_ci because that is the default collation for utf8.
This table default:
) ENGINE=InnoDB AUTO_INCREMENT=11468436 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
gives utf8_bin to any newly added rows.
Perhaps you did the obvious ALTER TABLE to change to _bin? Instead:
ALTER TABLE cities_city
CONVERT TO CHARACTER SET utf8
COLLATE utf8_bin;
this will go into each string column and make the change. Note that indexes (etc) must be rebuilt when the collation changes.
I am stuck with my parent-child mysql tables. I'm using preparedstatements in Java programming language. But I'm only successful with inserting data to my parent table. However, I can't insert data to my child table after executing another insert statement.
These are the glimpse of my tables:
tbl_patient:
ID (primary key)
patientName (primary key)
address
contact
tbl_admission:
ID (primary key)
admitDate
patientName (foreign key referenced from tbl_patient)
When I executed my statements for inserting a new patient, it became successful and I got the result stored to my tbl_patient table. However, my problem started when I executed my preparedstatements for tbl_admission since it cannot add data to my tbl_admission. Instead, I get the following error:
Cannot add or update a child row: a foreign key constraint fails (hms_mdh/admission, CONSTRAINT FK_admission_1 FOREIGN KEY (ID, patientName) REFERENCES tbl_patient (ID, patientName))
I really don't know what's happening here. Can I get any help? Thanks.
This is my sample sql in my child table - the admission table:
CREATE TABLE admission (
ID int(11) NOT NULL auto_increment,
admitID varchar(45) NOT NULL default '',
admitDate varchar(45) NOT NULL default '',
patientID varchar(45) NOT NULL default '',
entrance varchar(45) NOT NULL default '',
doctor varchar(45) NOT NULL default '',
initialDiagnosis varchar(45) NOT NULL default '',
recommend varchar(100) NOT NULL default '',
patientName varchar(45) NOT NULL default '',
PRIMARY KEY (ID),
KEY FK_admission_1 (ID,patientName),
CONSTRAINT FK_admission_1 FOREIGN KEY (ID, patientName) REFERENCES tbl_patient (ID, patientName)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 11264 kB; (ID patientName) REFER `hms_mdh/t'
And this is the parent table--the patients:
CREATE TABLE tbl_patient (
ID int(11) NOT NULL auto_increment,
patientID varchar(45) NOT NULL default '',
gender varchar(45) NOT NULL default '',
birthday varchar(45) NOT NULL default '',
chiefcomplain varchar(100) NOT NULL default '',
entrance varchar(45) NOT NULL default '',
patientName varchar(45) NOT NULL default '',
PRIMARY KEY (ID,patientName)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I have a problem with mysql c++ connector when i want to insert a string with a prepared statement he reduce my string in the database(saved in a longtext) . I have enormous loss of data because I want to save a longtext.
here is my code :
void RequetteBDD::add(Files::Fichier file)
{
string query = "INSERT INTO files(titre,url,type,txt,lastcrawl) VALUES (?,?,?,?,?)";
sql::PreparedStatement *prep_stmt;
prep_stmt = con->prepareStatement(query);
prep_stmt->setString(1,file.getNom()); //title
prep_stmt->setString(2,file.getURL().getUri()); //url
prep_stmt->setInt(3,file.getTypeInt()); //type
//i also try :
istringstream stream(file.getTextFull());
prep_stmt->setBlob(4,&stream);
//but the saved length was exactly the same.
prep_stmt->setString(4,file.getTextFull()); //here is the probleme
prep_stmt->setInt(5,time(NULL)); //timstamp
prep_stmt->execute();
delete prep_stmt;
}
mysql ddb:
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`titre` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
`type` int(1) NOT NULL DEFAULT '0' COMMENT ,
`txt` longtext COLLATE utf8_unicode_ci NOT NULL,
`lastcrawl` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
thanks for your help.
It was a encoding problem; they are two solution to solve the probleme:
change the encoding from the database to ASCII
Or change the encoding from the string can be easely do with boost::locale::conv
I hope it will help other people.