I have a spring boot application. And I configured my MySql Database table with 3 fields in the DAO Layer. After starting my application this table creating without problem. But after sometime it gets adding 2 more fields with underscore. Please find the below table.
CREATE TABLE `sample` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`contractId` varchar(20) NOT NULL UNIQUE,
`customerName` varchar(50),
PRIMARY KEY (`id`));
After sometime the table is having below fields.
id
contractId
customerName
contract_id
customer_name
Why last two columns are adding without doing anything?
Can anyone please help on this. Thanks
Try to delete these columns :
ALTER TABLE sample
DROP COLUMN contract_id;
DROP COLUMN contract_name;
Related
I have written an application in Javascript which inserts data into two tables via a connection to a MariaDB server.
There should be a 1:1 correspondance between the rows in these tables when first running the application.
One table stores (simulated) data about properties, the other table stores data about prices. There should be 1 price for each property. At a later date, the price might change, so there could be more than one entry for the price, but this cannot happen when the application is first run. These entries also cannot be in violation of a unique index - but they are.
Perhaps I have misconfigured something in MariaDB? Here is the code which generates the tables.
drop table if exists property_price;
drop table if exists property;
create table property
(
unique_id bigint unsigned not null auto_increment primary key,
web_id bigint unsigned not null,
url varchar(256),
street_address varchar(256),
address_country varchar(64),
property_type varchar(64),
num_bedrooms int,
num_bathrooms int,
created_datetime datetime not null,
modified_datetime datetime not null
);
create table property_price
(
property_unique_id bigint unsigned not null,
price_value decimal(19,2) not null,
price_currency varchar(64) not null,
price_qualifier varchar(64),
added_reduced_ind varchar(64),
added_reduced_date date,
created_datetime datetime not null
);
alter table property_price
add constraint fk_property_unique_id foreign key(property_unique_id)
references property(unique_id);
alter table property
add constraint ui_property_web_id
unique (web_id);
alter table property
add constraint ui_url
unique (url);
alter table property_price
add constraint ui_property_price
unique (property_unique_id, price_value, price_currency, price_qualifier, added_reduced_ind, added_reduced_date);
Below is a screenshot from DBeaver showing that a select statement returns two identical rows.
I don't understand why the unique constraint appears to be violated. The constraint does sometimes work, because if I run my application again, it fails because it attempts to insert a duplicate row which already exists in the DB. (Not the same as the one shown below.)
Can anyone point me in the right direction as to how I might debug this?
MariaDB permits multiple values on columns which form part of a unique constraint.
My solution would be to put the logic for checking for duplicate rows into the application, rather than this being on the database side. Essentially this means the unique constraint is not being used.
I need that the values for a ColumnID, which is a Primary Key, to start at 100 and increment by 5. This condition is asked to be included as a constraint before populating the tables. I already created the tables, I just need to add that constraint. I know I can't use AUTO_INCREMENT because the increase is only by 1. Is there a way to do it in MySQL?
MySQL does not provide any built-in function to create a sequence for a table's rows or columns. But we can generate it via SQL query.
Example:
Let us understand it with the help of the following example. First, we need to create a new table and make sure that there is one column with the AUTO_INCREMENT attribute and that too, as PRIMARY KEY.
Execute the below query to create a table:
CREATE TABLE Insects (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
Origin VARCHAR(30) NOT NULL
);
Then you can alter your column to start from another value:
ALTER TABLE Insects AUTO_INCREMENT=100;
You can check it here.
I have a main table called results. E.g.
CREATE TABLE results (
r_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
r_date DATE NOT NULL,
system_id INT NOT NULL,
FOREIGN KEY (system_id) REFERENCES systems(s_id) ON UPDATE CASCADE ON DELETE CASCADE
);
The systems table as:
CREATE TABLE systems (
s_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
system_name VARCHAR(50) NOT NULL UNIQUE
);
I'm writing a program in Python with MySQL connector. Is there a way to add data to the systems table and then auto assign the generated s_id to the results table?
I know I could INSERT into systems, then do another call to that table to see what the ID is for the s_name, to add to the results table but I thought there might be quirk in SQL that I'm not aware of to make life easier with less calls to the DB?
You could do what you describe in a trigger like this:
CREATE TRIGGER t AFTER INSERT ON systems
FOR EACH ROW
INSERT INTO results SET r_date = NOW(), system_id = NEW.s_id;
This is possible only because the columns of your results table are easy to fill in from the data the trigger has access to. The auto-increment fills itself in, and no additional columns need to be filled in. If you had more columns in the results table, this would be harder.
You should read more about triggers:
https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html
https://dev.mysql.com/doc/refman/8.0/en/triggers.html
I have a table which is given below and I need to its primary key as auto_increment and insert the value. I'm explaining my table structure below.
db_city:
city_id city_name date
I created the table using the below command.
CREATE TABLE spesh (city_id INT(11),city_name VARCHAR(200),date DATETIME);
But here I forget to add auto_increment. The above are the three fields of db_city table. Here I need to make city_id as auto_increment and its data type is int(11) now. I also need to insert the value using command line.
Please help me.
Try some thing like this:
CREATE TABLE animals (
id INT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
Or to add it using alter like:
ALTER TABLE animals CHANGE id id INT(11)AUTO_INCREMENT PRIMARY KEY;
NOTE: Auto Increment automatically fill its value if some records present in the table.
ALTER TABLE db_city MODIFY COLUMN city_id INT auto_increment
What is wrong with the following syntax? This is the code that MySQL work bench made and I can't tell what is wrong with it.
CREATE TABLE `data1`.`table1` ();
You are trying to create a table without columns. That is not possible in MySQL. You need to specify at least one column in order to create table.
You are missing columns, a table need columns
Propper syntax would be:
CREATE TABLE table1(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
);
Read more about this at w3school
A table is a collection of columns and rows. Without columns, it is not a table. 'An empty variable'- would be the best name.
Check the CREATE TABLE syntax here.