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;
Related
I have a database. As you can see the primary key is an auto_increment and is also unique. I read that publically sharing a row's primary key of a table to the public is unsafe. I want to assign each row in customers a unique ID that I can publically share. How can I do this without having to specify each time what the public_id is in the INSERT statement? The database should automatically find a unique ID to assign to that row just like it does for id because of auto_increment.
CREATE TABLE customers (
id int primary key auto_increment,
name varchar(32) not null,
-- public_id (an ID I can give to the public to uniquely identify this row
);
INSERT INTO customers (name) VALUES ('Bob'), ('Sarah'), ('Bob');
Well, here's one way:
CREATE TABLE customers (
id int primary key auto_increment,
name varchar(32) not null,
public_id char(36) not null unique default uuid()
);
Note that the manual says:
Warning
Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. If unpredictability is required, UUID values should be generated some other way.
So this is simple, and maybe will float your goat, but we can also try better:
CREATE TABLE customers (
id int primary key auto_increment,
name varchar(32) not null,
public_id char(24) not null unique default to_base64(random_bytes(18))
);
This will be a nice and dense identifier, but it will have characters + and / which don't play well with URLs. You can encode them, of course, but if you want to go one lazier, you can also do this:
CREATE TABLE customers (
id int primary key auto_increment,
name varchar(32) not null,
public_id char(32) not null unique default hex(random_bytes(16))
);
Mind you, the identifier will get quite a bit longer this way.
To get the best of both worlds, we can do this, at the expense of a really long default value:
CREATE TABLE customers (
id int primary key auto_increment,
name varchar(32) not null,
public_id char(24) not null unique default replace(replace(to_base64(random_bytes(18)), '+', '_'), '/', '-')
);
Also note that messing around with MD5()/SHA()/SHA1()/SHA2() is no better than just generating a random hex string with a given length.
I am trying to run a CREATE TABLE script which has multiple INDEXES.
CREATE TABLE IF NOT EXISTS Equipment (
EquipmentID BIGINT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
Type VARCHAR(255) NOT NULL,
Make VARCHAR(255),
Model VARCHAR(255),
Description VARCHAR(255),
OperationNotes TEXT,
Damaged BOOLEAN DEFAULT 0,
PRIMARY KEY (EquipmentID),
INDEX ('EquipmentID'),
INDEX('Type'),
INDEX('Model'),
INDEX('Description')
INDEX('Damaged')
);
However I get a syntax error:
"(" is not valid at this position for this server version
On line:
INDEX ('EquipmentID'),
Single quote (') denote string literals. When referring to object names (such as columns), you shouldn't use single quotes. Remove them, and you should be OK. Also, note that a primary key implicitly creates an index, so you don't need to explicitly create an index on EquipmentID:
CREATE TABLE IF NOT EXISTS Equipment (
EquipmentID BIGINT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
Type VARCHAR(255) NOT NULL,
Make VARCHAR(255),
Model VARCHAR(255),
Description VARCHAR(255),
OperationNotes TEXT,
Damaged BOOLEAN DEFAULT 0,
PRIMARY KEY (EquipmentID),
INDEX (Type),
INDEX (Model),
INDEX (Description),
INDEX (Damaged)
);
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'
I keep getting the error code 'ORA-02270' and no matter what I try, I can't seem to fix it. Below are my Create Table statements:
CREATE TABLE student
(
studentID CHAR(8) PRIMARY KEY,
studentName VARCHAR(25) NOT NULL,
studentAddress VARCHAR(30) NOT NULL,
studentDOB DATE NOT NULL,
studentGender CHAR(1) NOT NULL CHECK ((studentGender = 'F') OR (studentGender = 'M')),
studentNationality VARCHAR(15) NOT NULL,
studentCourse VARCHAR(30) NOT NULL,
studentSemesterExcellent CHAR(1) NOT NULL CHECK ((studentSemesterExcellent = 'Y') OR (studentSemesterExcellent = 'N'))
);
CREATE TABLE leaseAgreement
(
leaseNo CHAR(6) PRIMARY KEY,
studentID CHAR(8) NOT NULL,
leaseAccommodationType VARCHAR2(10) NOT NULL,
leaseDuration NUMBER NOT NULL,
leaseStartDate DATE NOT NULL,
leaseEndDate DATE,
studentSemesterExcellent CHAR(1) NOT NULL CHECK ((studentSemesterExcellent = 'Y') OR (studentSemesterExcellent = 'N')),
FOREIGN KEY (studentID) REFERENCES student(studentID),
FOREIGN KEY (studentSemesterExcellent) REFERENCES student(studentSemesterExcellent)
);
Am I not allowed to have two foreign keys from the same table? Please can someone explain this error and point me in the right direction. Thank you.
First, the answer from mustaccio is correct.
The column 'studentSemesterExcellent' in table 'student' is no key column. You can not reference it in a foreign key constraint.
And if you make it unique you can only have two rows in the student table, not what you are intending.
Second, you have tagged the question both with MySQL and Oracle. Choose one!
Third, you only need one FK if it is a not null column. Don't make it harder on yourself.
Fourth, if this is Oracle database, the advice is to only use varchar2() for string data. The char and varchar datatypes exist for compatibility reasons.
I'm trying to create a table to capture details of subtype variations using auto_increment but I keep get an error saying:
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
However, I'm unsure how to capture it all correctly as I want the auto_increment of subtypes to start from 1 for each TypeId....is this not possible?
CREATE TABLE IF NOT EXISTS `types` (
`TypeID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`TypeFeatures` TEXT NOT NULL,
PRIMARY KEY (`TypeID`))
ENGINE = InnoDB;
insert into types
(TypeFeatures)
values
(1,'Type1'),
(2,'Type2'),
(3,'Type3')
;
CREATE TABLE IF NOT EXISTS `subtypes` (
`Type_ID` INT UNSIGNED NOT NULL,
`SubtypeID` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`SubtypeFeatures` TEXT NOT NULL,
PRIMARY KEY(`Type_ID`,`SubtypeID`))
ENGINE = InnoDB;
insert into subtypes
(Type_ID,subtypeID,SubtypeFeatures)
values
(1,1,'Subtype1'),
(1,2,'Subtype2'),
(2,1,'Subtype1'),
(3,1,'Subtype1')
;
Thanks in advance for any suggestions!
Bendy