how to change (strip) multiple column names using MySQL - mysql

I have a table with column names like so:
BLAH_Q1_NAME, BLAH_BLAH_Q2_AGE, BLAH_UGLY_BLAH_Q3_DATE_OF_BIRTH, etc.
I want to be able to change the column names to strip away all of the name before the "Q#" portion, such that I end up with column names that look like so:
Q1_NAME, Q2_AGE, Q3_DATE_OF_BIRTH, etc.
There are 40+ variables in this table, which prohibits me simply doing this manually. (I hope!!) Is there any way to do this in MySQL? Thanks!

There's no MySQL command to let you quickly rename 40 columns, but you can just grab a random scripting language to help you write a query to do that for you.
$ perl -le'print "alter table MyTable ". (join ", ", map { "change BLAH_Q${_}_NAME Q${_}_NAME INT" } 1..40)'
(truncated) output:
alter table MyTable change BLAH_Q1_NAME Q1_NAME INT, change BLAH_Q2_NAME Q2_NAME INT, change BLAH_Q3_NAME Q3_NAME INT, change BLAH_Q4_NAME Q4_NAME INT, change BLAH_Q5_NAME Q5_NAME INT

Related

Select a specific part of a value from a column in mariadb

i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';

MySQL issue with CREATE TABLE

I've got this rare issue.. if I use this
CREATE TABLE 'my-test-table' (name TEXT, date TEXT)
I get a syntax error.. but if I just use ` insead of ' it works
CREATE TABLE `my-test-table` (name TEXT, date TEXT)
Any clues why? thanks
EDIT:
Oh okay, so the correct sintax for creating tables is using `
CREATE TABLE `my-test-table` (name TEXT, date TEXT)
And for updating using '
UPDATE `my-test-table` SET name = 'MyName'
The single quotation is used for defining strings. That is why.
It's not an issue. It's just, well, syntax. Only because this symbol ' looks similar to this ` that doesn't mean they are the same token. They are different symbols with different functions.
use
CREATE TABLE my-test-table (name TEXT, date TEXT);
and table updating
UPDATE my-test-table SET name = 'MyName' where yourcolum='field what you want';

Updating a column in mysql by added to the middle of the string?

I currently have a column (control_reference) setup as a varchar(120) with the data that looks something like this 15591_8571. The data in this column changes the first number but the second number is always the same. I would like to add something to the middle of this so the varchar would look something like this in the end. Remember I have hundreds of these so I wanted to find a way to change them all.
Current values in control_reference
15591_8571
16772_8571
20541_8571
New values in control_reference
15591_I18n_8571
16772_I18n_8571
20541_I18n_8571
I know I can use a simple UPDATE 'database.table' SET 'control_reference'='15591_I18n_8571' WHERE'id'='some_number'; but I have a lot of them to do.
If all rows in the table need to be updated, try:
UPDATE database.table SET control_reference=REPLACE(control_reference, '_', '_I18n_')
To limit the update to rows that match that pattern, add something like:
WHERE control_reference REGEXP '[[:digit:]]+_[[:digit:]]+'
Create a function or stored procedure with REPLACE command and use it.
Simple Usage of REPLACE sysntax
SELECT REPLACE('15591_8571', '_', '_I18n_') AS NewValue;
Result :
15591_I18n_8571

MySQL automatic conversion on lowercase

I have multiple web services that write data inside a database table.
I'd like to automatic convert uppercase strings into lowercase ones, for a specific field.
Is there any mysql function that performs this task?
Suppose this is the table:
id | name | language
Sometimes, inside the language field, web services write an uppercase string (IT).
I want to convert it into a lowercase string ("it"), directly inside MySQL.
thanks
Define triggers on the table:
CREATE TRIGGER lcase_insert BEFORE INSERT ON my_table FOR EACH ROW
SET NEW.language = LOWER(NEW.language);
CREATE TRIGGER lcase_update BEFORE UPDATE ON my_table FOR EACH ROW
SET NEW.language = LOWER(NEW.language);
Then update the existing data:
UPDATE my_table SET language = LOWER(language);
you could use:
LOWER("some_string");
See: LOWER
Did you mean something like this..
Added:
You could convert into lowercase while inserting the data in table, like:
INSERT INTO your_table (name, language)
VALUES ( "Some Name", LOWER( "SOME VALUE" ) );
Or a better solution would be converting the value to be inserted to language field to lowercase from your server side script before inserting to db.
You could do the automatic conversion thing using triggers though, but i suggest using LOWER() function during fetching your data from table, like:
SELECT LOWER(language) AS language FROM your_table_name

