I have a sql query,
$sql = "select
tbl_user.name,chat.from,chat.message,chat.to,chat.id,chat.sent,chat.recd
from chat,tbl_user where (chat.to =
'".mysql_real_escape_string($_SESSION['chatuser'])."' AND recd = 0) and
chat.from=tbl_user.user_id order by id ASC";
But i dont know how to create a table from this information. The above is
a select table syntax, but can anyone help me to make the above sql query
to a proper syntax, so that i can create the table.
Example, i need the ab0ve t0 like this
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
You can use the CREATE TABLE new_tbl AS SELECT ...
Read the documentation for MySql:
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
Related
I want to copy a column (definition, not data) from one table to another. Add the column if it doesn't exist in destination table, or better yet, modify it accordingly if it exists.
Tried to google but everything seems to be about either copying the data rather than the definition, or about copying an entire table using CREATE TABLE ... LIKE ....
Thus far the closest I could find is this:
CREATE TABLE table2 AS
SELECT field4, field7 -- only the columns you want
FROM table
WHERE FALSE; -- and no data
But I need to copy columns to existing tables. So I tried:
ALTER TABLE table2 AS
SELECT field4, field7 -- only the columns you want
FROM table
WHERE FALSE; -- and no data
But apparently it refused to work.
Is there something like this?
ALTER TABLE table1 ADD COLUMN column2 LIKE table2.column5
Any way to achieve this in MySQL?
The best way is to Dump the table and then you can use ALTER command to create the new table.
Dumping the table gives back the query to CREATE the table.
CREATE TABLE IF NOT EXISTS customer (
id int(11) NOT NULL AUTO_INCREMENT,
customerId varchar(50) NOT NULL,
firstName varchar(30) NOT NULL,
middleName varchar(30) DEFAULT NULL,
lastName varchar(30) CHARACTER SET utf8 COLLATE utf8_estonian_ci DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
USE the same to ALTER an existing table, (say customerinfo)
`ALTER TABLE customerinfo ADD COLUMN (
customerId varchar(50) NOT NULL,
firstName varchar(30) NOT NULL,
middleName varchar(30) DEFAULT NULL,
lastName varchar(30) CHARACTER SET utf8 COLLATE utf8_estonian_ci DEFAULT NULL
)
If you want to do it with queries this is the way:
Get the properties of the old column with:
SHOW COLUMNS FROM `mytable` where Field=`mycolumn`
Use mysqli_fetch_array to get the field properties in the variable $row.
Now create the new query:
$qry = "ALTER TABLE othertable";
$qry .= " ADD `newcol`"; /* name of new column; $row['Field'] works too */
$qry .= " ".$row["Type"]; /* example value: varchar(255) */
if($row["Null"] == "NO")
$qry .= " NOT NULL";
if($row["Default"] != "")
$qry .= " default '".$row["Default"]."'";
The $row contains also an Extra field but that can give problems with auto_increase.
Good day everyone. Hope someone can helps me in my experiments.
I have a simple Table for my needs and it's even have some data inside.
CREATE TABLE IF NOT exists main.test (
ID INT(11) NOT NULL,
NAME varchar(30) NOT NULL,
DATE_CREATED timestamp NOT NULL,
PRIMARY KEY (ID));
But then I should update this table with adding column FK_.
How can I check if table had already has field FK_?
If such column is not exist do:
ALTER TABLE main.test
ADD COLUMN FK INT(11),
ADD FOREIGN KEY (FK)
REFERENCES test2(ID_test2)
As I use java decision of my problem was using ResultSet.
ResultSet set = statement.executeQuery(query);
set.next();
int result = set.getInt(1); //it always return only one row
set.close();
And this is my sql-query:
SELECT COUNT(COLUMN_NAME) FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'main'
AND TABLE_NAME = 'test'
AND COLUMN_NAME = 'FK";
When I'll get a result I can decide what query I should to use.
I don't seem to be able to get my SELECT statement to work.
This is the table:
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL,
UNIQUE KEY (email)
);
The Select Query
SELECT user_id FROM clients WHERE email='info#candy.co.uk';
Whenever I try using this SELECT statement from mysqlADMIN it returns null; this happens even when I enter an email address that I know is in the database.
I would really appreciate some advice on where I am going wrong.
Try the statement without the "WHERE" clause. If it returns the entire table you have narrowed it down to an error in your "email" string.
If it returns nothing and you know there is data in this table then check your connection string and make sure you are using the correct DB.
I think there is some error in your SQL create statement. You should make the unique key to which you have applied auto increment. In this case the database will give an error.
Please try the following creation statement
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL
);
My table contains history of values relating to objects, it looks like:
create table History (
object_id bigint NOT NULL,
value int NOT NULL,
date bigint NOT NULL
);
How can I index it to optimize following query:
select * from History
where object_id = ? and date < ?
order by date desc
limit ?
Create composite index object_id + date
CREATE INDEX object_id_date ON History(object_id, `date`);
I'm trying to create a table in my sql using PHP but I'm not sure how to set an initial value for the auto increment field.
This is what i have so far:
function create_table($db_host,$db_user,$db_pswrd,$db_name){
$connect = mysql_connect($db_host,$db_user,$db_pswrd) or die(mysql_error());
mysql_select_db($db_name, $connect);
$sql = "CREATE TABLE MY_TABLE
(
table_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(table_id),
table_1 varchar(45),
table_2 varchar(45),
table_3 varchar(999),
table_4 varchar(45)
)"or die(mysql_error());
mysql_query($sql,$connect)or die(mysql_error());
mysql_close($connect);
}
So i need to know how to set the initial Auto Increment value on this table, upon creation?
Thanks
If you don't have the auto-increment column in the table yet:
$sql = "ALTER TABLE MY_TABLE ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (id);";
Then to set the auto-increment starting value:
$sql = "ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;";
Potential duplicate of this post.
Assuming your auto increment column is the first one:
$sql = "CREATE TABLE MY_TABLE
(
table_1 INT AUTO_INCREMENT,
table_2 varchar(45),
table_3 varchar(999),
table_4 varchar(45)
) AUTO_INCREMENT = 231";
The starting value will be, here, 231.
I changed the column type to INT, because you can't use a VARCHAR for auto-increment.
(and remove the or die(mysql_error()) on this line btw, its pointless because it's just a variable creation, not a SQL query being executed)