I saw the following SQL code. I perfectly understand it. But what am not getting is what the 66 in the code does.
CREATE TABLE `wordpress`.`campaign_queue` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`(66))
) ENGINE = InnoDB;
Explanation of the query: it is telling MySQL to create a sub part key* on the first 66 letters of id column. This only works for string types, so that query will return an error unless you change the field type to String
This give you error:
CREATE TABLE `campaign_queue1` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`(66))
) ENGINE = InnoDB;
because the (66) is to set string length.
Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys
If you change the type to VARCHAR then the problem would be with the AUTO_INCREMENT
CREATE TABLE `campaign_queue2` (
`id` VARCHAR(100) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`(10))
) ENGINE = InnoDB;
Incorrect column specifier for column 'id'
Related
Sorry if this is an easy question, I am coming to MySQL from SQL Server.
When I execute my create statement it contains nvarchar but commits to the database as varchar. Even in my alter statement afterwards the column does not change at all. Does the collation or DB engine make a difference?
During execution I am not encountering any issues in results, other than the fact the column changes datatype. I attached a screencast of my activity http://screencast.com/t/wc94oei2
I have not been able to find anyone with similar issues through my Google searches
Did you mean, this..
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name nvarchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
----turns to---
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name varchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
I'm having some problems with this piece of mySQL code that is not wanting to get fixed
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`),
INDEX `databaseID`
)ENGINE = InnoDB;
It says that there is an error at line 1 with the regular "check your mysql syntax for right usage" error in response to ` usage. Is there something I'm missing? I'm new to sql so I might be missing something obvious.
Thanks.
The main point for your problem is at the line you are defining the index. In create table statement, you should use it with this syntax:
create table table_name (
...
index `INDEX_NAME` (`INDEX_COLUMN`)
);
So you can fix your problem by changing your code to below:
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`),
INDEX `ix_databaseID` (`databaseID`) # Note the change on this line
)ENGINE = InnoDB;
However, in MySQL primary key column gets an index by default, so you can leave out that line totally, that results in the following code:
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`)
)ENGINE = InnoDB;
To improve more:
databaseID is already a primary key, so you do not have to make define it unique again, since: primary key = unique + not null
Since MySQL is case insensitive, you should not use camel case names. So instead of databaseID, better to say database_id. There are more naming convention you can go through, though I will not mention here.
So for final table defination I suggest:
CREATE TABLE `database_mgr`
(
`database_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`prim_cat` INT UNSIGNED NOT NULL,
`database_name` VARCHAR(20),
PRIMARY KEY (`databaseID`)
)ENGINE = InnoDB;
I am having a problem on the creation of my table.
The problem lays with the creation of the foreign key/relation to antoher table.
The internet told me to check the column type and check if it was the same as the column which i was referencing to. and its the same.
Futher solutions the internet gave me didn't work So can anyone please help me?
This is the query :
CREATE TABLE IF NOT EXISTS `finprodb`.`tblproject` (
`prj_id` INT(11) NOT NULL AUTO_INCREMENT ,
`prj_nummer` VARCHAR(45) NOT NULL ,
`prj_omschrijving` TEXT NULL DEFAULT NULL ,
`prj_verkoop_waarde` DECIMAL(20,4) NULL DEFAULT NULL ,
`prj_gereed` TINYINT(4) NULL DEFAULT NULL ,
`prj_bedr_id` INT(11) NOT NULL ,
PRIMARY KEY (`prj_id`) ,
UNIQUE INDEX `prj_id_UNIQUE` (`prj_id` ASC) ,
INDEX `fk_tblproject_tblbedrijf1_idx` (`prj_bedr_id` ASC) ,
CONSTRAINT `fk_tblproject_tblbedrijf1`
FOREIGN KEY (`prj_bedr_id` )
REFERENCES `finprodb`.`tblbedrijf` (`bedr_id` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
ON DELETE SET NULL does not go well with prj_bedr_id INT(11) NOT NULL
Make the column nullable (remove the NOT)
I don't know MYSQL well but in DB2 you can't create a relationship unless there is a unique index on the columns referenced in the parent table.
Is there a unique index on finprodb.tblbedrijf (bedr_id ) ?
I have created several tables with MySQL Workbench and I'm having a problem with Foreign Keys. If I export code below I get an error however if I do not export with foreign keys I do not get an error could you have a look at the code for me.
SQL Code:
DROP TABLE IF EXISTS `mydb`.`users` ;
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`uuserid` INT NOT NULL AUTO_INCREMENT ,
`ufname` VARCHAR(25) NULL ,
`ulname` VARCHAR(25) NULL ,
`uuname` VARCHAR(45) NULL ,
`upass` CHAR(64) NOT NULL ,
`uemail` VARCHAR(254) NOT NULL ,
`urole` INT NULL DEFAULT 0 ,
PRIMARY KEY (`uuserid`) )
ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`photos` ;
CREATE TABLE IF NOT EXISTS `mydb`.`photos` (
`pphotoid` INT NOT NULL ,
`pname` VARCHAR(4) NULL ,
`plat` FLOAT(10,6) NULL ,
`plng` FLOAT(10,6) NULL ,
`pflag` DECIMAL(10,0) NULL DEFAULT 0 ,
`pext` VARCHAR(4) NULL ,
`plike` DECIMAL(10,0) NULL DEFAULT 0 ,
PRIMARY KEY (`pphotoid`) ,
CONSTRAINT `uuserid`
FOREIGN KEY ()
REFERENCES `mydb`.`users` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
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 ') REFERENCES `mydb`.`users` () ON DELETE NO ACTION ON UPDAT' at line 21
My Database has many more tables but this code also breaks. I have tried to insert this using phpmyadmin.
Here you need to specify the column name
FOREIGN KEY (present_table_column_name)
REFERENCES mydb.users (foriegn_key_column_name)
Try running this script. I build this using your script and ran it in SQLFiddle so I know it works. In order to add a foreign key you have to have a field that will be appropriate. In this example I added a uuserid column in the photos table. This column has a foreign key constraint to the uuserid column in the users table.
DROP TABLE IF EXISTS `mydb`.`users` ;
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`uuserid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`ufname` VARCHAR(25) NULL ,
`ulname` VARCHAR(25) NULL ,
`uuname` VARCHAR(45) NULL ,
`upass` CHAR(64) NOT NULL ,
`uemail` VARCHAR(254) NOT NULL ,
`urole` INT NULL DEFAULT 0
) ENGINE = InnoDB;
DROP TABLE IF EXISTS `mydb`.`photos` ;
CREATE TABLE IF NOT EXISTS `mydb`.`photos` (
`pphotoid` INT NOT NULL PRIMARY KEY,
`pname` VARCHAR(4) NULL ,
`plat` FLOAT(10,6) NULL ,
`plng` FLOAT(10,6) NULL ,
`pflag` DECIMAL(10,0) NULL DEFAULT 0 ,
`pext` VARCHAR(4) NULL ,
`plike` DECIMAL(10,0) NULL DEFAULT 0,
`uuserid`INT NOT NULL
) ENGINE = InnoDB;
Alter Table `photos` add constraint foreign key(`uuserid`) references `users`(`uuserid`);
In this example above, I moved the statement that sets the primary keys to the columns themselves rather than creating a statement at the end of the SQL.
I thought it would be a little clearer to show you how to add a foreign key constraint outside of the table creation so you can see what caused the syntax error. You had no column names in your foreign key references. Once added the above code will run perfectly.
You have a syntax error exactly where the error message tells you you have one.
The problem is you don't have you have any fields referenced in your constraint. The form of the CONSTRAINT clause should be like this:
CONSTRAINT `fkuuserid`
FOREIGN KEY `fkuuserid` (some_field_in_this_table)
REFERENCES `mydb`.`users` (uuserid)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I honestly don't even see where you have a logical foreign key on the photos table, as there is no uuserid field present in this table.
I'm getting a syntax error when creating a table with the result of PHP's md5(microtime()) as the column name.
In particular, the error is getting thrown at the part with ** surrounding it:
CREATE TABLE form_data_38 (
id INT SIGNED auto_increment NOT NULL,
rltd_pri_key INT SIGNED NULL,
0accc77c084cc74a51dee479f8d095e3 TEXT(65535) NOT NULL,
**092e60b78f7804e86ea9a6e83701a929 TEXT(65535) NOT NULL**,
6734131796201537410e4d43635bf1b3 TEXT(65535) NULL,
PRIMARY KEY (id)
) TYPE=InnoDB;
What is confusing me is why it's being thrown at that spot and not the other 2 hash values prior to it. I appended 'a' to the column names and it created the table no problem. I've searched MySQL naming rules and so far I haven't come up with anything. It just says all alphanumeric characters plus '_' and '$' are okay to use, which should be fine in this instance.
What am I missing?
Put your table and field names in backquotes: `09e4_fieldName`, at least for those that can create such problems:
CREATE TABLE form_data_38
( id INT SIGNED auto_increment NOT NULL
, rltd_pri_key INT SIGNED NULL
, `0accc77c084cc74a51dee479f8d095e3` TEXT(65535) NOT NULL
, `092e60b78f7804e86ea9a6e83701a929` TEXT(65535) NOT NULL
, `6734131796201537410e4d43635bf1b3` TEXT(65535) NULL
, PRIMARY KEY (id)
)
ENGINE = InnoDB ; --- ENGINE, not TYPE