I have a script that drops a load of tables using DROP TABLE IF EXISTS, this works. There is also a delete in this script to DELETE a row from another table that I do not manage. This table may or …
Tag: mysql
MySQL comparison with null value
I have a column called CODE in a MySQL table which can be NULL. Say I have some rows with CODE=’C’ which I want to ignore in my select result set. I can have either CODE=NULL or CODE!=’C’ in my result …
Difference between primary key and unique key
I’m using a MySQL database. In which situations should I create a unique key or a primary key? Answer Primary Key: There can only be one primary key constraint in a table In some DBMS it cannot be NULL – e.g. MySQL adds NOT NULL Primary Key is a unique key identifier of the record Unique Key: Can be more
Prevent SQL injection attacks in a Java program
I have to add a statement to my java program to update a database table: String insert = “INSERT INTO customer(name,address,email) VALUES(‘” + name + “‘,'” + addre + “‘,'” + email + “‘);”; I …
Difference between SQL JOIN and querying from two tables
What is the difference between the query and this one Answer There is a small difference in syntax, but both queries are doing a join on the P_Id fields of the respective tables. In your second example, this is an implicit join, which you are constraining in your WHERE clause to the P_Id fields of both tables. The join is
Search between two SQL tables?
I have two tables: student Columns first_name last_name teacher_accounts Columns fname lname I have an input form that takes the data entered into an input field, and passes it through the …
I need to find and replace n in a mysql field
i’ve got data like this: 1 streetn2streetnmycitynmytown What i want to do is replace n with char(10) as i need a real linebreak in the db field. I’ve got: UPDATE data set `value` = REPLACE(`…
How to copy a row and insert in same table with a autoincrement field in MySQL?
In MySQL I am trying to copy a row with an autoincrement column ID=1 and insert the data into same table as a new row with column ID=2. How can I do this in a single query? Answer Use INSERT … SELECT: where c1, c2, … are all the columns except id. If you want to explicitly insert with an
How to calculate the slope in SQL
I have some data in a sql database and I’d like to calculate the slope. The data has this layout: I’d like the final output to look like this by creating a new table using SQL: To complicate things, not all Keywords have 3 dates worth of data, some have only 2 for instance. The simpler the SQL the better
Sort by last name and first name
I have a small little problem. I am have a drop down list that displays all of our customers names. As of right now, the list is sorted by last name (A-Z). But for example, there are 250 people with the last name “Smith”. What I need is a way that the last name of each customer to stay the