Make tables related - mysql

I'm trying to design a database for my website but i have a little problem with making related tables .
I have two tables . Table1 and Table2
Table1 is used to store user`s information and Table2 is used to store their activation codes
To relate them i need to store each user's ID in Table1 , inside Each record of Table2 so that i can get my user`s activation code
And as i know it is how relational databases work.
My problem is when using php to run mysql commands and add new user information in Table1 , How can i find What ID Number will be assigned to my user so that i can save both Id and activation code in my other table ?

If you use PDO, you can use the function lastInsertId() to retrieve the last id inserted in your last query.
http://php.net/manual/en/pdo.lastinsertid.php
If you use Mysqli, you have the mysqli_insert_id() function .
http://php.net/manual/en/mysqli.insert-id.php

Related

SQL: Help creating a table based on another tables value

I am new to SQL and am currently doing a project on MSQL 2019.
I have one table with a list of codes mapping a "Loss ID" to a Reason.
Now I have another table that just gives you the number of the Loss ID but not the text version of it.
How can I create a table that will take the number code and automatically change it to the text version of it?
Im not even sure what this type of scripting is called, so even pointers on what to google or look for would be very useful.
Rather than creating another table use a view.
Let's call your tables tbl1 and tbl2. The view would be:
CREATE VIEW my_view AS SELECT tbl2.your_date_column,
tbl2.LossID,
tbl1.reason
FROM tbl1
INNER JOIN tbl2 on tbl1.LossID=tbl2.LossID;
Lear more about VIEWS https://www.mysqltutorial.org/create-sql-views-mysql.aspx

MySQL Command what does a point mean?

I'm a newbie in mysql and have to write a implemention for a custom mysql asp.net identity storage.
I follow this tutorial and the first steps are done.
https://learn.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider
Now i have the follow mysql command:
"Select Roles.Name from UserRoles, Roles where UserRoles.UserId = #userId and UserRoles.RoleId = Roles.Id"
My problem is now that i dont know how the table have to look for this request?
I would say:
Tablename : Roles
Select: Roles and Name? or is it a name?
same with UserRoles.UserID and UserRoles.RoleId
What does the point mean?
Thanks a lot
You question is quite unclear, however, if I understood correctly, you can't figure out clearly how the database schema you are using is structured and what you'll get from this query.
The query you have written SELECTs the data field called Name from the table called Roles. In order to do this, the query uses data coming from two tables: one is the Roles table itself, the other is called UserRoles.
It will extract Names data from the Roles table only for the Roles entries that have the Id field matching with the RoleId field of the entries in the UserRoles table that have the UserId equal to the given #UserId.
In other words, this SELECT query in will give you as a result a list of Names coming from the entries in the Roles table which match the given conditional check, which is what is written after the where SQL condition: where UserRoles.UserId = #userId and UserRoles.RoleId = Roles.Id.
Finally, the point "." in SQL queries is used to disambiguate between fields (or columns, if you want to call it so) with same name but coming from different tables. It is quite common that all the tables have an Id field, for example. You can identify the correct Id field in your database by writing Table1.Id, Table2.Id, and so on. Even if you don't have naming conflicts in your tables columns, adding the table name can be very good for code readability.
Edit:
As other users correctly pointed out in the comments to your question, you should also have a look to what an SQL JOIN operation is. Since you are searching data using information coming from different tables, you are actually doing an implicit JOIN on those tables.

Connect Tables in MySQL

I'm attempting to connect two tables in MySQL so that when selected, a value is pulled from the table.
Table 1
id
mobile_car
Table 2
id
mobile_car
car_domain
When the mobile_car is selected via a dropdown and the user registers this, I would like to be able to have it connect to the car_domain so I can call this later in the application.
For example, if the user selects "Verizon" as their mobile_car, I would like it to link to the car_domain "#vtext.com" so I can connect their phone number with the car_domain. Essentially, I'm sending a text by way of emailing their cell number.
I know there are programs and pre-built options, but I'd like an understanding of how this could be done.
Thanks!
Try joining your two tables together:
SELECT t1.mobile_car, t2.car_domain
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.mobile_car = t2.mobile_car
I'm also assuming that your actual table structure will be more complex than this, since right now you can actually just query Table 2 by itself to get what you need.

How to change the values of two table automatically (MySQL)?

I have a database with two tables. The first one contains the user_name, user_password, user_email. The second one contains the user_name, user_age, user_description.
When a person finds the user he needs by the user_name, the script looks through the database using the user_name, to give out the information about certain user.
But if the person changes his user_name via preferences, the value changes only in the first table.
Question:
1) Is there a way to make the user_name in the second table change automatically? (To connect them some how)
I am using MySQL (phpMyAdmin).
This is just a simple example. In "real world" I am trying to manage more serious applications that have more tables. Is there an easier way than to create a separate php query for each table?
You could always create an AFTER UPDATE MySQL trigger targeting single rows for this. See the manual. It's probably not easier than using separate PHP queries for the tables, though. You don't need to spell them all out, just map up what needs to be synchronized when, and abstract your code.
However I'd recommend that you use a unique ID field for the user and only store the username in one of the tables -- and refer to the user with the ID under the hood of your code, and in both tables. Not a good idea to use something changeable as a unique identifier in your database design.

Using Sphinx for the first time - configuring the sql_query key

I'm currently practicing using Sphinx, I've not far off done much, except the configuration what I'm trying to do. The sql_query key is leaving me somewhat confused what to put there, I read in the Sphinx documentation of sql_query but it doesn't seem to clear my mind from knowing what to do since I have many SELECTs in my web application, and I want to use Sphinx for my search and the SQL is often changed (upon user search filtering).
As of my search using MySQL, I want to integrate Sphinx to my web application, if the sql_key is not optional, do I have to expect to put the whole search SQL query into that field or do I pick out the necessary fields from tables to start a reindex?
Can someone point me to the right direction so I can get things going well with Sphinx and my web application.
sql_query is mandatory , it's run by sphinx to get the data you want to be indexed from mysql . You can have joins , conditions etc. , must be a valid sql query . You should have something like "SELECT id ,field1,field2,fieldx from table" . id must be a primary id .Each row returned by this query is considered a document ( which is returned by sphinx when you search ) .
If you have multiple tables ( that are very different by meaning - users , articles etc.) - you need to create an index for each .
Read tutorials from here : http://sphinxsearch.com/info/articles/ to understand how sphinx works .
You can create a sql query to get union set of records from the Database. If you do multiple table joining and query to select the best result set, you can do it with Sphinx too.
You may run into a few trouble with your existing table structure in the database.
Like :
Base table does not have integer primary key field
Create a new table which has two fields. One for the integer id field and the other field to hold the primary key of the base table. Do an inner join with that table and select the id field from that table.
Eg. SELECT t1.id, t2.name, t2.description, t2.content FROM table_new t1 INNER JOIN table_2 t2 WHERE t1.document_id = t1.thread_id INNER JOIN REST_OF_YOUR_SELECT_QUERY
The ta.id is for Sphinx search engine to do its internal indexing.
You filter data by placing WHERE clause and filtering
You can do that in Sphinx by setting filters dynamically based on the conditions.
You select and join different tables to get results
This also can be done by setting different sources and indexes based on your requirements.
Hope this would help you to get an understanding what you need to add and modify to start thinking how Sphinx search engine can be configured to your requirements. Just come here again if your need more help.