Full text search index independent of the table - mysql

In SQLite, we can create full text search indices as new objects, like so
CREATE VIRTUAL TABLE search_data USING fts4;
INSERT INTO search_data (description) select description from data;
In MySQL, the only way to do the same seems to be to alter the existing table like so
ALTER TABLE data ADD FULLTEXT(description);
What I am looking for is a way to create the full text index in MySQL as a new independent object without having to duplicate the data of the tables (like I can do in SQLite).
How can I do that?

You can give a name to an index, but the name is valid only within the table (e.g. you can have two indexes with the same name in if they are in different tables). Indexes don't have "global" names.
If you don't want to use ALTER TABLE you can use CREATE INDEX, for example:
create fulltext index INDEXNAME on TABLENAME (COLUMN);

Related

How to create an index on two fields within MYSQL

Currently I have a MYSQL query that's under performing. I need to create an index on two fields within my query: cancel and complete.
What is the best way to perform this?
SELECT* FROM table WHERE cancel != 'CANCEL' AND complete IN ('COMPLETE','TAKEN_BACK')
To create a multi-column index, but the column names in parentheses.
ALTER TABLE table
ADD INDEX (cancel, complete);
This is equivalent to
CREATE INDEX table_cancel_complete_ix
ON table (cancel, complete);
CREATE INDEX requires you to give a name to the index, it's optional with ALTER TABLE (it will be assigned a default name).
To add an index over more than one column (aka multi-column index or composite index), use this syntax:
ALTER TABLE your_table
ADD INDEX (cancel, complete);
However, you might want to make sure this is the reason for your statement to be underperforming. Take a look into the manual to dig deeper into this.

How can I create a copy of a table in Redshift WITHOUT DATA but with all the table schema (like compressions and sort keys etc.)

Is there a way to create a copy of a table in redshift without data? However, I want all the table schema (like compression, data types for each column sort keys and primary keys) to remain the same.
Check LIKE option for CREATE TABLE statement in the docs: CREATE TABLE
It doesn't preserve primary key but it duplicates columns and sort/dist keys like this:
CREATE TABLE new_table (LIKE existing_table);
This is the best you can get unless you keep the original DDL statement somewhere and just re-execute it with a new table name.

How do I change the data type for all columns in MySQL?

I want to change the datatype for all columns in my table mysql.
For instance varchar to double.
alter table myTable alter column vColumn int;
This will work as long as:
-all of the data will fit inside an int
-all of the data can be converted to int (i.e. a value of "car" will fail)
-there are no indexes that include vColumn. If there are indexes, you will need to include a drop and create for them to get back to where you were.
Changing of column types is possible with SQL command ALTER TABLE MODIFY COLUMN (it does not work in every DBMS, however).
Usually you have to remove data anyway, so another option would be to DROP TABLE (=remove it entirely) and create anew with desired columns (with CREATE TABLE). You could also remove just the single column (ALTER TABLE DROP COLUMN) and add a new one (ALTER TABLE NEW COLUMN) with the new definition.
Of course changing a column is so simple only as long as this column is not used in any constraints or keys
For syntax of the above commands see MySQL docs

Replacing one empty table with another table without affecting Foreign Keys in MySQL

I have migrated a database using structure only
In this database I have a table called hash that is empty, of course.
This table is being used by tons of other tables through foreign key.
I have another table called hash_copy that has been just copied from another database and is full of records (500'000 records).
I tried to replace one table by another with the following SQL Statement
SET FOREIGN_KEY_CHECKS=0;
RENAME TABLE hash to hash_empty, hash_copy to hash;
SET FOREIGN_KEY_CHECKS=1;
Problem is that now all my foreing keys are poiting to hash_empty which is what I was trying to avoid.
To sumup
I'd like to turn off Foreign Keys just to swap one table for another (throw the empty away and plug the full of records) without having to go through all the tables that makes references to it.
Is this possible?
Thanks in advance.
As per the comments, a table can be copied to another table like:
INSERT INTO hash SELECT * from hash_copy
More generally, the insert-select syntax works like as you would expect; you can specify column names (INSERT INTO hash(col1, col2, col3)) and include any SELECT syntax you normally would (functions, joins, where clauses, etc).

copy table from one database to another, maintaining table's structure and renaming it too

I want to copy a table (say tbl_1) from one database (say source_db) to another database (say target_db), with following things in consideration:
Structure of table should be preserved, including primary key and
auto-increment key
While creating a copy of tbl_1, I need to rename it to cpy_tbl_1
How it is possible using query?
P.S. I know there will be many similar questions like mine, but I have those special considerations too.
CREATE TABLE db_target.cloned_table
SELECT *
FROM db_source.source_table;
With the previous sentence the table will be created with just the fields and it's types, but no keys, constraints, engine will be set. You can specify them manually in the same sentence like this:
CREATE TABLE db_target.cloned_table (
a INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (a), KEY(b)
) ENGINE=MyISAM
SELECT b,c FROM db_source.source_table;
mysql create table doc
you can attain this as follows;
First create the target_db
mysql > create database target_db;
then use it
mysql > use target_db;
then create the structure of the tb1 with name cpy_tbl_1
which is done as
mysql> create table cpy_tbl_1 like source_db.tb1;
then just copy the data.
mysql > insert into cpy_tbl_1 select * from source_db.tb1;
and check results
if you are able to use shell scripting then mysqldump is a powerful and flexible option... especially if you can use a little sed to do search and replaces.
simple version:
mysqldump $SOURCE $TABLE | mysql $TARGET