I need to create a table in MySQL that will have indexes for each row , and when I insert records they will be inserted at the tail of the table.
And I will be able to make queries by row numbers.
Can you give me an example how to do so ?
Thanks for help.
This is how you create a table:
CREATE TABLE `symbols` (
`id` int(11) NOT NULL auto_increment,
`country` varchar(255) NOT NULL default '',
`animal` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
Read more on mysql here and here
Related
Unable to create table via
CREATE TABLE AAA (
[Percentage] Number(15),
[ID] AutoIncrement CONSTRAINT PRIMARY KEY ,
[CaseRecNo] Number(15),
[CaseName] Text,
[Amount] Currency
)
Gives Syntax Error,
How to set Number filed type double ?
The field size is a double by default. Create this table in access and you will see the result. You also need to name your constraint
CREATE TABLE AAAB (
[Percentage] NUMBER,
[ID] AutoIncrement CONSTRAINT MyFieldConstraint PRIMARY KEY ,
[CaseRecNo] Number,
[CaseName] Text,
[Amount] Currency
)
I have two tables , one to one relationship i makes DetailsID of projectDetails FK in ID of projects table:
projects:
ID, //has FK With DetailsID in Details table & auto identity (1,1)
ProjectName,
Areas,
PaymentSystem,
ReceivedDate,
PropertyClassification,
ProjectImage
ProjectDetails:
DetailsID ,auto identity ( 1,1)
ProjectDetailName,
ProjectDetailImage
I am trying to insert new record in projects table , gives me this error at this line of code :
con.Open();
comm.ExecuteNonQuery(); // when execute
System.Data.SqlClient.SqlException: 'The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Projects_ProjectDetails". The conflict occurred in database "AlamaarRealEstate", table "dbo.ProjectDetails", column 'DetailsID'.
and this is my stored to insert :
ALTER proc [Insert_Project]
#Projectname NVARCHAR(MAX) ,
#areas NVARCHAR(MAX) ,
#Paymentsystem NVARCHAR(MAX) ,
#Receiveddate date ,
#Classification NVARCHAR(MAX) ,
#Projectimage Nvarchar(MAX)
as
INSERT INTO dbo.Projects
(
ProjectName,
Areas,
PaymentSystem,
ReceivedDate,
PropertyClassification,
ProjectImage
)
VALUES
(
#Projectname ,
#areas,
#Paymentsystem ,
#Receiveddate ,
#Classification,
#Projectimage
)
The question explains the answer. Referential Integrity is not maintained properly, you are trying to insert into a child table for which the master value does not exist. Please insert values to Project_details first. This will resolve your issue. If you did not what this to throw an error, just check the existence of the DetailID in Projects table before inserting.
Without more detail your question is hard to answer. For instance in your procedure you do explicit column naming for inserts and LEAVE OUT the column you are having a problem with. So if the proc was wrong it would be bombing with your example foreign key constraint error. But you are not even listing the 'ID' field to insert into with the procedure. So this is common if you are using an 'Identity' field to self seed, but you are claiming it is a foreign key. So like others have commented, without more code to show the exact way your tables are made it's hard to guess. Here is a self extracting example you could run that shows if a column is nullable and I had a key constraint it would work. Without the exact code of the tables as well as the proc it is hard to tell. What you gave is pseudo code.
USE Tester --just a test database I have, you can use whatever database you want
GO
IF OBJECT_ID('Projects') IS NOT NULL
DROP TABLE Projects
IF OBJECT_ID('ProjectDetails') IS NOT NULL
DROP TABLE ProjectDetails
create TABLE ProjectDetails
(
DetailsID INT CONSTRAINT PK_DetailsId PRIMARY KEY,
ProjectDetailName VARCHAR(32)
)
CREATE TABLE Projects
(
Id INT CONSTRAINT FK_Projects_ProjectDetails FOREIGN KEY (Id) REFERENCES ProjectDetails(DetailsId),
ProjectName varchar(32)
)
GO
IF OBJECT_ID('Insert_Project') IS NOT NULL
DROP PROC Insert_Project
GO
Create proc Insert_Project
#Projectname NVARCHAR(MAX)
as
INSERT INTO dbo.Projects ( ProjectName )
VALUES ( #Projectname )
GO
Select *
From dbo.Projects
EXEC dbo.Insert_Project #Projectname = N'Test' -- nvarchar(max)
Select *
From dbo.Projects
I Create two sample table and create both tables inserted procedure in MySql.I listed the both procedure and table script below. Now i try to add the procedures using DataAdapter. First Procedure add successfully.But the second procedure return the error.Cannot add or update a child row : a foreign key constraint fails.
CREATE TABLE `hd_test` (
`FLD_ID` int(11) NOT NULL AUTO_INCREMENT,
`FLD_NAME` varchar(20) DEFAULT NULL,
UNIQUE KEY `FLD_ID` (`FLD_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `hd_test1` (
`FLD_ID` int(11) NOT NULL DEFAULT '0',
`FLD_NAME` varchar(20) DEFAULT NULL,
`FLD_PARENT` int(11) DEFAULT NULL,
KEY `FLD_PARENT` (`FLD_PARENT`),
CONSTRAINT `hd_test1_ibfk_1` FOREIGN KEY (`FLD_PARENT`) REFERENCES `hd_test` (`FLD_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP PROCEDURE IF EXISTS db_helpdesk.SP_HD_TEST;
CREATE PROCEDURE db_helpdesk.`SP_HD_TEST`(
IN NAME VARCHAR(20))
BEGIN
INSERT INTO HD_TEST(FLD_NAME)VALUES(NAME);
END;
DROP PROCEDURE IF EXISTS db_helpdesk.SP_HD_TEST1;
CREATE PROCEDURE db_helpdesk.`SP_HD_TEST1`(
IN NAME VARCHAR(20),
IN PARENT INTEGER)
BEGIN
INSERT INTO HD_TEST1(FLD_NAME,FLD_PARENT)VALUES(NAME,PARENT);
END;
Error
Cannot add or update a child row : a foreign key constraint fails..
Please help me to fix this error.
I found in stackoverflow a link who explains this question. I wrote a procedure who does that but the retreived data is not as it's expected by mysql
Here is data that I receive using procedure (this gives an error when I use it for creating a table in this form, the error is syntax error near id int(11))
CREATE TABLE IF NOT EXISTS `atelier`(
`id` int(11) NO PRI auto_increment
`nom` varchar(30) NO UNI
`at_date` date NO
`pax` int(11) NO
`prix` double NO
`vente` double YES
`note` varchar(200) YES
`cr_user` varchar(15) NO
`cr_date` datetime NO
`up_user` varchar(15) YES
`up_date` datetime YES
And here is the data given by phpmyadmin
CREATE TABLE `Atelier` (
`id` int(11) NOT NULL auto_increment,
`nom` varchar(30) NOT NULL,
`date` date NOT NULL,
`pax` int(11) NOT NULL,
`prix` double NOT NULL,
`vente` double default NULL,
`note` varchar(200) default NULL,
`cr_user` varchar(15) NOT NULL,
`cr_date` datetime NOT NULL,
`up_user` varchar(15) default NULL,
`up_date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=Innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
What I want to know should I do translation of YES AND NO within my program or there is a way to obtain it with a query, and with wich query I can obtain info of two last lines given by phpmyadmin.(I think they are usefull for the choose of ENGINE and define the unique and primary keys)
PS:Using c#, and names of the tables I get from another procedure that exist in my program. I don't get it with DESCRIBE. What I receive is between id and up_date.
Here is the link of my procedures MY PROCEDURES
You didn't show the procedure generating this output, but if you replace DESCRIBE in there with SHOW CREATE TABLE I suppose it will work.
I'm trying to add a row into a mysql database table using visual studio 2010 and I haven't had problems until now. I keep getting the error:
"Fatal error encountered during command execution."
Table structure:
delimiter $$
CREATE TABLE `magazine_images` (
`image_id` int(11) NOT NULL AUTO_INCREMENT,
`MagazineKey` int(5) DEFAULT NULL,
`image_type` varchar(45) NOT NULL,
`image` blob,
`image_size` varchar(45) NOT NULL,
`image_ctgy` varchar(45) NOT NULL,
`image_name` varchar(50) NOT NULL,
PRIMARY KEY (`image_id`),
KEY `image_magazine` (`MagazineKey`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$
C Sharp query:
string image = "INSERT INTO magazine_images (MagazineKey, image, image_type, image_size, image_ctgy, image_name) " +
"VALUES(#magkey, #image, #image_type, #image_size, #image_ctgy, #image_name)";
this.cmd = new MySqlCommand(putImage, this.con);
this.read = this.cmd.ExecuteReader();
this.read.Read();
this.cmd.Parameters.AddWithValue("#magkey", int.Parse(labelKey.Text));
this.cmd.Parameters.AddWithValue("#image", textBoxImageFile.Text);
this.cmd.Parameters.AddWithValue("#image_name", textBoxImageName.Text);
this.cmd.Parameters.AddWithValue("#image_type", comboBoxImageType.Text);
this.cmd.Parameters.AddWithValue("#image_ctgy", textBoxImageCategory.Text);
this.cmd.Parameters.AddWithValue("#image_size", labelImageSize.Text);
this.read.Close();
Either your code is incomplete or you are currently adding your query parameters after you execute the query - you have to do it before the query executes.
Also you are not even using the image variable - you are using putImage which you haven't shown. Thirdly you should not use ExecuteReader() - this is for retrieving only, but you are doing an insertion, for that you need ExecuteNonQuery().
In summary: Either you have a lot of copy & paste errors or your sample does not reflect your real code.