Displaying two fields in lookup

Introduction first, question at the end. Please read carefully!
I have a master-detail relation between two tables:
CREATE TABLE [dbo].[LookupAttributes] (
[Id] int IDENTITY (1, 1) NOT NULL,
[Name] nvarchar (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ;
ALTER TABLE [dbo].[LookupAttributes] ADD CONSTRAINT [PK_LookupAttributes] PRIMARY KEY ([Identity]) ;
CREATE TABLE [dbo].[Lookup] (
[Id] int IDENTITY (1, 1) NOT NULL,
[LookupAttributesLink] int NOT NULL,
[Code] nvarchar (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Value] nvarchar (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ;
ALTER TABLE [dbo].[Lookup] ADD CONSTRAINT [IX_Lookup] UNIQUE ([LookupAttributenLink], [Code]) ;
(There are more fields and indices in both tables but these are the ones that matter...)
The project I'm working on is meant to maintain data in 50+ tables, and every week this data is exported to XML to be used by some desktop application as source data. While I wanted to make this a pretty-looking application, it just needed to be done fast, thus I use a Dynamic Data Site so the data can be maintained. It works just fine, except for this table...
As it turns out, there are 600 different lookup records that share the same code, but different attributes. The DDS displays attribute and code correctly in the list of lookup records so there never was any confusion about which lookup record someone was editing. And this has been in use for over 2 years now.
Now the problem: A new table "Lookup-Override" has been added which links to the [Id] field of the Lookup table. Each record in this new table thus displays the [Code] field, but since [Code] isn't unique, it's unclear which Override record belongs to which Lookup record.
To solve this, I need to display more information from the Lookup record. Since the only unique set of fields is the attribute plus code, I need to display both. But displaying [LookupAttributesLink]+[Code] isn't an option either, since [LookupAttributesLink] is just a number.I need the DDS to display [Attributes].[LookupAttributesLink]+[Lookup].[Code] in a single column. Question is: how?I've considered adding a calculated field to the Lookup table, but I cannot get the attribute name that way.I could create a special page to maintain this table but I don't like that solution either, since it "breaks" the DDS principle in my opinion. I'm trying to avoid such pages.So, any other possibilities to get the site display both attribute name and lookup code in the override table?
The most interesting solution would be by using a calculated field which could retrieve the attribute name. How to do that?
Solved it myself! See answer below, which works just fine.
Found it! I had to do a few things:
CREATE FUNCTION LookupName (
#Attr int,
#Code nvarchar(255)
) RETURNS nvarchar(1000)
AS
BEGIN
DECLARE #Name nvarchar(1000)
SELECT #Name = Name
FROM [dbo].[LookupAttributes]
WHERE [Id]=#Attr;
RETURN #Name + '/' + #Code;
END
GO
ALTER TABLE [dbo].[lookup] ADD [Name] AS [dbo].[LookupName]([LookupAttributesLink], [Code])
GO
This will add an additional calculated field to the table which uses a function to calculate the proper name. I then had to add some metadata for the lookup table:
[MetadataType(typeof(LookupMetadata))]
public partial class Lookup { }
[DisplayColumn("Name", "Name")]
[DisplayName("Lookup")]
public class LookupMetadata
{
[ScaffoldColumn(false)]
public int Id;
[ScaffoldColumn(false)]
public object Name;
}
This will hide the Name column from the Lookup table itself, but it makes it visible for the Override table. (And it will be used to display the proper value.
Done this, solved the problem! Quite easy, actually. :-)