Prevent Duplicate Table entry in Database - mysql

I'm trying to Create new table for record of each company and its information. And for that I'm using simple Create query. Is there any way to prevent duplication of Table?

In every database that I know of, duplicate table names are not allowed. In some, there is a third element, the schema, but I assume everything is in the same schema.
In other words, trying to create a duplicate will cause an error. Try it.
By the way, you should always tag your questions with the database you are using.

Related

Add new columns without using Alter

Is it possible to add new columns to an existing table without using alter statement?
Other people are answering unequivocally "no, it is not possible." This is the answer to your literal question. But I'm wondering why you ask the question.
One of the biggest pain points of MySQL is that using ALTER TABLE locks the table while you're making a change like adding a column, and the more data in your table, the longer this lasts while it restructures the table. I'm guessing this is the issue you have, and you're trying to get an alternative that doesn't block access to the table while you're adding a new column.
(In the future, it would help folks give you the best answers if you explain more about what you're trying to do.)
The answer to this question is yes, there is a solution: pt-online-schema-change is a free tool that accomplishes this.
You use it just like you would use ALTER TABLE, but you use it at the command-line instead of in an SQL query.
pt-online-schema-change --alter "ADD COLUMN c1 INT" D=sakila,t=actor
In this example, the database name is sakila and the table name is actor. The script does a lot of work behind the scenes:
Create a table like the original table, but empty of rows
ALTER TABLE to add the column or whatever other alteration you told it. You can do anything you would normally do with ALTER TABLE. In fact, it's doing ALTER TABLE for you, against the empty copy table.
Copy rows from the original table to the new table in the background.
Create triggers to capture any changes made to the original table while it's gradually copying the bulk of the data.
Swap the names of the new table (with the extra column) and the original table, once all data has been copied.
Drop the original table.
This has a few caveats, like the original table must have a primary key, and must not have existing triggers.
It tends to take longer than doing a traditional ALTER TABLE, but since it's not blocking access to the original table, it's still more convenient.
Does this help?
Is it possible to add new columns to an existing table without using the alter statement?
No.
Is it possible to add new columns to an existing table without using alter statement?
I don't think it's impossible.
However I'm not sure what you want to do.
lets say you have a table
select * from Store
and you want just export the data or perhaps you want to do something with that data like a selection. but you don't want to STORE the data in your Database
you can just fill a value and give it a name
select
'Test' as name,
*
from Store
this will populate your column with the value your entered.
data results

Updating existing lines in MySql and treating Duplicated Keys

I have a MySql database containing data about users of an application. This application is in production already, however improvements are added every day. The last improvement I've made changed the way data is collected and inserted into the database.
Just to be clearer, my database is composed of 5 tables containing user data and 1 table to relate all the tables, through foreign keys. These 5 foreign keys, together, form my Unique Index for this "Main Table" I have.
The issue is that one of these tables containing user data changed its format, and I want to remove all the data older than the modification I made on my application (just from this table, the other ones I need to keep untouched). However, this dataset has foreign keys in the main table, and I can't just drop these lines on the main table because the other informations I have are important. I tried to change the value of the foreign key for this table, in specific, but then, obviously, I have a problem related to duplicated indexes.
Reading on internet, I've found a solution to my problem using "Insert ... On duplicate key update ...", but i'm not inserting data, just updating it. I have an Idea about how to make a program on PHP to update my database, but is there another easier solution? Is it possible to avoid these problems using just MySql syntax?
might be worth looking at the below link
http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html

Drop table in sql and add

I have some information that I am downloading via API into a SQL database. I am setting up cron to do this in the middle of the night. Sometimes new products are added or old ones are edited. I need to add new ones and update old ones if they exist. I am pretty sure it looks something like:
If (id exists){
update product
}else{
insert product
Is this the best way? What about just dropping then re-constructing it?
I would update the existing product, especially if the ID is an auto-number. But even if you have a surrogate key other than this ID, I'd still update existing products. In the future, your database may grow more complex and your products may get a couple of child tables. You don't want to reconstruct all of them.
Just update them.
You are looking for INSERT ... ON DUPLICATE UPDATE, i guess. See here.

MySQL find record through many table

In my case I have many table in my database.
My goal is to create a search engine where user can create all logical search he wants.
So I need to find a solution to generate all join based on user search critera.
In some case table has (1:n) links in other case (n:1).
One solution is to image all links and create all join, but I thinks it's a worse solution.
So if you have an idea, I'll very happy to read that.
Thanks a lot.
You can manage it like this don't know it is good or bad but a solution.
Create a new table containing all the searchable fields from various tables and reference to the record id to that table should also be stored in this table.
Insert the new record in this table whenever a new record inserted in those tables.
Search in this single table containing data from all other tables.
OR
consider to use VIEW

MySQL Inserting data and setting FK's?

I have a table like this in my database
http://i.stack.imgur.com/BsoS9.png
I have all my PK,FK relationships setup and im ready to start inserting data.
However I do not know where to start.
Do I insert into tables where my Primary Keys are first.
But then how do I give my Foreign keys the values of the primary key in the linking tables?
I thought with starting with:
-Patient
-Department
-Procedure
-Staff
-Events
-Supplies
Any reference material would be appreciated, I tried googling my question but not luck. Perhaps someone could eloquote it more accurately.
You thought correctly, start with the foremost "parent" table, and work your way down. Inserting the foreign keys can be done either with nested queries or simply getting the key, storing it, and reusing it.
Personally I'd go with nested queries.
Yes, you'll need to logically populate the parent records first. Obviously, you cannot insert a child record without having a parent existing first -- so long as you have true relationships between the two. In your case, just as your diagram suggests, you'll need to populate Events before you can populate Supplies. And then, what ever other logical ties you have.