Consider the following tables for a LMS:
Item (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
ConnectLog BIGINT NOT NULL,
ItemClass BIGINT NOT NULL,
ItemType BIGINT NOT NULL,
AcqDate TIMESTAMP NOT NULL DEFAULT NOW(),
Vendor BIGINT NOT NULL,
Cost DECIMAL(64,2) NOT NULL DEFAULT '0.00',
Image VARCHAR(255),
Access INTEGER NOT NULL,
Notes VARCHAR(255),
PRIMARY KEY (ID)
)
Book (
ID BIGINT NOT NULL UNIQUE AUTO_INCREMENT,
Item BIGINT NOT NULL UNIQUE,
ISBN BIGINT,
Title VARCHAR(255) NOT NULL,
Authors VARCHAR(255),
Publisher VARCHAR(255),
DDC VARCHAR(255),
PubDate DATETIME,
Edition VARCHAR(255),
BookCase VARCHAR(255),
Shelf VARCHAR(255),
PRIMARY KEY (ID)
)
Now when a user makes an entry for Book, first an entry for Item has to be created first. But i need to find the ID for the Item entry that was created so i can use that value for Item in the Book table...
How? :/
Use mysql_insert_id()
// Create Entry
$sql = "INSERT INTO TABLE () VALUES()";
mysql_query($sql);
$id = mysql_insert_id();
// Create Book
$sql = "INSERT INTO TABLE (`Item_ID`) VALUES(".$id.")";
mysql_query($sql);
I bet there is a MySQL Command you could use to do it in a single query. But, this works.
According to this link, you could do an SQL query like:
INSERT INTO foo (auto,text) VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'text'); # use ID in second table
I haven't tested it, but it seems as though LAST_INSERT_ID() contains the last inserted ID, no matter what table it was inserted into.
If you're worried about heavy loads, and LAST_INSERT_ID() not containing the appropriate entry's ID, you could wrap these SQL statements in a transaction.
Related
How to generate unique tag_id start from F-00001 when insert date into item table using php and mysql.
This is my my table. I have three columns in my database table using MySQL.
CREATE TABLE item (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
tag_id VARCHAR(8) NOT NULL
)
As #mickmackusa points out, you don't need to have this field in your database as you can automatically generate it from your auto-increment id value. There are a number of ways to do this.
Use a generated column (MySQL 5.7 or later):
CREATE TABLE item (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
tag_id VARCHAR(8) AS CONCAT('F-', LPAD(id, 5, '0'))
)
Use a view:
CREATE VIEW item_view AS
SELECT *, CONCAT('F-', LPAD(id, 5, '0')) AS tag_id FROM item
Generate in PHP using str_pad:
// assume $row is an associative array of row of table
$tag_id = 'F-' . str_pad($row['id'], 5, '0', STR_PAD_LEFT);
I am creating a MySQL database and this doesn't seem to work, i was able to create a review table but now i'm trying to drop that table and create a reviews table but it doesn't seem to work. Please can someone take a look at this and help me check to see what's wrong here?
$reviewsTable = "CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (website)
)";
$drop = "DROP TABLE review";
mysqli_query($connect,$drop);
mysqli_query($connect,$reviewsTable);
Just use if exists to drop the table if there is one then create your table.
Id has to be primary key because of the auto increment. all auto increments have to be primary key. You can index website though. but i set id as primary key below this should help.
$reviewsTable = "
DROP TABLE IF EXISTS review;
CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (ID)
)";
The exact error I keep seeing is:
Key column 'alarmID' doesn't exist in table
alarmID is my primary key field.
Here is the code I have:
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alarmID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
Note: I am coding in PHP.
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
alaramID
The primary key in your table is alaramID and note the error its alarmID.So correct the spelling in the query like this
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
I have set up four additional tables for my plugin to use what I am trying to do is take a name and assign it a ID then use this data to populate drop down menus with a name and the same for class and position I am unsure as to how to do this correctly this is what i have so far.
$sql = "CREATE TABLE $tableName (
recordID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(recordID),
driverID int,
driverName varchar(30),
classID int,
driverClass varchar(20),
posID,
driverPosition varchar(6),
trackName varchar(30),
raceDate date
);";
$sql = "CREATE TABLE $driverTableName (
driverID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(driverID),
driverName varchar(30)
);";
$sql = "CREATE TABLE $classTableName (
classID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(classID),
className varchar (20)
);";
$sql = "CREATE TABLE $posTableName (
posID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(posID),
posName varchar(6)
);";
The bottom three tables will store the data I want to populate the drop down boxes to create a record with I am unsure as to how to link them to the top table where this record will be stored.
This is pretty much a indexing issue. If you are going to access the database separately to the standard calls that Wordpress provides, you should at the very least use http://codex.wordpress.org/Class_Reference/wpdb as it will save you some coding time.
The rest of it is a MySQL question. (Assuming you are using MySQL) In how to properly index the data together and then parsing the data as it comes in.
I got this create table statement;
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100),
`old_term` varchar(255),
`new_term` varchar(100),
`same_as` varchar(100),
`last_access` datetime)";
Is there a way to put comments into this statement to the same effect as follows?
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), //sample value services.media
`old_term` varchar(255), // something like "services.media>category:perspective"
`new_term` varchar(100), // something like "opinion"
`same_as` varchar(100), // the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place
Try following SQL comment syntax, but be careful with " in your text
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), -- sample value services.media
`old_term` varchar(255), -- something like "services.media>category:perspective"
`new_term` varchar(100), -- something like "opinion"
`same_as` varchar(100), -- the seed id of another record or another "old_term" from this table
`last_update` datetime)"; // when the last update took place
Read more...
you would have to use sql comments in the lines your still in the sql statement. for mysql this would be:
$sql = "CREATE TABLE TermsFinal(
`seed` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`source` varchar(100), /* sample value services.media */
`old_term` varchar(255), /* something like "services.media>category:perspective" */
`new_term` varchar(100), /* something like "opinion" */
`same_as` varchar(100), /* the seed id of another record or another "old_term" from this table */
`last_update` datetime)"; // when the last update took place