What does the engine and char set means after the ()? - mysql

DROP DATABASE IF EXISTS `Database` ;
CREATE DATABASE `Database` ;
DROP TABLE IF EXISTS `Registration Form` ;
CREATE TABLE `Registration Form`
(
`ID` int(11) NOT NULL AUTO_INCREMENT ,
`Username` varchar(50) NOT NULL ,
`Password` varchar(50) NOT NULL ,
`Display Name` varchar(255) NOT NULL ,
`Question` varchar(255) NOT NULL ,
`Answer` varchar(255) NOT NULL ,
`Title` varchar(10) NOT NULL ,
`Name` varchar(255) NOT NULL ,
`Date Of Birth` date NOT NULL ,
`Gender` varchar(10) NOT NULL ,
`Address` varchar(255) NOT NULL ,
`Postal Code` int(11) NOT NULL ,
`City / Town` varchar(255) NOT NULL ,
`Federal Territory / State` varchar(255) NOT NULL ,
`Citizenship` varchar(255) NOT NULL ,
`E-mail` varchar(255) NOT NULL ,
`Contact Number` int(20) NOT NULL ,
`Registration Date` datetime NOT NULL ,
PRIMARY KEY ( `ID` )
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
I'm just starting to work arounnd mysql and php ... so bear with me as im a newbie ...
What does ALL the code after the CREATE() mean ? Why is there a ENGINE and DEFAULT what do they do ? Is CHARSET same as collation , if yes why not put it inside the row instead ? Do you need to give a length/value for date and daetime ?

CREATE TABLE is the sql clause which is required to creating a table.
After that you have to give a table name i.e "Registration Form" .but avoid spaces in between table name
Next what ever you gave those are column name along with their datatype and size.NOT NULL means while inserting data those column can't be blank.
For more details check the below link
http://www.tutorialspoint.com/mysql/mysql-create-tables.htm

Related

How to resolve an error near NULL when creating a table

Whenever I try to create a table
CREATE TABLE registration` (`id` INT NOT NULL , `name` VARCHAR(30) NOT NULL , `email` VARCHAR(20) NOT NULL , `password` VARCHAR(15) NOT NULL , `DOB` DATE NOT NULL , `age` INT NOT NULL , `number` BIGINT NOT NULL , `religion` VARCHAR(10) NOT NULL , `education` VARCHAR(20) NOT NULL , `profession` VARCHAR(20) NOT NULL , `gender` ENUM NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
The following error occurs
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL , PRIMARY KEY (id)) ENGINE = InnoDB' at line 1
ENUM needs values so you need to define the values they can get
CREATE TABLE registration (
`id` INT NOT NULL
, `name` VARCHAR(30) NOT NULL
, `email` VARCHAR(20) NOT NULL
, `password` VARCHAR(15) NOT NULL
, `DOB` DATE NOT NULL
, `age` INT NOT NULL
, `number` BIGINT NOT NULL
, `religion` VARCHAR(10) NOT NULL
, `education` VARCHAR(20) NOT NULL
, `profession` VARCHAR(20) NOT NULL
, `gender` ENUM ('male','female')
, PRIMARY KEY (`id`)
) ENGINE = InnoDB;
When you declare a ENUM column (your gender column) you have to assign all possible values for the enumeration (See here for syntax)

MySQL error while trying to create table,

I'm using PHPMyAdmin, hosted with hostgator, to add a table to a database, but I keep getting the following 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 ') NOT NULL , note TEXT NOT NULL , cardNumber BIGINT(20) NOT NULL , `nameC' at line 1
Here's what I have:
and here's the preview of the SQL
CREATE TABLE `nightwin_mark-inn`.`guests` (
`id` INT(3) NOT NULL AUTO_INCREMENT ,
`dateIn` DATE NOT NULL ,
`dateOut` DATE NOT NULL ,
`email` TEXT NOT NULL ,
`phone` INT(10) NOT NULL ,
`room` TINYINT(2) NOT NULL ,
`price` DOUBLE(6) NOT NULL ,
`note` TEXT NOT NULL ,
`cardNumber` BIGINT(20) NOT NULL ,
`nameCard` TEXT NOT NULL ,
`expDate` TEXT NOT NULL ,
`cvc` TINYINT(3) NOT NULL ,
PRIMARY KEY (`id`)
)
What's causing this issue? Do I have the length of one of the fields wrong?
Try to use this
price` DOUBLE(6,2) NOT NULL //9999.99 max value stored
instead of
price` DOUBLE(6) NOT NULL
Note: for price field use datatype DECIMAL more preferable. In FLOAT or DOUBLE datatype you will get rounding number issue
Reference
You can try below - DOUBLE(6) should be only DOUBLE
CREATE TABLE `nightwin_mark-inn`.`guests` ( `id` INT(3) NOT NULL AUTO_INCREMENT ,
`dateIn` DATE NOT NULL , `dateOut` DATE NOT NULL , `email` TEXT NOT NULL ,
`phone` INT(10) NOT NULL , `room` TINYINT(2) NOT NULL , `price` DOUBLE NOT NULL ,
`note` TEXT NOT NULL , `cardNumber` BIGINT(20) NOT NULL , `nameCard` TEXT NOT NULL ,
`expDate` TEXT NOT NULL , `cvc` TINYINT(3) NOT NULL , PRIMARY KEY (`id`))
please try using this MySQL statement
CREATE TABLE `guests` ( `id` INT(3) NOT NULL AUTO_INCREMENT , `dateIn` DATE NOT NULL , `dateOut` DATE NOT NULL , `email` TEXT NOT NULL , `phone` INT(10) NOT NULL , `room` TINYINT(2) NOT NULL , `price` DOUBLE(6,2) NOT NULL , `note` TEXT NOT NULL , `cardNumber` BIGINT(20) NOT NULL , `nameCard` TEXT NOT NULL , `expDate` TEXT NOT NULL , `cvc` TINYINT(3) NOT NULL , PRIMARY KEY (`id`));
please try using this MySQL statement
CREATE TABLE guests ( id INT(3) NOT NULL AUTO_INCREMENT , dateIn DATE NOT NULL , dateOut DATE NOT NULL , email TEXT NOT NULL , phone INT(10) NOT NULL , room TINYINT(2) NOT NULL , price DOUBLE(6,2) NOT NULL , note TEXT NOT NULL , cardNumber BIGINT(20) NOT NULL , nameCard TEXT NOT NULL , expDate TEXT NOT NULL , cvc TINYINT(3) NOT NULL , PRIMARY KEY (id));

Unable to create mysql table Dynamically

I am trying to create a page where a user inputs data for an advertisement and after taking inputs it creates a table where data can be stored . Data being passed correctly and a way to always ensure the table name generated will be unique.
But I am unable to get the query executed and having no idea that why its not working.
I am a beginner and hence any advice at all would be much appreciated.
My code is given below
$Tablename = $Title.$Mobileno;
echo $Tablename;
$sqltable = "CREATE TABLE $Tablename ( `ID` INT NOT NULL AUTO_INCREMENT , `Title` VARCHAR(30) NOT NULL , `Sname` VARCHAR(30) NOT NULL , `email` VARCHAR(50) NOT NULL , `descrip` VARCHAR(200) NOT NULL , `mno` INT(30) NOT NULL , `base` INT(30) NOT NULL , `cutoff` INT(30) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB";
mysqli_query($con,$sqltable);
if(!mysqli_query($con,$sqltable))
{echo "TABLE NOT MADE ";}
$sql = "INSERT INTO $Tablename(Title,Sname,email,descrip,mno,base,cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')";
mysqli_query($con,$sqltable);
if(!mysqli_query($con,$sql))
{echo'not inserted';}
else {echo'inserted';}
?>
Try enciosing with quote the tablename
"CREATE TABLE '$Tablename' (
`ID` INT NOT NULL AUTO_INCREMENT
, `Title` VARCHAR(30) NOT NULL
, `Sname` VARCHAR(30) NOT NULL
, `email` VARCHAR(50) NOT NULL
, `descrip` VARCHAR(200) NOT NULL
, `mno` INT(30) NOT NULL
, `base` INT(30) NOT NULL
, `cutoff` INT(30) NOT NULL
, PRIMARY KEY (`ID`)) ENGINE = InnoDB";
or use string concatenation
"CREATE TABLE '" . $Tablename."' (
`ID` INT NOT NULL AUTO_INCREMENT
, `Title` VARCHAR(30) NOT NULL
, `Sname` VARCHAR(30) NOT NULL
, `email` VARCHAR(50) NOT NULL
, `descrip` VARCHAR(200) NOT NULL
, `mno` INT(30) NOT NULL
, `base` INT(30) NOT NULL
, `cutoff` INT(30) NOT NULL
, PRIMARY KEY (`ID`)) ENGINE = InnoDB";
be sure you have proper sanitize the $Tablename vars

