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.
Related
this is my code for table
CREATE TABLE [dbo].[product] (
[prod_Id] NUMERIC (5) NULL,
[barcode] NUMERIC (13) NULL,
[prod_name] NVARCHAR (50) NULL,
[price] FLOAT (53) NULL,
[stock] NUMERIC (18) NULL,
[cat_id] NCHAR (10) NULL,
[discount] FLOAT (53) NULL,
PRIMARY KEY CLUSTERED ([prod_Id],[barcode] ASC)
);
I have 2 primary keys prod_id and barcode. I want to make them null as user have choice either to enter prod_id or barcode. Both will not have value at same time. Now please anyone help me how to do it. I tried to make them null it gives syntax error.
Replace primary key with unique
unique CLUSTERED ([prod_Id],[barcode] ASC)
(or)
Give some default value to the columns that are there in the composite primary key.
CREATE TABLE [dbo].[product] (
[prod_Id] NUMERIC (5) default 0,
[barcode] NUMERIC (13) default 0,
[prod_name] NVARCHAR (50) NULL,
[price] FLOAT (53) NULL,
[stock] NUMERIC (18) NULL,
[cat_id] NCHAR (10) NULL,
[discount] FLOAT (53) NULL,
PRIMARY KEY CLUSTERED ([prod_Id],[barcode] ASC)
);
If you are using C# and sending data from C# to SQL server then then you can have validation in place to check if 1 is null then other one must be not null and throw exception/error from there and make both the columns in DB as Unique not primary key. It might help.
In any database table, there is one and only one primary key and that primary key should not be null.
In your case, for each prod_id there will be unique barcode and vise versa. So, it won't make any sense making any one of this as NULL.
instead create 2 table, one containing table_id and other containing barcode and then refer one in another using foreign_key.
Ran into a small problem while working on EF 6 Database first approach.
When I try to insert a value into the key field of a table which is not set to be the Identity, I receive the following exception:
Cannot insert the value NULL into column 'emp_main_id',column does not allow nulls. INSERT fails.
The statement has been terminated.
I added the following code in the entity class to avoid the error
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int emp_main_id { get; set; }
but still could not get it working.
Below is the script to create the db table
CREATE TABLE [dbo].[EmployeesMain] (
[emp_main_id] INT NOT NULL,
[emp_main_first] NVARCHAR (40) NOT NULL,
[emp_main_middle] NVARCHAR (40) NULL,
[emp_main_last] NVARCHAR (40) NOT NULL,
[emp_main_dob] DATE NOT NULL,
[emp_main_gender] INT DEFAULT ((1)) NOT NULL,
[emp_main_type] INT DEFAULT ((0)) NOT NULL,
[emp_main_email] NVARCHAR (150) NOT NULL,
[emp_main_password] NVARCHAR (MAX) NOT NULL,
[emp_main_designation] NVARCHAR (150) NULL,
[emp_main_skill] NVARCHAR (MAX) NULL,
[emp_main_contract] INT DEFAULT ((0)) NOT NULL,
[emp_main_lead] NVARCHAR (81) NULL,
[emp_main_img] NVARCHAR (MAX) NULL,
[emp_main_doj] DATE DEFAULT ('01-JAN-2012') NULL,
CONSTRAINT [emp_main_primary_key] PRIMARY KEY ([emp_main_id])
);
Any help on this will be appreciated.
Problem is you are trying to insert NULL value to a Primary Key
column i.e emp_main_id.
Primary Key doesn't allow to insert NULL values or duplicate values.
If you want to insert NULL values remove Primary Key constraint.
Decided to write the answer myself.
The problem was, since I had set the field as an identity earlier, the values for the field were being generated automatically. Even after removing the identity and updating the model from the database, this was not reset.
So what I had to do was,
Go to the .edmx file.
Right click on the property "emp_main_id"
Click on Properties
Set StoreGeneratedPatter to None
Save the model and rebuild the solution
That's it. it gives me a non-identity key, with all the other constraints along with the option to explicitly enter a value for the field. Peace :)
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'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.
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