简体   繁体   English

在mysql数据库中创建表

[英]creating tables in mysql database

i am trying to create two tables in mysql database via code 我正在尝试通过代码在mysql数据库中创建两个表

this is my creation script 这是我的创作脚本

CREATE DATABASE if not exists feedback;

CREATE TABLE IF NOT EXISTS Authorizations(
    authDesc CHAR(50),
    id MEDIUMINT NOT NULL AUTO_INCREMENT,
    authLevel INT NOT NULL DEFAULT 0,

    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS Users(
    lastName CHAR(50) NOT NULL,
    userID MEDIUMINT NOT NULL,
    phone CHAR(15),
    email CHAR(50),
    address CHAR(100),
    authLevel INT NOT NULL DEFAULT 0,

    FOREIGN KEY(authLevel) REFERENCES Authorizations (authLevel),
    login CHAR(30) NOT NULL,
    firstName CHAR(50) NOT NULL,

    PRIMARY KEY (login),password CHAR(30) NOT NULL
);

i am getting this error when trying to execute that 我在尝试执行该错误

java.sql.SQLException: Can't create table 'feedback.users' (errno: 150)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at com.database.Database.createDatabase(Database.java:45)
    at com.database.Main.main(Main.java:6)

line 45 has the create table users line in my code 第45行在我的代码中有create table users行

my database engine is InnoDB, via mysql 我的数据库引擎是通过MySQL的InnoDB

what am i doing wrong ? 我究竟做错了什么 ?

Your problem 你的问题

Code wise 明智的代码

You can only create a FOREIGN KEY constraint by referencing a column or set of columns which uniquely identify one single row in your destination table. 您只能通过引用唯一标识目标表中单个行的一列或一组列来创建FOREIGN KEY约束。

That means that @Ulrich was right you need an index but not any index you need an UNIQUE KEY constraint. 这意味着@Ulrich是正确的,您需要一个索引,但不需要任何索引就需要UNIQUE KEY约束。

Logical 合乎逻辑

You need to read up on a few things. 您需要阅读一些内容。 A direct solution like "add an index here" might silence the error but it may also restrict what you can do with those tales (due to the uniqueness constraint). 诸如“在此处添加索引”之类的直接解决方案可能会使错误静音,但也可能会限制您对这些故事的处理(由于唯一性约束)。

Your table structure may be the problem, and you may need to fully understand UNIQUE KEY an FOREIGN KEY before you can take a new approach to the table structure. 您的表结构可能是问题所在,您可能需要完全了解UNIQUE KEY和FOREIGN KEY,然后才能采用新的表结构方法。

MySQL background info about keys and indexes 有关键和索引的MySQL背景信息

Indexes 索引

When creating an indexed column the MySQL server stores some data about the content of that column which makes it easier for the server to find records which are filtered/sorted using that column. 创建索引列时,MySQL服务器会存储有关该列内容的一些数据,这使服务器可以更轻松地查找使用该列进行过滤/排序的记录。 So indexes represent a data structure stored separately by MySQL for easier reading. 因此,索引表示由MySQL单独存储的数据结构,以便于阅读。

If you know your application will attempt to search for records by a certain column (ex.: title, name, created_at, etc.) you may want to declare a simple INDEX or KEY to specify that you want easy lookup for those columns. 如果您知道您的应用程序将尝试按特定列(例如:标题,名称,created_at等)搜索记录,则可能需要声明一个简单的INDEXKEY以指定您希望对这些列进行轻松查找。 Direct selects will be faster, joins will be faster etc.. 直接选择将更快,联接将更快等。

The table will also use more memory because some extra data is stored somewhere regarding those columns. 该表还将使用更多的内存,因为一些额外的数据存储在与这些列有关的某处。

Also you might need to declare a multiple column INDEX in order to cover for ex. 另外,您可能需要声明多列INDEX才能覆盖ex。 the title and the content of a blog article. 博客文章的标题和内容。 INDEXES have limits to the amount of (string length) of data they can use so you'll sometimes have to declare an INDEX which uses just the first 512 (or some number) characters of the column(s). INDEXES限制了它们可以使用的数据量(字符串长度),因此有时您必须声明一个INDEX ,它仅使用列的前512个(或一些数字)字符。

Constraints 约束

When creating a constraint (a PRIMARY KEY , a UNIQUE KEY or a FOREIGN KEY ) the constraint needs to look up data in the table in order to enforce the rule (uniqueness or referenceability etc.). 创建约束( PRIMARY KEYUNIQUE KEYFOREIGN KEY )时,该约束需要在表中查找数据以实施规则(唯一性或可引用性等)。

Because of that, these constraints will also need an INDEX . 因此,这些约束也将需要INDEX Every time you declare a column as PRIMARY KEY or as UNIQUE KEY an INDEX is also created, so you don't have to create it yourself. 每次将列声明为PRIMARY KEYUNIQUE KEY ,也会创建一个INDEX ,因此您不必自己创建它。

When declaring a FOREIGN KEY the index that needs to be created should be in the referenced table not in the current table. 当声明一个FOREIGN KEY ,需要创建的索引应该在被引用的表中而不是当前表中。 Because of that you have to create it yourself. 因此,您必须自己创建它。

You cannot reference with a foreign key a field of an other table that is not primary key. 您不能用外键引用不是主键的另一个表的字段。

Use instead Authorization.id as a field to link to your foreign key. 而是使用Authorization.id作为字段来链接到您的外键。

Then if you really need to know about authLevel in users table, join the two tables. 然后,如果您真的需要了解users表中的authLevel,请将两个表连接起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM