Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL FOREIGN KEY USAGE ? what does it really do ? and when it was needed?
    primarykey
    data
    text
    <p>ok im makeing a simple database for my example, there is users data and the user company's data .</p> <pre><code>CREATE TABLE `users` ( `UID` INT(25) NOT NULL AUTO_INCREMENT , `username` VARCHAR(60) NOT NULL , `password` VARCHAR(100) NOT NULL , `ownername` VARCHAR(150) NOT NULL , `userstatus` TINYINT(1) NOT NULL , `userregistertime` DATETIME NOT NULL , `userlastonline` DATETIME NOT NULL , PRIMARY KEY (`UID`) , INDEX `username` (`username` ASC) ) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; CREATE TABLE `company` ( `CID` INT(25) NOT NULL AUTO_INCREMENT , `UID` INT(25) NOT NULL , `companyname` VARCHAR(60) NOT NULL , `companyaddress` VARCHAR(255) NOT NULL , `companyemail` VARCHAR(255) NULL DEFAULT NULL , `companyphone` VARCHAR(20) NOT NULL , `companyimage` VARCHAR(255) NULL DEFAULT NULL , `companyyahoo` VARCHAR(255) NULL DEFAULT NULL , `companytwitter` VARCHAR(255) NULL DEFAULT NULL , `companykaskus` VARCHAR(255) NULL DEFAULT NULL , `companyfacebook` VARCHAR(255) NULL DEFAULT NULL , `companytype` TINYINT(1) NOT NULL DEFAULT '0' , `companystatus` TEXT NULL DEFAULT NULL , `companytemplate` TEXT NULL DEFAULT NULL , `companyintroduction` TEXT NULL DEFAULT NULL , `partnership` TINYINT(1) NOT NULL DEFAULT '0' , PRIMARY KEY (`CID`) , INDEX `ownername` (`UID` ASC) , INDEX `companyname` (`companyname` ASC) , CONSTRAINT `ownernamecompany` FOREIGN KEY (`UID` ) REFERENCES `users` (`UID` ) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; </code></pre> <p>1.why after i insert data to the users table ( uid is auto increment ) it doesnt update my company UID table ?</p> <p>if you have php knowledge please see how im inserting it.</p> <pre><code>$RegisterInsert1 = $dbConnect-&gt;prepare("INSERT INTO users ( `username`, `password`, `ownername`, `userregistertime`, `userlastonline`) VALUES ( :username, :password, :ownername, :userregistertime, :userlastonline)"); $RegisterInsert1-&gt;execute($RegisterData1); </code></pre> <p>as you see, i get, UID = 1 ( auto ) then username,pasword,ownername,etc inserted on user table. but somehow my company UID is not updated. it should be UID = 1 then the rest CID ( auto ) companyname null, etc null.</p> <p>2.am i right defining a foreign key usage for?</p> <p>3.please give me the best example of inserting users data + company data with the right usage from the right usage of foreign.</p> <p>how im doing it right now</p> <pre><code>// INSERT USERS DB $RegisterInsert1 = $dbConnect-&gt;prepare("INSERT INTO users ( `username`, `password`, `ownername`, `userregistertime`, `userlastonline`) VALUES ( :username, :password, :ownername, :userregistertime, :userlastonline)"); $RegisterInsert1-&gt;execute($RegisterData1); // GET USERS GIVEN AUTO GENERATED UID // QUESTION ? THIS one should be automated by foreign useage ? $GetUid = $dbConnect-&gt;prepare("SELECT UID FROM users WHERE username = :username"); $GetUid-&gt;execute($RegisterData3); $UserID = $GetUid-&gt;fetch(); $RegisterData2['UID'] = $UserID; // INSERT COMPANY INFO + UID $RegisterInsert2 = $dbConnect-&gt;prepare("INSERT INTO company ( `UID`,`companyphone`,`partnership`) VALUES ( :UID, :companyphone, :partnership)"); $RegisterInsert2-&gt;execute($RegisterData2); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload