This is the SQL line I'm using to create the table:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(255),
surname VARCHAR(255),
email VARCHAR(255) UNIQUE,
username VARCHAR(255) UNIQUE,
passwordHash CHAR(60),
admin BIT
)
Should be pretty generic, however, I'm getting the error "Specified key was too long; max key length is 767 bytes"
Which just leaves me wondering, what key is too long? The longest is supposedly 255B, no?
It depends on your server default encoding. For instance, in UTF-8 you use three bytes for each character.
The issue comes from 'UNIQUE' constraint on VARCHAR(255) columns.
Here Try This out:-
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(255),
surname VARCHAR(255),
email VARCHAR(255) UNIQUE,
username VARCHAR(255) UNIQUE,
passwordHash CHAR(60),
admin BIT,
PRIMARY KEY (id),
CONSTRAINT user_info UNIQUE(email,username)
);
i think this will work
Related
complete novice to mysql but i'm having problems finding sources online that explain my problem. Essentially I need to add a constraint to the videoCode row where the identifier is a code in the form of XX##
where the X are letters and the # are numbers.
Any help would be greatly appreciated.
CREATE TABLE videoTable(
videoID INT,
channelID INT,
videoTitle VARCHAR(255) NOT NULL,
videoPostedDate DATE NOT NULL,
videoTags VARCHAR(255),
noViews INT NOT NULL,
videoCode VARCHAR(4) NOT NULL (ADD CONSTRAINT)
PRIMARY KEY (videoID),
FOREIGN KEY (channelID) REFERENCES channelTable(channelID)
);
Use a CHECK constraint:
CREATE TABLE videoTable(
videoID INT,
channelID INT,
videoTitle VARCHAR(255) NOT NULL,
videoPostedDate DATE NOT NULL,
videoTags VARCHAR(255),
noViews INT NOT NULL,
videoCode VARCHAR(4) NOT NULL CHECK(videoCode REGEXP '^[A-Za-z]{2}[0-9]+$')
PRIMARY KEY (videoID),
FOREIGN KEY (channelID) REFERENCES channelTable(channelID)
);
The regexp describes a string made of 2 alphabetic characters, followed by numbers only (at least one number must be present).
Note: this requires MySQL 8.0. In earlier versions, check constraints were parsed but ignored, and you would typically need a trigger for this.
Suppose i have a table customers having multiple fields. I am considering, three fields to be unique :date of birth, phone number, mail id. in order to create this table, the create script would be:
create table customers(
customer_id INT AUTO_INCREMENT,
first_name VARCHAR(255),
last_name VARCHAR(255),
address VARCHAR(255),
DOB datetime Not NULL,
phone_number int(10) NOT NULL,
email_address VARCHAR(255) NOT NULL,
PRIMARY KEY (customer_id),
UNIQUE KEY (customer_id,DOB,phone_number,email_address)
);
however i have noticed that unique key is declared with a name - UNIQUE KEY customer_id (customer_id,DOB,phone_number,email_address).
I need help in understanding why and when the unique constraint to be named, and what value would it add? Also what is the best practice for naming?
(Note:I do understand the concept of UNIQUE INDEX, but here i want to understand the naming of UNIQUE constraint)
I want to create a table in mysql:--
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT(1000),
primary key(app_id ),
FOREIGN KEY (user_id) REFERENCES student(sp_id));
where app_id is the primary key and sp_id is the foreign key reference from student table..
but I am getting error:--
ERROR 1064 (42000): 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 '(1000)0,primary key(app_id ),FOREIGN KEY (sp_id)
REFERENCE' at line 1
why the error is occuring?
Try to Run This one:-
use db;
CREATE TABLE parent (
id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
) ;
CREATE TABLE Persons
(
PID int UNSIGNED NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City longtext,
PRIMARY KEY (PID),
FOREIGN KEY (PID)
REFERENCES parent(id)
ON DELETE CASCADE
);
LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length of LONGTEXT columns also depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGTEXT value is stored using a 4-byte length prefix that indicates the number of bytes in the value.
You should user varchar(1000) in place of longtext(1000).
Moreover user_id column doesn't exist in you table.
You can´t specify the LONGTEXT in this context. Have a look in the manual.
Change your code to:
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT,
primary key(app_id),
FOREIGN KEY (user_id) REFERENCES student(sp_id));
Also, you shouldn't be using spaces inbetween the brackets at primary key(app_id).
I have attempted to create the following tables:
Customer:
CREATE TABLE Customer(customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
surname VARCHAR(15) NOT NULL,
forename VARCHAR(15) NOT NULL,
DOB TIMESTAMP NOT NULL,
phone_no VARCHAR(20) NOT NULL,
email_address VARCHAR(30),
postcode VARCHAR(15) NOT NULL,
PRIMARY KEY(customer_id)) ENGINE=INNODB;
When I attempt to create the 'Booking' table afterwards, I get an error
"ERROR 1005(HY000): can't create table 'Test.Booking' (errno:150)"
I checked the error online and found out that it was relating to the Foreign key connection that I attempted to make between the tables; which I will give below.
Booking:
CREATE TABLE Booking (booking_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
customer_id INT NOT NULL,
staying_from TIMESTAMP,
staying_until TIMESTAMP,
cost INT,
PRIMARY KEY(booking_id),
FOREIGN KEY(customer_id) REFERENCES Customer(customer_id)
ON UPDATE CASCADE ON DELETE RESTRICT) ENGINE=INNODB;
As documented under FOREIGN KEY Constraints:
Foreign keys definitions are subject to the following conditions:
[ deletia ]
Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.
In Customer you have INT UNSIGNED whereas in Booking you have INT.
When using jdbcrealm in Glassfish v3 how strictly must I follow the recommendations regarding tables? Currently I have the following setup:
CREATE TABLE roles (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
rolename VARCHAR(255) NOT NULL,
);
CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
username VARCHAR(255) PRIMARY KEY NOT NULL,
password VARCHAR(255) NOT NULL,
firstname VARCHAR(255),
lastname VARCHAR(255),
email VARCHAR(255),
status VARCHAR(255),
role_id INTEGER,
CONSTRAINT FOREIGN KEY(role_id) REFERENCES roles(id)
);
Is it possible to use this setup without changing anything to create a jdbcrealm or must I change my tables?
Thanks in advance!
Have you tried it? It seems ok. The strange thing with the jdbcRealm is that it expects an unnormalized database, one would like something more like:
user (userid, username, passw, ...)
security_group (security_groupid, name)
user_in_group (user_in_groupid, userid, security_groupid)
Wich is more normalized. However this setup does not work. But if you're like me and think this should work take a look at the lovely custom Flexible JDBC Realm. It worked for me.