I'm very new to databases, so apologies if this is an incredibly stupid quesiton. I was sent the schema of a mysql table in the body of an email. It looks like this, except it has many more rows:
+-------------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+---------------+------+-----+---------+-------+
| _id | varchar(40) | NO | FOO | NULL | |
| foo | text | YES | | NULL | |
| bar | text | YES | | NULL | |
| baz | text | YES | | NULL | |
| qux | datetime | YES | | NULL | |
+-------------------------+---------------+------+-----+---------+-------+
I need to create the table locally before reading in the actual data, which is in an accompanying tsv file. I'd rather not create the table schema manually, as it is a lot of columns. Is there a way to do this automatically using the schema that I was sent?
There is no way to automatically convert an ASCII-art table of that form to an SQL CREATE statement.
If you are bored and inclined to do so, I suppose you could write a utility to parse that input and generate an output.
You will either have to create the table with a query (see CREATE), or use an administrative front-end such as phpMyAdmin to enter the information in a more human-friendly manner (I believe phpMyAdmin also now has a graphical designer feature).
Some other DBMS's have table generation tools built in to their front-ends too, e.g. MS SQL Server Management Studio has a nice graphical table builder.
By the way, to pick nits, you mean to say it is a lot of columns, not rows.
Edit: Just for fun, here is a quick Linux shell script that converts most of the informational portion of your table to an SQL statement (command line parameter is input file name):
#!/bin/sh
echo "CREATE TABLE myTable ("
cat "$1" | awk '{ split($0,a,"|"); print a[2] a[3] (match(a[4],"NO")?"NOT NULL":"") " DEFAULT" a[6] ","; }' | tr -s " "
echo ");"
The following input file:
| _id | varchar(40) | NO | FOO | NULL | |
| foo | text | YES | | NULL | |
| bar | text | YES | | NULL | |
| baz | text | YES | | NULL | |
| qux | datetime | YES | | NULL | |
Generates the following output:
CREATE TABLE myTable (
_id varchar(40) NOT NULL DEFAULT NULL ,
foo text DEFAULT NULL ,
bar text DEFAULT NULL ,
baz text DEFAULT NULL ,
qux datetime DEFAULT NULL ,
);
But you still have to remove the foo line manually and replace _id with foo (and the NOT NULL DEFAULT NULL is odd, I don't actually know of MySQL will accept a null default on a not null column). It doesn't care about your KEY column. You also have to manually delete that last comma. It does do most of the work, though.
You could make a start with a good text editor by running through your schema with intelligent replace operations. But in the end it will remain a mainly manual process.
Related
Working on mysql.5.7
Here is my bugs table
MySQL [jira_statistics]> describe bugs;
+---------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------------+--------------+------+-----+---------+-------+
| issue_key | varchar(45) | NO | PRI | NULL | |
| release_name | varchar(45) | YES | MUL | NULL | |
| issue_summary | varchar(200) | YES | | NULL | |
| story_points | int(11) | NO | | 0 | |
| qa_reopened | float | NO | | 0 | |
| done_reopened | float | NO | | 0 | |
This table is updated by periodic calls to LOAD DATA LOCAL INFILE bugs <file.csv>
Whenever this update takes place (which may either update existing lines and/or insert new ones) I want another table that has some yielded statistics to be updated via the following trigger
create trigger update_bugs_stats after insert on `jira_statistics`.`bugs` for each row
begin
delimiter ;
-- STORY POINTS -------------------------
SELECT AVG(story_points) INTO #avg_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MAX(story_points) INTO #max_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MIN(story_points) INTO #min_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
INSERT INTO storypoints_stats (release_name, avg_bugs_storypoints, max_bugs_storypoints, min_bugs_storypoints)
VALUES (relName, #avg_bugs_storypoints, #max_bugs_storypoints, #min_bugs_storypoints)
ON DUPLICATE KEY UPDATE
relName=new.release_name,
avg_bugs_storypoints=#avg_bugs_storypoints,
max_bugs_storypoints=#max_bugs_storypoints,
min_bugs_storypoints=#min_bugs_storypoints;
However this gives me the following error whenever trying to create the trigger:
Unknown column new.release_name in where clause.
Why isn't the new keyword bein recognized?
Because new is reserved as a system word
Ref: https://dev.mysql.com/doc/refman/8.0/en/keywords.html
Please modify
new.release_name ==> `new`.`release_name`
etc..
Τhe error was more stupid than I thought;
I was working directly on sql query editor and not on the triggers tab of mysql workbench so it did not parse correctly the new keyword`.
I'm trying to load different data, from different files, into multiple columns in MySQL. I'm not a big database guy, so maybe I have my data structured wrong. :)
Here's how I have it set up:
DATABASE: mydb
TABLE: aixserver1
COLUMNS: os, hostname, num_users, num_groups, pkg_epoch
shown from mysql:
+---------------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cur_timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| pkg_epoch | int(11) | NO | | NULL | |
| os | char(5) | YES | | NULL | |
| hostname | char(40) | YES | | NULL | |
| num_users | int(10) | YES | | NULL | |
| num_groups | int(10) | YES | | NULL | |
+---------------+-----------+------+-----+-------------------+----------------+
So basically I want to populate pkg_epoch, os, hostname, num_users and num_groups into the database. The data I want to load is inside 5 flat files on the server. I'm using ruby to load the data.
My question is how do I load all these values from those files into my table at once. If I do my inserts one at a time, then the other records become NULL. I.E, I load data into just the hostname column, and all the other columns become NULL for that row.
What am I missing? :)
You can do this a couple ways but the trick is to use a variable placeholder. Here is an example if you used the database's LOAD DATA function:
LOAD DATA INFILE '/PATH/TO/FILE' IGNORE INTO TABLE tableName FIELDS TERMINATED BY '\t' LINES
TERMINATED BY '\r' (#skip, #skip, #skip, login_name, pwd, #skip, #skip, #skip, #skip, #skip, first_name, last_name);
You see I just set a variable #skip or #anything for the fields I don't want to include in the database and name the columns that I do want.
I can get you halfway there with this but am uncertain best approach if you build your own loader with Ruby. I would suggest you retrieve the file and let MySQL import using LOAD DATA as it'll be very performant and you can use trick above.
I have an update query that shouldbe working but for some reason it doesnt work
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI SET YEAR='year1', MONTH='month1', DAY='day1', MJD='mjd1', WHERE (EOPID=1)";
It gives me the following error
Incorrect integer value 'year1' for column YEAR at row1
my table consist of the following columns and their types
| EOPID | int(11) | NO | PRI | NULL | auto_increment |
| YEAR | int(11) | YES | | NULL | |
| MONTH | int(11) | YES | | NULL | |
| DAY | int(11) | YES | | NULL | |
| MJD | int(11) | YES | | NULL | |
I retrieve the valuues to use in my sql update query from a jTable in the following manner
Object year=model.getValueAt(row, column);
years=year.toString();
year1=Integer.parseInt(years);
so i believe i am using the correct type but i cant figure out why it wont update . Is this a mysql version thing?
Your query should be like.
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI
SET
YEAR="+year1+",
MONTH="+month1+",
DAY="+day1+",
MJD="+mjd1+"
WHERE
EOPID=1";
Where year1, month1, day1, mjd1 should be variables containing appropriate values (there is an extra comm before the WHERE clause though).
The system is complaining that you're giving it a STRING ("year1"), not the integer value (e.g. 2012) it's expecting.
You should write this more like:
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI SET YEAR=" +
year1.toString() + ", month..."
I have a tableA with following output from desc tableA command:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | | |
| city | varchar(50) | YES | | NULL | |
| state | char(2) | YES | | NULL | |
| country | varchar(30) | YES | | NULL | |
| notes | longtext | YES | | NULL | |
| type | varchar(50) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Now there are 3 columns with NOT NULL constraints:
id
name
type
For columns id and type, I need to remove the default constraint. Basically I want Default: None. I do not want to use the workarounds eg . setting default to '' for a varchar.
The difference between NULL, NONE, and '' is made more clear from this discussion Default-values-for-varchar-and-int-mysql-data-types
I tried using the command:
alter table tableA alter column type drop default;
The query runs fine, but no rows are affected. And no change in Default value is shown when I run describe command.
If I set the default value to '' I run into different issue - the database allows the entry of empty string in the db. For me that is equivalent to inserting NULL for a column's value, and I do not want to allow that.
I need some guidance on how to handle Default values in this situation where I cannot allow empty strings as data in the db. I want to mention that I am planning to put validations in the code to check if the incoming data is an empty string or NULL. But just in case that validation is not working etc, I want to make sure the DB can refuse to add such data.
Any help is really appreciated.
If the column can be then null, then either default or null are the same.
So Allowed Null, Default null is effectively irrelevant except when doing say
Insert (name,city,type) Values ('Fred',DEFAULT,'Caucasian')
Null isn't an empty string. Given you are allowing null in the table but interpreting it as empty string in your application, you have an irritating flaw in your design.
If you don't want empty strings in there, normally you'd use a check constraint which as far as I know still isn't implemented in mysql. Apparently this lack is usually solved with an insert trigger.
So you'd check the value in the trigger and then fail the insert for empty strings.
PS it doesn't solve the integrity problem, but if you did want a way to put nulls in when a straing was empty so you would not have to distinguish between empty string and null.
Then have a look at the nullif function.
I have this project I am working on, I have a table schema, see below
+--------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+----------------+
| codeId | int(15) | NO | PRI | NULL | auto_increment |
| code | varchar(9) | YES | | NULL | |
| status | varchar(5) | YES | | 0 | |
+--------+------------+------+-----+---------+----------------+
This table is used for authorizations of codes, however some people send codes like dsfffMUBBDG345qwewqe for authorization, please note the capitalized part. In the code column there is a code MUBBDG345. I need to be able to check from the table if any combination of 9 characters the codes sent matches any of the codes in the db.
I have tried using this query but i just does not work.
select code, codeId, status from authCodes where 'dsfffMUBBDG345qwewqe' like code;
Is this even possible with a mysql query only?
you want to use
SELECT code, codeId, status
FROM authCodes
WHERE 'dsfffMUBBDG345qwewqe' LIKE CONCAT('%', code, '%')