best structure of my table in Perfomance way: [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have two different database schemas
First Structure is :
table 1 : country_master
Column name: id(primary key) country_name
table 2: State_master
Column name: id(primary key) State_name Country_id(foreign key of country_master)
table 3: City_Master
Column name: id(primary key) city_name State_id(foreign key of State_master)
Second Structure is:
table 1 : Master_table
Column name: id(primary key) name
table 2 : Sub_master
Column name: id(primary key) name master_id(Foreign key of master Table) subid(id of Sub_master)
Record of first structure
table: Country_master
1 india
2 UK
3 USA
table:State_master
1 gujarat 1
2 MP 1
3 Up 1
table:City_master
1 ahmedabad 1
2 surat 1
Record of structure 2:
table:master_table
1 Country
2 City
3 State
table:master_table
1 india 1 0
2 UK 1 0
3 USA 1 0
4 Gujarat 3 1
5 MP 3 1
6 Up 3 1
7 ahmedabad 2 4
8 surat 2 4
now my question is what is best schema of my tables for performance:

The second structure gonna be create more issue regarding performance point of view.
As the child table have all references, it is difficult to maintain. Consider the scenario, where you need to mark for the "capital" of each state having the crores of data, then what would you do to add one more column into existing table and update each of one? No not obviously, instead the better job is to separate all the cities, states and countries in a separate table and use in query having joins, so join performes on primary key. And the most important is, these data not gonna be change often means these remains constant most of the times. These are called lookups, hence you need to separate in a table. The first structure is good in a first appearance because dataset is too small, as data increases, that gonna create problems, hence the better is to use the first structure.

You could probably achieve the same performance by adding the right indices on the tables.
For ex: if one if your queries is search for all states in a country you would need to create an index on the master and sub id of the sub table in the second structure.

I think your second table structure is not complete.here parentid can be non-clustered index,type can be non-clustered index.table variable is just example to show
Declare #t table (id int identity(1,1) primary key,names varchar(100)
,parentid int,[type] int)
--where [type] can be enum 1=country,2=states,3 city
insert into #t values ('india',null,1) ,('gujrat',1,2)
,('surat',2,3) ,('Bihar',1,2),('Pakistan',null,1)
;with CTE as
(
select * from #t where id=1
union all
select a.* from #t a inner join cte b on a.parentid=b.id
)
select * from cte
IMHO,Advantage is that if city is not mandatory.then in first table
structure you have to use left join. In second example whatever be the
requirement you can always use Inner join.Hence second structure is
fast.Also second is more flexible.Just one proc will do
.
Though let other comment .

Related

How do I change the value of one field in SQL record to a value from another table?

First of all, I'm sorry if this is a bit of a dumb question. I checked what's written in similarly phrased questions like "How do I update fields from one table with values from another table" but the content doesn't seem to match what I'm trying to do.
Let's say I have a table called site_users:
user_id
login
password
user_id2
2
user
password
1
7
access
xyz
2
11
otherlogin
abc
3
15
somebody
defg
4
22
user
qwert
5
Then I have a lot of other tables in the same database, that have some columns of various names that are actually corespondent to the "user_id" of the "site_users" table. There are no relations set or anything like that. I want to change the values in the fields of those other tables to user_id2. So let's say I have a table: user_options:
admin_id
perms1
perms2
2
1
12139389
7
1
13232111
I want to change it to:
admin_id
perms1
perms2
1
1
12139389
2
1
13232111
How can I do that? This is the first time I'm doing anything other than just simple mass changes of text with some regex :/
If i am understanding your question correctly you should be able to do following where table1 is top table and table2 is table you are trying to update:
update table2 t set admin_id = (select user_id2 from table1 where user_id = t.admin_id)

SQL - Normalizing a table containing multiple choices

