I am trying to remove a column from a MySQL table that I am using with Handsontable. When I remove a column from the Handsontable, I can get the column's index using the afterRemoveCol() callback:
afterRemoveCol: function (index, amount) {
alert(index +' amount: '+amount);
}
I would like to remove the column using the column number (n) returned by this callback function from the MySQL table using something like:
ALTER TABLE tbl_Blah DROP COLUMN n;
So, if I want to drop column #3 from the MySQL table, How would I do this using just the column number?
To add to RMathis answer, you can do everything within SQL by also using SET to define the DROP string in conjuntion with PREPARE and EXECUTE
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`col1` varchar(1),
`col2` varchar(1),
`col3` varchar(1),
`col4` varchar(1),
`col5` varchar(1))
;
set #col = (select column_name
from information_schema.columns
where table_name='table1' and ordinal_position=3);
SET #s = CONCAT("alter table table1 drop column ", #col);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Query 1:
desc table1
Results:
| COLUMN_NAME | COLUMN_TYPE | IS_NULLABLE | COLUMN_KEY | COLUMN_DEFAULT | EXTRA |
|-------------|-------------|-------------|------------|----------------|-------|
| col1 | varchar(1) | YES | | (null) | |
| col2 | varchar(1) | YES | | (null) | |
| col4 | varchar(1) | YES | | (null) | |
| col5 | varchar(1) | YES | | (null) | |
SQL from SQL can generate the drop statement using ordinal_position from information_schema.columns.
mysql test> create table test.tab (col1 char(1), col2 char(1), col3 char(1), col4 char(1), col5 char(1));
Query OK, 0 rows affected (0.01 sec)
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col3 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)
This query is generating the statement to drop the third column...
mysql test> select concat('alter table ',table_schema,'.',table_name,' drop column ',column_name,';') as query1 from information_schema.columns where table_schema='test' and table_name='tab' and ordinal_position=3;
+----------------------------------------+
| query1 |
+----------------------------------------+
| alter table test.tab drop column col3; |
+----------------------------------------+
1 row in set (0.00 sec)
Now to run the projected command...
mysql test> alter table test.tab drop column col3;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
The column definition has been removed from the table.
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
Name them (columns in your db mirroring the spreadsheet) with the numbers/letters in full correspondence with your spreadsheet.
And then you'd be able to use regular ALTER TABLE ... DROP COLUMN.
Related
I how defined a procedure in MySql which has an input and input will be a name of column that will be added to table 'test1' but sql names that column input instead of using the value of input .how can i do this in the right way?
DELIMITER
CREATE PROCEDURE p1
(IN input CHAR(20))
BEGIN
ALTER TABLE test1
ADD COLUMN input char(20);
END
DELIMITER ;
You should google dynamic sql and read the manual https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
In the meantime here's an example.
drop procedure if exists p;
alter table users
drop column abc;
DELIMITER $$
CREATE PROCEDURE p
(IN input CHAR(20))
BEGIN
#ALTER TABLE test1
#ADD COLUMN input char(20);
set #sql = concat('alter table users add column ',input,' char(20);');
select #sql;
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
END $$
DELIMITER ;
call p('abc');
describe users;
MariaDB [sandbox]> call p('abc');
+--------------------------------------------+
| #sql |
+--------------------------------------------+
| alter table users add column abc char(20); |
+--------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.42 sec)
MariaDB [sandbox]> describe users;
+---------------------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+-------------------+-------+
| id | int(11) | NO | PRI | NULL | |
| userName | varchar(60) | NO | | NULL | |
| photo | varchar(50) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| ts | datetime | YES | | CURRENT_TIMESTAMP | |
| events_participated | int(11) | YES | | NULL | |
| fb_uid | int(11) | YES | | NULL | |
| Column_name | varchar(10) | YES | | NULL | |
| post_type | varchar(10) | YES | | NULL | |
| password | varchar(8) | YES | | NULL | |
| abc | char(20) | YES | | NULL | |
+---------------------+-------------+------+-----+-------------------+-------+
11 rows in set (0.03 sec)
I am trying to remove a column from a MySQL table that I am using with Handsontable. When I remove a column from the Handsontable, I can get the column's index using the afterRemoveCol() callback:
afterRemoveCol: function (index, amount) {
alert(index +' amount: '+amount);
}
I would like to remove the column using the column number (n) returned by this callback function from the MySQL table using something like:
ALTER TABLE tbl_Blah DROP COLUMN n;
So, if I want to drop column #3 from the MySQL table, How would I do this using just the column number?
To add to RMathis answer, you can do everything within SQL by also using SET to define the DROP string in conjuntion with PREPARE and EXECUTE
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`col1` varchar(1),
`col2` varchar(1),
`col3` varchar(1),
`col4` varchar(1),
`col5` varchar(1))
;
set #col = (select column_name
from information_schema.columns
where table_name='table1' and ordinal_position=3);
SET #s = CONCAT("alter table table1 drop column ", #col);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Query 1:
desc table1
Results:
| COLUMN_NAME | COLUMN_TYPE | IS_NULLABLE | COLUMN_KEY | COLUMN_DEFAULT | EXTRA |
|-------------|-------------|-------------|------------|----------------|-------|
| col1 | varchar(1) | YES | | (null) | |
| col2 | varchar(1) | YES | | (null) | |
| col4 | varchar(1) | YES | | (null) | |
| col5 | varchar(1) | YES | | (null) | |
SQL from SQL can generate the drop statement using ordinal_position from information_schema.columns.
mysql test> create table test.tab (col1 char(1), col2 char(1), col3 char(1), col4 char(1), col5 char(1));
Query OK, 0 rows affected (0.01 sec)
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col3 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)
This query is generating the statement to drop the third column...
mysql test> select concat('alter table ',table_schema,'.',table_name,' drop column ',column_name,';') as query1 from information_schema.columns where table_schema='test' and table_name='tab' and ordinal_position=3;
+----------------------------------------+
| query1 |
+----------------------------------------+
| alter table test.tab drop column col3; |
+----------------------------------------+
1 row in set (0.00 sec)
Now to run the projected command...
mysql test> alter table test.tab drop column col3;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
The column definition has been removed from the table.
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
Name them (columns in your db mirroring the spreadsheet) with the numbers/letters in full correspondence with your spreadsheet.
And then you'd be able to use regular ALTER TABLE ... DROP COLUMN.
foo_bars is a many-2-many table with both columns pointing to foo.id
I want foo_bars.[id1, id] to for a unique key
How do I avoid same id being used in a foo_bars entry.
i.e. insert into foo_bars (2,2) - How do I avoid this?
mysql> create table foo (id int(11), name varchar(255));
Query OK, 0 rows affected (0.49 sec)
mysql> desc foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create table foo_bars (id1 int(11), id int(11));
Query OK, 0 rows affected (0.34 sec)
mysql> desc foo_bars;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id1 | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
you can add a unique contatraint
CHECK (id1<>id)
create table foo_bars (id1 int(11), id int(11),CHECK (id1<>id));
You can create a trigger, as follows:
DELIMITER $$
CREATE TRIGGER `test_id_uniqueness` BEFORE INSERT ON `foo_bars`
FOR EACH ROW
BEGIN
IF NEW.id = NEW.id1 THEN
SIGNAL SQLSTATE '12345';
SET MESSAGE_TEXT := 'foo_bars.ID and foo_bars.ID1 cannot be the same';
END IF;
END$$
DELIMETER ;
I am having issues inserting Id fields from two tables into a single record in a third table.
mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | UNI | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.22 sec)
mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | varchar(64) | NO | UNI | NULL | |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ID_Title | int(10) unsigned | NO | MUL | NULL | |
| ID_Category | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
Let's say the data from the tables is:
mysql> select * from ing_titles;
+----------+-------------------+
| ID_Title | title |
+----------+-------------------+
| 3 | Chicken |
| 2 | corn |
| 1 | Fettucini Alfredo |
+----------+-------------------+
3 rows in set (0.00 sec)
mysql> select * from ing_categories;
+-------------+----------+
| ID_Category | category |
+-------------+----------+
| 1 | Dinner |
| 3 | Meat |
| 2 | Veggie |
+-------------+----------+
3 rows in set (0.00 sec)
I want to insert into ing_title_categories the record "corn, Veggie" or where ID_Title = 2 and ID_Category = 2.
Here's what I tried:
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT ing_titles.ID_Title, ing_categories.ID_Category
FROM ing_title_categories
LEFT JOIN ing_titles ON ing_title_categories.ID_Title=ing_titles.ID_Title
LEFT JOIN ing_categories ON ing_title_categories.ID_Category=ing_categories.ID_Category
WHERE (ing_titles.ID_Title=2) AND (ing_categories.ID_Category = 2);
There is no data inserted into the table ing_title_categories, and here is the reply from MySQL:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
What is the correct syntax for inserting the ing_titles.ID_Title and ing_categories.ID_Category into the table ing_titles_categories?
Please, no PHP or Python examples. Use SQL that I can copy and paste into the MySQL prompt. I will be adding this to a C++ program, not PHP, JavaScript or Python.
Edit 1:
The ing_title_categories.ID_Title and ing_title_categories.ID_Category are foreign keys into the other tables.
INSERT INTO
ing_title_categories (ID_Title, ID_Category)
SELECT
ing_titles.ID_Title, ing_categories.ID_Category
FROM
ing_titles, ing_categories
WHERE
ing_titles.ID_Title = ing_categories.ID_Category AND
ing_titles.ID_Title = 2 AND ing_categories.ID_Category = 2;
SQL Fiddle demo
After taking advice from #DrewPierce and #KaiserM11, here is the MySQL sequence:
mysql> INSERT INTO ing_title_categories (ID_Title, ID_Category)
-> SELECT
-> ing_titles.ID_Title,
-> ing_categories.ID_Category
-> FROM ing_titles, ing_categories
-> where (ing_titles.ID_Title = 2) AND (ing_categories.ID_Category = 2)
-> ;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from ing_title_categories;
+-------------------+----------+-------------+
| ID_Title_Category | ID_Title | ID_Category |
+-------------------+----------+-------------+
| 17 | 2 | 2 |
+-------------------+----------+-------------+
1 row in set (0.00 sec)
In this case, only possible way I see is using a UNION query like
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT Title, NULL
FROM ing_title WHERE ID_Title = 2
UNION
SELECT NULL, category
FROM ing_categories
WHERE ID_Category = 2
(OR)
You can change your table design and use an AFTER INSERT trigger to perform the same in one go.
EDIT:
If you can change your table design to something like below (No need of that extra chaining table)
ing_titles(ID_Title int not null auto_increment PK, title varchar(64) not null);
ing_categories( ID_Category int not null auto_increment PK,
category varchar(64) not null,
ing_titles_ID_Title int not null,
FOREIGN KEY (ing_titles_ID_Title)
REFERENCES ing_titles(ID_Title));
Then you can use a AFTER INSERT trigger and do the insertion like
DELIMITER //
CREATE TRIGGER ing_titles_after_insert
AFTER INSERT
ON ing_titles FOR EACH ROW
BEGIN
-- Insert record into ing_categories table
INSERT INTO ing_categories
( category,
ing_titles_ID_Title)
VALUES
('Meat' NEW.ID_Title);
END; //
DELIMITER ;
Supposing a query such as:
SELECT * FROM tableA;
How can I prepend a_ to each columns' name? For example if there is a column "username" it would be accessed in the results as "a_username".
EDIT: The SELECT username AS a_username format will not help as I need to continue using the * field selection. There is a JOIN and a potential conflict with a returned column from another table in the JOIN. I will be iterating over the returned columns (foreach) and only want to output the columns that came from a particular table (whose schema may change) to HTML input fields where a site admin could edit the fields' content directly. The SQL query in question looks like SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField and their exists the possibility that someField or someOtherField exists in firstTable.
Thanks.
You can use the INFORMATION_SCHEMA.COLUMNS table to formulate the query and then use dynamic SQL to execute it.
First let's make a sample database called dotancohen and a table called mytable
mysql> drop database if exists dotancohen;
Query OK, 1 row affected (0.03 sec)
mysql> create database dotancohen;
Query OK, 1 row affected (0.00 sec)
mysql> use dotancohen
Database changed
mysql> create table mytable
-> (
-> id int not null auto_increment,
-> username varchar(30),
-> realname varchar(30),
-> primary key (id)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> insert into mytable (realname,username) values
-> ('rolando','odnalor'),('pamela','alemap'),
-> ('dominique','euqinimod'),('diamond','dnomaid');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from mytable;
+----+-----------+-----------+
| id | username | realname |
+----+-----------+-----------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+----+-----------+-----------+
4 rows in set (0.00 sec)
mysql>
Here is the metadata table called INFORMATION_SCHEMA.COLUMNS:
mysql> desc INFORMATION_SCHEMA.COLUMNS;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(27) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
+--------------------------+---------------------+------+-----+---------+-------+
19 rows in set (0.02 sec)
mysql>
What you need from this table are the following columns:
table_schema
table_name
column_name
ordinal_position
What you are asking for is to have the column_name and the column_name prepended with a_
Here is the query and how to execute it:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = 'dotancohen' and table_name = 'mytable'
order by ordinal_position) A;
select #newsql;
prepare stmt from #newsql;
execute stmt;
deallocate prepare stmt;
Let's execute it
mysql> select concat('select ',column_list,' from ',dbtb) into #newsql
-> from (select group_concat(concat(column_name,' a_',column_name)) column_list,
-> concat(table_schema,'.',table_name) dbtb from information_schema.columns
-> where table_schema = 'dotancohen' and table_name = 'mytable'
-> order by ordinal_position) A;
Query OK, 1 row affected (0.01 sec)
mysql> select #newsql;
+--------------------------------------------------------------------------------+
| #newsql |
+--------------------------------------------------------------------------------+
| select id a_id,username a_username,realname a_realname from dotancohen.mytable |
+--------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> prepare stmt from #newsql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> execute stmt;
+------+------------+------------+
| a_id | a_username | a_realname |
+------+------------+------------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+------+------------+------------+
4 rows in set (0.01 sec)
mysql> deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)
mysql>
Give it a Try !!!
You mentioned in your question : The SELECT username AS a_username format will not help as I need to continue using the * field selection.
All you have to do to implement my suggestion is run the query using tableA as follows:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = DATABASE() and table_name = 'tableA'
order by ordinal_position) A;
When you retrieve that query result, just use it as the query to submit to mysql_query.
You'll need to list the columns, e.g
SELECT username AS a_username FROM tableA;
alternatively, post-process in back-end, e.g. change the array keys in your code
Create a view with renamed columns, e.g. -
CREATE VIEW a_view AS SELECT username AS a_username FROM table;
Then refer to this view.
As already mentioned, there is no standard way to mass-prefix column names in a regular query.
But if you really wanted to achieve it, you could write a stored procedure which would query information_schema to get a list of columns in a table, and then prefix them one by one. After that it's possible to concatenate a query as string, PREPARE and EXECUTE it.
A downside to this approach is the fact that you cannot join on a stored procedure's result. But of course, you could as well create a stored procedure for each type of query you issue. Prefixing the fields for any table could then be made a separate generic FUNCTION.
All of this stuff sounds to me like an overkill, though. I would recommend either renaming the actual columns, so that they are always prefixed, or just listing all the result fields with AS aliases, as Scibuff and Alister suggested.
I don't believe it can be done for all columns automatically, but you can list as many columns as you like with AS
SELECT id AS a_id,
name AS a_name,
email AS a_email /*, etc....*/
FROM tableA;
I've only inserted newlines for some extra clarity.