I have three tables-
Class (parent table)
Announcement (child table)
Class-ann(junction table)
All have their respective autoincrement ids
I am.looking to insert a new announcement into announcement table and assign it to a class in class-ann table
Please help me in that.
I can easily retrieve table using join. Should i use a join and then insert? Also classID will be predefined
I don't see any reason to use a join unless you're going to be using that data somewhere else. Just do an insert into the Class-ann table using the class_id and announcement_id from your other tables.
Related
I want to use multiple database because I have 1 database which contain 40 tables and it doesnt seem good.Thats why I want to create another database but problem is I am not able to join this databases at one query for example
I have 'core' database and it contains users table which has user_id column
second database is post database and I need get user_id from users table which is in 'core' database like below
SELECT post_message from posts where 'core'.user_id=123
is that possible?or should I stick with one database?I have also foreign key problem which is related with database relation.
If it is SQL SERVER Product, You can call the Object residing on another DB like below
SELECT * FROM [DB_NAME].[Schema_Name].[Table_NAME]
For Example:
SELECT * FROM DB1.dbo.Employee
For MySQL : Please go through the below ans
Select columns across different databases
Okay so im new to databases, and have created a site with a users table, and i also hace a list table, where suers can insert list items, however when they log in everyones list is appearing, how can i link the user table to the lists table, is it creating the same field in each one and using a foreign key? Sorry I am very new to this. Appreciate any help
I think you can just use user_id on both tables to fix this. Let me give an example:
Table A (user_id, username, password)
Table B (list_item_id, user_id , any_other_attribute)
When you design your tables like this a simple sql call will do what you need like:
SELECT 'list_item_id','any_other_attribute' FROM Table B Where user_id=$user_id
Where $user_id is the user_id of the one's who loginned your system.
Also by your question, i suggest you to read about these : 'sessions' , 'sql queries' , 'generating sql query results' on your choice of programming language.
It calls MANY MANY relationnship. There mus be 1 table with fields user_id and field_id that will join this 2 tables
I have this code to delete data from multiple tables in one go:
DB::table('tb_stikes_register_school')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_guardian')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_student')->where('register_id', $_POST['id'])->delete();
I'm trying to shorten this into 1 query only, register_id from guardian and school tables is the foreign key of student table. I've been trying to use join but only student table record is deleted. Is there any workaround this?
Something like this maybe - haven't tested
DB::table(DB::raw('FROM tb_stikes_register_school, tb_stikes_register_guardian, tb_stikes_register_student'))
->join(ENTER JOIN INFO) // wasn't clear how your tables were related
->where('register_id', $_POST['id'])
->delete();
Or you could use a fully raw query:
DB::query('SQL statement here');
Basically recreating something similar to this: delete rows from multiple tables
I have one column called 'speeding' in a table containing many other columns. This column contains an integer that is foreign key to an entry on a table named Speeding. This table has columns 1-25 and an id that is referenced by the 'speeding' column from the first table.
Besides using join, is there any setting I can set on 'speeding' to make it automatically pull the associated data from the table Speeding?
You could create a view, a view is basically a SQL statement that is stored on the MySQL server and acts like a table
CREATE VIEW ViewName AS
SELECT tbl1.data, tbl2.speeding
FROM tbl1
INNER JOIN tbl2 ON tbl2.key = tbl1.key;
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
You then use the view as you would use any table
SELECT data, speeding
FROM ViewName
I'm trying to create a mysql table from the inner join between two other tables. I'm dealing with a database someone creates which has the following tables:
sitematrix_sites
sitematrix_databases
They are related by another table (I don't know why don't use a foreign key) called sitematrix_sites_databases which has the following fields:
site_id and database_id.
That's how the two tables relate. Now I'm trying to remove that to make my life easier, so I have:
mysql> CREATE TABLE result AS(select * from sitematrix_databases INNER JOIN site
matrix_site_databases ON sitematrix_site_databases.database_id = sitematrix_data
bases.database_id);
ERROR 1060 (42S21): Duplicate column name 'database_id'
However, I'm getting that error. Does someone know how can I merge the two tables without repeating the database_id field?
Thanks
Remove the * in your SELECT statement and actually list out the columns you want in your new table. For columns that appear in both original tables, name the table as well (e.g. sitematrix_databases.database_id).
Don't use * instead name each column and use aliases. For instance instead of sitematrix_database.database_id you can have alternativeName. Also you can pick and choose which columns you want this way as well.
In SQL Server, you can use "select into". This might be equivalent syntax for mySql:
http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
Unfortunately, it's a two commands (not just one):
http://www.tech-recipes.com/rx/1487/copy-an-existing-mysql-table-to-a-new-table/
CREATE TABLE recipes_new LIKE production.recipes; INSERT recipes_new SELECT * FROM production.recipes;
Instead of using SELECT * ... try SELECT database_id ...
MySQL does not like joining tables that have the same column name.