I am learning about sql databases. I am using mysql.
I have designed the tables, and queries. I am now working on the code to put stuff into the database.
I can not work out how to ensure that a record is unique when I have a text field. I tried to mark the part of the record that was not the pk (primary key) as unique, but when it is text it complains that it is not fixed length. I then played with the idea of conditionals in a stored procedure, but could not get it to work.
DELIMITER $$
DROP PROCEDURE IF EXISTS `experiment1`.`add_zzzz`$$
CREATE PROCEDURE `experiment1`.`add_zzzz` (IN v INT, IN n TEXT)
BEGIN
IF EXISTS (
SELECT value, name
FROM zzzz
WHERE value=v AND name=n
)
THEN
ELSE
INSERT INTO zzzz(value,name)
VALUES v,n;
END IF;
END$$
DELIMITER ;
So anyone know what I am doing wrong?
VARCHAR is not fixed and you can use unique index with it
more on http://www.w3schools.com/sql/sql_unique.asp
you can also use INSERT ignore
http://dev.mysql.com/doc/refman/5.5/en/insert.html
Related
I am writing a stored procedure in mysql which simply returns the row with ID provided or return all table when no ID is provided.
CREATE DEFINER=`root`#`localhost` PROCEDURE `SLICE_GET`(`slice_id` int)
BEGIN
SELECT *
FROM `thesis_db`.`SLICE_INFO`
WHERE (SLICE_ID = `slice_id` OR `slice_id` IS NULL);
END
I have used the same idea in ms-sql for years yet it doesn't seem to work for mysql since no matter which ID is passed, the procedure returns entire table.
What am I missing here ?
This is a way to write procedures in mysql
DELIMITER $$
CREATE PROCEDURE `name of procedure` (x CHAR(1), D1 DATE, D2 DATE)
BEGIN
SELECT name of columns you want to display
FROM table name
WHERE SLICE_ID= x
OR SLICE_ID IS NULL;
END
$$
Note: Moreover mysql is not case sensitive means all caps or all small will not effect it.
delimiter is used to:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises.
By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
I'm attempting to create a procedure that is a basic insert into a table, and then performs a quick update on another table afterwards in MySQL. Please find the code below:
DROP PROCEDURE IF EXISTS `sp_insertUserSocial`
GO
CREATE PROCEDURE sp_insertUserSocial
(
IN p_userSocialID INT(11),
IN p_socialID INT(11),
IN p_userID INT(11),
IN p_referralID INT(11)
)
BEGIN
INSERT INTO UserSocial
(
userSocialID,
socialID,
userID,
referralID
)
VALUES
(
IN p_userSocialID,
IN p_socialID,
IN p_userID,
IN p_referralID
) ;
UPDATE Users
SET connCount = connCount + 1
WHERE UserID = p_referralID;
END
GO
In PHPAdmin it's giving me a syntax error, but I'm not sure where exactly it is? It says line 23, which makes me think it's the semi-colon but I thought that these were needed after an insert statement?
Any help is appreciated, thanks!
I see a couple of problems:
GO, must specify the DELIMITER. 19.1. Defining Stored Programs.
In the INSERT (13.2.5. INSERT Syntax), the IN is optional to pass parameters to the stored procedure (13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax), but not part of the syntax of the INSERT.
SQL Fiddle demo
I am trying to create a trigger (this is my first trigger, and question, so be gentle) that will insert new rows into two different tables.
* Edit *
Adding this in as I forgot to mention it until ypercube answered.
I am trying to avoid listing all of the column names, as in the real world usage the table this will be used on has a very large number of columns (not my design, too late to refactor).
* End Edit *
Here's what I have so far.
CREATE TABLE test_table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
message VARCHAR(255)
);
CREATE TABLE test_table_copy LIKE test_table;
DELIMITER $$
CREATE TRIGGER copy_test_table_data AFTER INSERT ON test_table
FOR EACH ROW
BEGIN
INSERT INTO test_table_copy SELECT * FROM NEW;
END;$$
DELIMITER ;
Unfortunately this results in an error.
mysql> INSERT INTO test_table VALUES (1, 'This is a message');
ERROR 1146 (4502): Table 'test_db.NEW' doesn't exist
I am not quite sure what the problem is, I thought NEW referred the table data was being inserted into?
You could possibly get a list of column names in that table from information_schema views, then use them to create a prepared statement (using cursor to iterate column names) and CONCAT() function to glue together the query string. Then execute the prepared statement.
Seems very contrived even to me, and I'm not sure it would work (and if it did, how efficient it would be)
Hi I have a MYSQL table like
PART{
part_id :long,Auto Increment
parent_part_id :long
root_part_id :long
}
I need to do Insert Query on this table where root_part_id should get value of part_id .
In Other cases I know the parent and root part Id . but while doing insertion of root part i need this query. I know there is a option that I should fire a update query after this insert query but I need a solution in a single Query.
You can use a trigger:
DELIMITER $$
CREATE TRIGGER `before_insert_PART`
BEFORE INSERT ON `PART` FOR EACH ROW
BEGIN
SET NEW.root_part_id = NEW.part_id;
END
$$
DELIMITER ;
PS, better use only small characters for your table names. It can lead to OS incompatibilities, on Linux names are case sensitive, and on Windows - not.
I have a table with names (names). I have another that holds ids (user).
I have a conditional construct - a case-when statement that is supposed to insert an id into the user table if a name in the names table matches a certain condition.
I have used like % to match string patterns:
delimiter //
create procedure name_matching (in names.name varchar, out id int)
begin
case
when names.name like 's%_%a' then
insert into user (id) values ('1');
else
insert into user (id) values ('2');
end case
end//
This outputs error 1064 on mysql terminal.
Is there a way to do this differently?
There are a couple of small problems with your procedure. You need a semicolon after the end case, you need to specify a field size for the varchar in the input parameter list, etc. The following works for me on MySQL 5.1 ("works" meaning: does not produce error when creating procedure):
delimiter $$
create procedure name_matching (in name varchar(500))
begin
case
when name like 's%_%a' then
insert into user (id) values (1);
else
insert into user (id) values (2);
end case;
end $$