I trying to create a database that includes a table, which will have the answers to a multiple choice quiz..
My problem is that, is it normal to create a column for every question?
I mean like if I have 100 questions do I create a column for every one of them?
What is the best approach for this?
Thank you
You can refer this link
Table: User
user_id auto integer
regtime datetime
username varchar
useremail varchar
userpass varchar
Table: Questions
question_id auto integer
question varchar
is_active enum(0,1)
Table: Question_choices
choice_id auto integer
question_id integer
is_correct_choice enum(0,1)
choice varchar
Table: User_question_answer
user_id integer
question_id integer
choice_id integer
is_correct enum(0,1)
answer_time datetime
https://www.quora.com/What-is-a-good-database-schema-using-MySQL-for-storing-multiple-choice-questions
It's normal (both in the ordinary sense and in the database-normalizing sense) to create a row for every question. And in another table create a row for every answer.
Read about database normalization.
Your question table might have columns with values like this
question_id question_text
1 What color is the sky?
2 What number comes after 1?
and your answer table might have columns with values like this
answer_id question_id correct answer_text
1 1 0 Green
2 1 1 Blue
3 1 0 Orange
4 2 0 -2
5 2 1 2
6 2 0 42
7 2 0 π
With a design such as this you can add as many questions and answers as you require without changing the structure of your tables.
You can retrieve the correct answer, for example, to every question like this
SELECT question_text, answer_text
FROM question
JOIN answer ON question.question_id = answer.question_id
WHERE answer.correct = 1

Count and sum up all duplicate records in MySQL

I have table with, following structure.
id name
1 john
2 ana
3 john
4 ana
5 peter
6 ana
7 Abrar
8 Raju
Duplicate entries in the table are as follows
john(2) duplicate
ana(3) duplicate
The names which are duplicates are john and ana.
My question is how would I count the records in total which are duplicate here it is '5' records
Note : I also followed the similar question in community but it explains how we can add the number of duplicates exists for that particular name in the table and adds up the third column in table representing the duplicates records with same name but in my case I wanted to know the number of all duplicates exist in the table (here the result of the query is just number "5") irrespective of the names.
Just take a count subquery on the query you already have in mind (or perhaps have already written):
SELECT SUM(cnt) AS total_duplicates
FROM
(
SELECT COUNT(*) AS cnt
FROM yourTable
GROUP BY name
HAVING COUNT(*) > 1
) t;
Demo

Insert date from two tables in MySql

Consider the following two table
Table A:
id int auto_increment
with 2 rows of data.
id
1
2
Table B:
id auto_increment
aid reference to A.id
with 3 rows of data
id aid
1 1
2 2
3 2
If now the table A has been inserted 2 rows to
Table A:
id
1
2
3
4
So, how to write the insert statement so that it can insert to table B as result
Table B:
id aid
1 1
2 2
3 2
4 3
5 4
6 4
7 5
8 6
9 6
The question is for study only. I know there are many ways to do it, but I am just wondering if it can be done by using sql or just in mysql?
UPDATE
Sorry, the question since to be unclear. Let me state it in clear.
The data in table B has relation to table A. B.aid = A.id
The new data in table A, which is A.id, are in sequence, also has relation to the first two id. That means 1 and 3 with the same meaning, 2 and 4 also.
In the insertion of table B, it should consider both 1. and 2. That means, since with one aid=1 and two aid=2, then the data that needs to insert into table B is one aid=3, two aid=4, one aid=5 and two aid=6.
The question: This can be done easily with programming, however, I just wondering can 3. be done in a insert statement with out programming in Mysql?
Assuming you are inserting the ID(s) into Table A,
INSERT INTO [Table B](aid) VALUES ([THE ID])
Where this will insert the specified ID, in the aid column in Table B.
Otherwise if you want to match up the values you can use NOT IN to detect values that are not like, then you must insert these values,
This will select all IDs that do not exist in Table B:
SELECT id
FROM [Table A]
WHERE [Table A].id Not In (SELECT aid FROM [Table B])
...then later (depending what you're using it in, PHP? Then fletch the data (should look something like this))...
while($row = mysqli_fetch_assoc(...))
{
INSERT INTO [Table B](aid) VALUES($row['aid'])
}

Consolidating data from multiple fields to a single field [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
I have 2 tables with 1 to many relationship between them.
I need to consolidate data contained in 1 column in the 2nd table and add it to corresponding single entry in the first table.
How could I do that in MySQL ?
ID is the primary key
Example:
Table 2 Calculation
ID Forms Calc
1 A 20
1 B 30
1 C 10
Target Table :
Table1 Client
ID Name Forms
1 XYZ A,B,C
INSERT INTO Client (ID, Forms)
SELECT ID , GROUP_CONCAT(Forms)
FROM Calculation
GROUP BY ID