I've created a database in MySQL which has a user_id that is primary key and auto incremented. Now my question is can I insert a static data in a column that is auto incremented? For example I want to insert a data "U-01" instead of just 1 only.
The column must be unauto increment, then you can insert this data!
Related
Good day guys, I have a grade/score table in MySql that students record will be inserted into using php. I want to avoid a student having a score repeated for a term/period. What I mean is that a student cant have two(2) grades/scores for a subject(mathematics) in a term(periodOne) table. How do I accomplish this in MySql or php? here is how my table looks:
table periodOne (
id int AUTO_INCREMENT,
studentId int,
subjectId int,
score
)
Let me know if you need extra information. Thanks!!!!!!
you have to add a unique contraint in mysql like this : ALTER TABLE periodOne ADD CONSTRAINT uc_check UNIQUE(studentId, subjectId). You will also have to check with PHP that there is no existing row before to do your INSERT
You can declare the attribute as "Unique" by using UNIQUE CONSTRAINT for which you don't want duplicate value.
If your score are dependent on some other table also then you can use Composite Primary Key.
Two Table
First table CUST_ENTRY
cust_id(primary key),cust_name,cust_add
Second table CUST_PRICE
cust_price_id,cust_id(foreign key),cust_price1,cust_date1,cust_price2,cust_date2
I have insert first table completely but second table data cust_price2 and cust_date2 is insert blank but that not possible.
Please help me.
I have a table - 'A' which has primary key - id which is auto incremented.I also have a table - 'B' which also has the column 'id'. Now I want to add entries to the table A so that value of id attribute of table 'A' which just got inserted to the table also gets inserted to B while other attributes of B can remain NULL.I know I can just make one table with all the columns but is there a way to do this with two tables??
Use the mysqli_insert_id () function right after you run the insert to table a. That will give you the value of the auto incremented id from table a which you can then populate in the surrogate key column of table b.
Read about it here:
http://php.net/manual/en/mysqli.insert-id.php
how can i set id primarykey auto_increment same value that automatically generating into my id field will also generate in other field in same table.
How could it possible?
Use mysql_insert_id which returns last auto increment ID...
1) insert into first table, use mysql_insert_id() to get auto increment ID
2) after getting auto increment ID from 1st table, insert on second table with the ID you got.
for more refer http://php.net/manual/en/mysqli.insert-id.php
I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;