How to implement the following data transferring in mysql - php

First I have to create a column in the end of table
If there exist the data in the original column , move them into the new column
If the original column has no data now
(because they must be nullable , so that there is chance the column has no data, the column will be deleted)
The problems are:
move from col to new col is insert into new col {select old colmn from table} ?
Then how can I check the column exist no data / all col is null.
Also, How can I check datatype and whether it is null in pdo ?
Thank you

If you want to add a column to a table:
alter table myTable add column 'columnName' <column-specifications>
If you want to insert values from another column into this column:
update myTable set columnName=originalColumn
If you want to remove a column from a table:
alter table myTable drop column columnName
That being said, like Jeff Paquette mentioned, are you sure you want to create a new column? I don't really understand the point of creating a column to insert values from another column. Won't renaming your original column do the exact same thing?
alter table myTable change column ...

Related

how to insert a column value in mysql

A db table consists of 3 columns. But I want to insert a value to one column and I want the other 2 to remain empty. Like database has three input column- roll, date, time, I want only to insert roll. My query:
insert into tableName(roll) values ('$_POST['roll'])
But I am getting the error message ( Field attn_time doesn't have a default value41 ). What would be the correct query?enter image description here
database table
You need to alter your database table by giving a default value to your attn_time column.
Example:
ALTER TABLE table_name MODIFY COLUMN column_name VARCHAR(255) NOT NULL DEFAULT '';
You achieve this in two ways:
1) Go to your phpmyadmin/mysql and change your columns(attn, attn_time) "Null" property to "Yes". This will allow null value to store in column if non value given.
[You can find "Change" in "Structure" tab in phpmyadmin]
2) You can change your query to
insert into tbl1 (roll, attn, attn_time) values ('$roll', null, null)
OR
insert into tbl1 (roll, attn, attn_time) values ('$roll', '', '')
You can use :
$roll=$_POST['roll'];
add your validation :
insert into tableName(roll) values ('$roll');

Re-order my records and then create(or reset if its possible) a new auto increment field in MySQL

I need to do something in MySQL, but I can't. I want to reorder
my records in my table and then create(or reset if its possible) a new auto
increment field in MySQL, as it is shown in the picture. I want to order them by middle column and then reset the first column.
Before you start backup your table or database. After that you can create same table and set auto increment to the field which you want to be ai, somethign like this below:
CREATE TABLE sampleTable
(`ID` int auto_increment primary key, `Field1` int, `Field2` int);
ID will be your auto increment(in this example) and fields will be your fields(or you can just copy the structure of your table instead of writing them down again.)
Afterwards select your old table data by the order you want and insert into new table:
INSERT INTO sampleTable (`Field1`, `Field2`)
SELECT Field1, Field2
FROM oldTable
ORDER BY Field1;
So your new table will be sorted by the field you want and have their auto increment reset as well.

PHP Mysql query insert

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 to get MySQL value from certain row?

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;

How to add same column to multiple tables in a DB and fill it with a constant value?

I have a database with multiple tables. I want to add a new column to each one of them and fill these column with some constant integer value. My search on google did not give me desired answer.
I am using PostgreSQL and PHP.
Can't you use ALTER TABLE to create a new column and SET DEFAULT to set the default value to all column fields?
Like:
ALTER TABLE mytable ADD COLUMN mynumber integer;
ALTER mynumber SET DEFAULT 7
(http://www.postgresql.org/docs/8.2/static/sql-altertable.html)

Categories