How to create table in PHP and Oracle - php

I am trying to create table in PHP and Oracle
$create_table =" CREATE TABLE email_valid(
eid int(11) NOT NULL,
email varchar2(255) NOT NULL UNIQUE,
password varchar2(255) NOT NULL,
activation varchar2(255) NOT NULL UNIQUE,
status enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (eid))";
$stid=oci_parse($conn,$create_table);
oci_execute($stid) or die(oci_error());
But the error occurred:
Warning: oci_execute() [function.oci-execute]: ORA-00907: missing right parenthesis in C:\PORTAL\xampp\htdocs\email\create.php on line 15
I can't find out where the error is? Please help me.

Oracle does not have a sized INT(11) type, if you want to use size, you'll need to use NUMERIC.
Oracle does not have ENUM, but in this case you can use a simple char with a CHECK constraint;
--
CREATE TABLE email_valid(
eid numeric(11) NOT NULL,
email varchar2(255) NOT NULL UNIQUE,
password varchar2(255) NOT NULL,
activation varchar2(255) NOT NULL UNIQUE,
stats char(1) DEFAULT '0' NOT NULL CHECK (stats IN ('0','1')),
PRIMARY KEY (eid)
)
A simple SQLfiddle test.

Related

1.6.1.24 update to 1.7.7.4 database schema error [duplicate]

ERROR 1265: Data truncated for column 'profile_pic' at row 1
SQL Statement:
ALTER TABLE `student`.`student_info`
CHANGE COLUMN `profile_pic` `profile_pic` VARCHAR(50) NOT NULL DEFAULT 'images/profile.png'
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'student_info' already exists
SQL Statement:
CREATE TABLE `student_info` (
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`profile_pic` varchar(500) DEFAULT 'images/profile.png',
PRIMARY KEY (`email`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `password_UNIQUE` (`password`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
this error is flashing when i am setting the value of profile_pic column as not null however if i am not not doing so this error is not occurring can any body please explain me why is this error occurring and how to remove this
Change your Alter query to
ALTER TABLE `student`.`student_info`
CHANGE COLUMN `profile_pic` `profile_pic` VARCHAR(500) NOT NULL DEFAULT 'images/profile.png'
In your schema you have previously defined profile_pic with varchar(500) now you are trying to set it not null but with varchar(50) so your column contains the data which is longer than 50 characters therefore you see this truncated error

Getting error while running the mysql query to create table

I am getting one error while creating table using MySQL. I am explaining my query below.
$sql="CREATE TABLE db_cron( ".
"id INT NOT NULL AUTO_INCREMENT, ".
"user_alert INT(11) NOT NULL DEFAULT NONE, ".
"status INT(11) NOT NULL DEFAULT NONE, ".
"PRIMARY KEY ( id )); ";
I am getting the below error:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'NONE, status INT(11) NOT NULL DEFAULT NONE, PRIMARY KEY ( id ))'
at line 1
Here I need to create table with default value as none.
You have used too many quotes just to connect multiple string, it does not required.
Please remove those quotes and you also have to provide default value instead of NULL something like 0 OR any other value that you want.
However you can use below query to create table.
$sql = "CREATE TABLE `db_cron`( id INT NOT NULL AUTO_INCREMENT,
user_alert INT(11) NOT NULL DEFAULT 0,
status INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY ( id ))";
above query will create table with default value 0 for user_alert and status
Hope this will helps you
Thanks & Regards.
1.You have applied unnecessary quotes.
2.You have NOT NULL along with DEFAULT NONE, which is not going to happen(You have to provide some value as default when using NOT NULL)
Do like below:-
$sql="CREATE TABLE db_cron(
id INT(11) NOT NULL AUTO_INCREMENT,
user_alert INT(11) NOT NULL DEFAULT 0,
status INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY ( id )
)";

PDOException error in mysql SQL syntax [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to create 2 tables in the same MySQL database with a PHP-script:
table 'user' with primary key 'user_id' and table 'order' with primary key 'order_id' and foreign key 'user_id' from the 'user' table (1 to many relationship).
Table user creates successfully without problems:
$sql="CREATE TABLE user(
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
type ENUM('member','admin') NOT NULL,
username VARCHAR(30) NOT NULL,
email VARCHAR(80) NOT NULL,
pass VARBINARY(32) NOT NULL,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
date_expires DATE NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (user_id),
UNIQUE (username),
UNIQUE (email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
However, I am not able to create table order:
$sql="CREATE TABLE order(
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
I get the following error:
Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1
Already checked the syntax and cannot find the mistake. Could you please advise what went wrong? Thanks a lot.
You need to escape reserved words like order with backticks
CREATE TABLE `order` ( ...
or better use another name instead.
order is keyword used by mysql like (select from tbl_name order by id ASC) so for escaping from using keywords you have to use quotes `` to avoid my sql error
so your query should by
$sql="CREATE TABLE `order` (
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
enjoy :D

Why CREATE TABLE IF NOT EXISTS always returns false?

i don't know what i am doing wrong here but i have been trying to get this to work for hours. i just want it to create a table that doesn't exist. i made it as simple as i can make it and still it just returns false and doesn't change anything. please let me what i am doing wrong thank you in advance.
$conn=mysql_connect('localhost:3306', 'root', '');
mysql_select_db("test",$conn);
$sql = "CREATE TABLE IF NOT EXISTS 'works' (
`autoPlace` int(11) unsigned NOT NULL auto_increment,
`element` float(255) NOT NULL,
`month` tinyint(4) NOT NULL ,
`mday` tinyint(4) NOT NULL ,
`wday` char(12) NOT NULL ,
`time` smallint(6) NOT NULL,
PRIMARY KEY (`autoPlace`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$thisKeepsReturningFalse= mysql_query($sql);
var_dump($thisKeepsReturningFalse);
When I tried to execute your query I got this error:
Incorrect column specifier for column 'element'
The problem is float(255) is not a valid declaration
It needs to be float(x) where x<=53, or you can use float(x,y) - see mysql docs here
Also, as pointed out in the comments, you have single quotes around works. Remove them or replace them with backticks.
After fixing these errors, I was able to successfully execute your query.
"CREATE" command in MySQL is not return value command so that it always return false(not like SELECT). To check if the command "CREATE" success or not, you can use "SHOW TABLES" to check after executing the "CREATE" command.
When you create a float element you must specify the decimals with a comma.And also the name of the table can't be with this format '' it must to be inside ``.
So in this example the query should be :
CREATE TABLE IF NOT EXISTS `works`
(
`autoPlace` int(11) unsigned NOT NULL auto_increment,
`element` float(255,0) NOT NULL,
`month` tinyint(4) NOT NULL ,
`mday` tinyint(4) NOT NULL ,
`wday` char(12) NOT NULL ,
`time` smallint(6) NOT NULL,
PRIMARY KEY (`autoPlace`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
Note how the float it's float(255,0) and not just 255. You must set this , 0 is the default

PHP: code to create mysql table with default value

I am trying to create a table and set default values at the same time. Is it possible? When I create a table without default values, it is working fine. But when I try to add default values, it does not work. Please point out the error.
mysql_query("CREATE TABLE test
(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
test_id INT,
pwd VARCHAR(50),
cost FLOAT(5,2) NOT NULL DEFAULT '0.00',
status VARCHAR(15) NOT NULL DEFAULT 'on',
code INT NOT NULL DEFAULT '0',
scene VARCHAR(50) NOT NULL DEFAULT '0,0,0,0,0'
) TYPE=innoDB");

Categories