外键显示空值

时间:2016-10-18 10:07:15

标签: mysql

我正在尝试显示从另一个表中提取的外键值。 我使用的代码:

第一张表的代码:

CREATE TABLE trip (
    tripID  INT(10) PRIMARY KEY,
    startTime   TIME,
    endTime     TIME,
    tripDate    DATE,
    price       VARCHAR(5),
    databaseID  INT(10),
    FOREIGN KEY (databaseID)
       REFERENCES databasestorage(databaseID)
);

第二个表的代码:

CREATE TABLE databasestorage (
    databaseID  INT(10) PRIMARY KEY,                    
    location        VARCHAR(40)
);

将值插入两个表后,在trip表中,databaseID仍显示为null,而其他所有列都正确。如何使databaseID表格中的trip显示databaseID表中databasestorage的值?

2 个答案:

答案 0 :(得分:1)

您需要传递trip表的主键,同时将数据插入INSERT INTO databasestorage(databaseID, location) VALUES('1021', 'Caulfield') 表吗?

databaseID

对于旅行表,您需要明确传递旅行表中 ---------v INSERT INTO trip(tripID, startTime, endTime, tripDate, price,databaseID) VALUES('101', '20:20:00', '20:40:00', '2016-10-22', '$5.25','1021') -----^ 列的值,外键不会自动插入

import UIKit

public struct Pixel {
public var value: UInt32

public var red: UInt8 {
    get {
        return UInt8(value & 0xFF)
    }
    set {
        value = UInt32(newValue) | (value & 0xFFFFFF00)
    }
}

public var green: UInt8 {
    get {
        return UInt8((value >> 8) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
    }
}

public var blue: UInt8 {
    get {
        return UInt8((value >> 16) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
    }
}

public var alpha: UInt8 {
    get {
        return UInt8((value >> 24) & 0xFF)
    }
    set {
        value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
    }
}

public struct RGBAImage {
public var pixels: [Pixel]

public var width: Int
public var height: Int

public init?(image: UIImage) {
    guard let cgImage = image.cgImage else { return nil }

    // Redraw image for correct pixel format
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue

    width = Int(image.size.width)
    height = Int(image.size.height)
    let bytesPerRow = width * 4

    let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: width * height)

    guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
    CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)

    let bufferPointer = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
    pixels = Array(bufferPointer)

    imageData.deinitialize()
    imageData.deallocate(capacity: width * height)
}

public func toUIImage() -> UIImage? {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue

    let bytesPerRow = width * 4

    let imageDataReference = UnsafeMutablePointer<Pixel>(mutating: pixels)
    defer {
        imageDataReference.deinitialize()
    }
    let imageContext = CGContext(data: imageDataReference, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil)

    guard let cgImage = imageContext!.makeImage() else {return nil}
    let image = UIImage(cgImage: cgImage)

    return image
}

答案 1 :(得分:0)

试试这个;)

auto_increment添加到表定义:

首先:

CREATE TABLE databasestorage (
    databaseID  INT(10) PRIMARY KEY auto_increment,
    location        VARCHAR(40)
);

第二名:

CREATE TABLE trip (
    tripID  INT(10) PRIMARY KEY auto_increment,
    startTime   TIME,
    endTime     TIME,
    tripDate    DATE,
    price       VARCHAR(5),
    databaseID  INT(10),
    FOREIGN KEY (databaseID)
        REFERENCES databasestorage(databaseID)
);

ADDITION (插入查询)

INSERT INTO trip(tripID, startTime, endTime, tripDate, price) VALUES('101', '20:20:00', '20:40:00', '2016-10-22', '$5.25');

在此查询中,无需传递tripID auto_increment字段的值,我认为您将databaseID值传递给tripID。因此,databasestorage.databaseID插入后您获得的值应该在trip.databaseID

中传递

所以最后的查询是:

INSERT INTO trip(startTime, endTime, tripDate, price, databaseID) VALUES('20:20:00', '20:40:00', '2016-10-22', '$5.25', 101);

由于trip.databaseIDINT字段,因此无需添加引号('');