Create a table with select columns from Another Table

I'm trying to break down a GPS point in a new table since the old table is in CHAR my new table will be set to INT.
Drop Table LoopTest1;
CREATE TABLE LoopTest1 (
`Account Number` char(32) NULL ,
`Latitude` decimal(30,2) NULL ,
`LatDegree` int(2) NULL ,
`LatMinutes` int(2) NULL ,
`LatSeconds` decimal(5,2) NULL ,
`LatDecimal` decimal(30,6) NULL ,
`Longitude` decimal(30,2) NULL ,
`LongDegree` int(2) NULL ,
`LongMinutes` int(2) NULL ,
`LongSeconds` decimal(5,2) NULL ,
`LongDecimal` decimal(30,6) NULL
);
set
LoopTest2.`Account Number` = LoopTest1.`Account Number`,
LoopTest2.Latitude = LoopTest1.Latitude,
LoopTest2.Longitude =LoopTest1. Longitude
FROM
LoopTest2
This will only be a temp table since I will update LoopTest2 with the changes made in LoopTest1

MYSQL error in creating foreign key constraint

I am using MySQL to create a small database. and facing some issues in foreign keys and primary key. I really don't understand as it seems a simple problem.
CREATE TABLE IF NOT EXISTS `db_trimms`.`urban_area` (
`area_id` INT(10) NOT NULL ,
`city` VARCHAR(60) NOT NULL ,
`state` VARCHAR(60) NOT NULL ,
`urban_area` VARCHAR(60) NOT NULL ,
`census_region` VARCHAR(60) NOT NULL ,
`area_no` VARCHAR(60) NOT NULL ,
`freeway_speed` VARCHAR(60) NOT NULL ,
`arterial_speed` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
its running successfully. But while create another table and creating foreign key referring to the area_id of the above table...its creating issues...
#1005 - Can't create table 'db_trimms.emiss_others_offpeak' (errno: 150) (Details...)
and here is the query
CREATE TABLE IF NOT EXISTS `db_trimms`.`emiss_others_offpeak` (
`area_id` INT(10) UNSIGNED NOT NULL ,
`ammonia` VARCHAR(60) NOT NULL ,
`atm_carbon_dio` VARCHAR(60) NOT NULL ,
`carbon_dio_equiv` VARCHAR(60) NOT NULL ,
`carbon_mono` VARCHAR(60) NOT NULL ,
`methane` VARCHAR(60) NOT NULL ,
`nitrogen_dio` VARCHAR(60) NOT NULL ,
`nitrogen_oxide` VARCHAR(60) NOT NULL ,
`nitrous_oxide` VARCHAR(60) NOT NULL ,
`non_meth_hydrocarbs` VARCHAR(60) NOT NULL ,
`oxides_of_nitrogen` VARCHAR(60) NOT NULL ,
`particulate_matter_pm10` VARCHAR(60) NOT NULL ,
`particulate_matter_pm2_5` VARCHAR(60) NOT NULL ,
`sulfate` VARCHAR(60) NOT NULL ,
` sulfur_dioxide` VARCHAR(60) NOT NULL ,
`total_hydrocarbon` VARCHAR(60) NOT NULL ,
`vol_org_comp` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) ,
CONSTRAINT `fk_others_offpeak_urban`
FOREIGN KEY (`area_id` )
REFERENCES `db_trimms`.`urban_area` (`area_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
The data types are incompatible with each other. Make column area_id from table urban_area also UNSIGNED.
`area_id` INT(10) UNSIGNED NOT NULL ,
SQLFiddle Demo