foreign-keys

Keep getting the error TypeError: function takes at most 2 arguments (3 given)

帅比萌擦擦* 提交于 2019-12-02 21:13:42
问题 I am currently working on my coursework project for college which involves a quiz which stores all background data in a database. With the addition of foreign keys, i have tried to find a way to merge the data from the foreign key between two tables. For example , The users table stores user data and their UserID. The data table will store information about the level of the quiz,etc along with that specific user id. How would that information be automatically updated from the two tables when

MySQL with Soft-Deletion, Unique Key and Foreign Key Constraints

好久不见. 提交于 2019-12-02 20:47:15
Say I have two tables, user and comment . They have table definitions that look like this: CREATE TABLE `user` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, `deleted` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY (`username`) ) ENGINE=InnoDB; CREATE TABLE `comment` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `user_id` INTEGER NOT NULL, `comment` TEXT, `deleted` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), CONSTRAINT `fk_comment_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB; This is

A way to check if foreign key exists in SQL 2005

江枫思渺然 提交于 2019-12-02 18:46:46
Is there an easy way to check if a foreign key exists for a column in a table? I am writing a script which will add the foreign key only if it does not exist. CodeLikeBeaker You can use this script: IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_NAME]') AND parent_object_id = OBJECT_ID(N'[dbo].[MyTable]')) BEGIN -- do stuff END This can be done if you expand out the table and right click on an existing FK and choose script key as "DROP TO" and then you will get a generated script from SQL. ristonj Woo-hoo! I just spent the past two days doing this. IF NOT

Can't create table (errno: 150) on FOREIGN KEY

删除回忆录丶 提交于 2019-12-02 17:40:36
问题 I saw a lot of same question but I couldn't solve my case. If I run this code: <?php include_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); $servername = HOST; $username = USERNAME; $password = PASSWORD; $dbname = DB; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // sql to create table $sql = "CREATE TABLE IF NOT EXISTS Articls ( id INT(10) UNSIGNED AUTO

MySQL: How do I find out which tables reference a specific table?

限于喜欢 提交于 2019-12-02 17:30:18
I want to drop a table but it is referenced by one or more other tables. How can I find out which tables are referencing this table without having to look at each of the tables in the database one by one? select table_name from information_schema.KEY_COLUMN_USAGE where table_schema = 'my_database' and referenced_table_name = 'my_table_here'; This works. select table_name from information_schema.referential_constraints where referenced_table_name = 'parent table here'; If you have phpMyAdmin installed, you can use its designer feature to visualize table relationships. To use the designer,

PostgreSQL: How to index all foreign keys?

房东的猫 提交于 2019-12-02 16:50:33
I am working with a large PostgreSQL database, and I am trying to tune it to get more performance. Our queries and updates seem to be doing a lot of lookups using foreign keys. What I would like is a relatively simple way to add Indexes to all of our foreign keys without having to go through every table (~140) and doing it manually. In researching this, I've come to find that there is no way to have Postgres do this for you automatically (like MySQL does), but I would be happy to hear otherwise there, too. EDIT : so, I wrote the query below and then thought... "hang on, Postgresql requires

Primary & Foreign Keys in pgAdmin

给你一囗甜甜゛ 提交于 2019-12-02 16:07:13
I was wondering can some give me an explanation on how to assign primary and foreign keys in pgAdmin? I can't find any information online. For example...I've got a Student table with all their details (address, d.o.b. and etc.). I'm going to add a student_number to the table and make it a primary key. I just want to know how do I do that using pgAdmin? And if you may be kind to explain give me further information on using Primary Keys in postgreSQL (and pgAdmin). The same case with the foreign keys. There is no option in pgAdmin to add a column to an existing table and make it the primary key

ERRO 1215. MySql InnoDB

泪湿孤枕 提交于 2019-12-02 14:34:45
问题 Executing: CREATE TABLE `calls`.`called` ( `date` DATETIME NULL, `rate` VARCHAR(10) NULL, `duration` TIME NULL, `Name` VARCHAR(20) NOT NULL, `Code` VARCHAR(10) NOT NULL, `Number` VARCHAR(10) NOT NULL, PRIMARY KEY (`Name`, `Code`, `Number`), INDEX `Code_idx` (`Code` ASC), INDEX `Number_idx` (`Number` ASC), CONSTRAINT `Name` FOREIGN KEY (`Name`) REFERENCES `calls`.`city` (`Name`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `Code` FOREIGN KEY (`Code`) REFERENCES `calls`.`city` (`Code`) ON

SQL error: “name already used by an existing constraint”

拟墨画扇 提交于 2019-12-02 13:34:15
I'm trying to create some tables and setup the foreign keys but I keep encountering problems with the foreign keys. Earlier on I created the below table and it works fine CREATE TABLE inpatient (PatientNo varchar(6) NOT NULL, WardNo number(2), BedNo number(3) NOT NULL, OnWaitingList date, WardRequired varchar(25), ExpectStayInDays number(4), DatePlaced date, DateLeave date, ActualLeave date, constraint PatientFK foreign key (PatientNo) references Patient (patientNo), constraint bedFK foreign key (BedNo) references Bed (bedNo)); Notice the use of patientFK on 2nd last line. then I went on to

Foreign key constraints while dumping data

馋奶兔 提交于 2019-12-02 13:18:40
mysqldump --compact --no-create-info -h192.168.150.180 -uroot -p live pnlbus_stops | sed s/pnlbus_stops/bus_stops/g | mysql test I am getting an error: ERROR 1062 (23000) at line 1: Duplicate entry 'AN' for key 1 This is because bus_stops table in the test DB has foreign key constraints. How do I truncate the bus_stops table from test database in a SINGLE STATEMENT before inserting from "live" DB? put set FOREIGN_KEY_CHECKS = 0; at the top of your dump file and put SET FOREIGN_KEY_CHECKS = 1; at the bottom of your dump file 来源: https://stackoverflow.com/questions/1382583/foreign-key