Cant delete some table data using sql - sql

I have a phone_book table. It has columns named id, first_name, last_name and phone. Im trying to delete all contacts with the first name of Jonathan and the last name of Luna.
My code is this
DELETE FROM phone_book WHERE first_name, last_name = "Jonathan" , "Luna";
Im getting a syntax error near the comma. Please help thanks

Set the WHERE conditions separated by AND:
DELETE FROM phone_book WHERE first_name = "Jonathan" AND last_name = "Luna";

Related

Oracle Live SQL: SQL command not properly ended

I want to show first_name, last_name columns and make UPPER-CASE LETTERS for those entries who have the letter 's' in the last name.
I am getting
ORA-00933: SQL command not properly ended
error. Anyone has an idea where I go wrong and How do I fix it?
SELECT first_name, last_name FROM CUSTOMERS
WHERE last_name LIKE "S"
UPDATE CUSTOMERS
SET
first_name = UPPER(first_name)
last_name = UPPER(last_name)
You seem to want:
UPDATE CUSTOMERS
SET first_name = UPPER(first_name), last_name = UPPER(last_name)
WHERE last_name LIKE '%S%'
It is unclear whether you want to match on upper-case or lower-case "s" in the name. If you want both, then:
WHERE UPPER(last_name) LIKE '%S%'
try putting "%S" instead of "S". If you put "S" it will only show you results for surnames that are just the letter S
Try following with subquery:-
UPDATE CUSTOMERS
SET ( first_name , last_name ) = (SELECT UPPER(first_name) , UPPER(last_name) FROM CUSTOMERS )
WHERE UPPER(last_name) LIKE 'S'
However, if you want all last_name starting with S then add wildcard such as
WHERE UPPER(last_name) LIKE 'S%'

how to add an element into a nested table in sql (oracle)?

Supposing i am using a table person, and persons might have multiple last names, so that attribute should be stored in a nested table (it's not about where to store last names), here is a simple sql for creating the type last name, the table person and adding an example row in oracle's sql developper (11G XE):
create type lastn as table of varchar2(10);
CREATE TABLE person
(
ID NUMBER NOT NULL
, last_name lastn
, CONSTRAINT EXEMPLE_PK PRIMARY KEY
(
ID
)
ENABLE
)nested table last_name store as ln;
insert into person values(1,ln('dani','bilel'));
I know how to update all last names at once, but i need to preserve existing last names and add other last names, or remove a single last name without affecting the others. In a nutshell, i want my code to be like (i am not familiar with PL/SQL):
update person set last_name=last_name+'third last name' where id=1;
I know that it doesn't work like that, should i use PL/SQL ?, isn't it posible other ways ?
Please excuse my question, and thank you for your response.
You can insert into the nested table by using a table collection operator:
insert into table(select last_name from person where id = 1) values ('third');
1 row inserted.
select last_name from person where id = 1;
LAST_NAME
--------------------------------------------------
LASTN('dani', 'bilel', 'third')
You can delete elements the same way:
delete from table(select last_name from person where id = 1) where column_value = 'bilel';
1 row deleted.
select last_name from person where id = 1;
LAST_NAME
--------------------------------------------------
LASTN('dani', 'third')
and you can even update them:
update table(select last_name from person where id = 1)
set column_value = 'second' where column_value = 'third';
1 row updated.
select last_name from person where id = 1;
LAST_NAME
--------------------------------------------------
LASTN('dani', 'second')

email address format sql oracle

I want to insert email address as firstname.last_name#domain.com in my table
if i do the following:
insert into employee
values ('EMP01','Mona','Ali',first_name||'.'||LAST_NAME||'#dentalHouse.com');
i get column not allowed here. Help please. Dont complicate the answer though.
is there any way to get around rather than typing name again ?
Thank you
I am guessing that you want update:
update employee
set emailaddress = first_name || '.' || LAST_NAME || '#dentalHouse.com'
where empid = 'EMP01';
I am just guessing what the id column is named.

SQL checking the last letter in string / AKA LIKE strange behaviour

I know it was already answered but it doesn't work for me.
So quick introduce:
I have table called swimmers. I'll insert new record to it which fulfils all columns:
INSERT INTO swimmers(id, first_name, last_name, age, gender)
VALUES(9,'Maria','Spolsky',34,'Female');
Now I want to find records in swimmers table which first_name DOES NOT end with letter a and where gender is Female
So I wrote SQL query:
SELECT first_name, last_name
FROM swimmers
WHERE first_name NOT LIKE '%a' AND gender = 'Female'
But it does return Maria which we just added. It basically returns every female
I'm using ORACLE iSQL plus.
EDIT:
I tried to use substr(first_name, -1) = 'a' but it turned out that -1 is empty, because I use varchar(20) and names are usually smaller than 20 characters
EDIT2:
I tried to find issue in data type. I used char(20) for first_name.
I changed it into varchar2(20):
ALTER TABLE swimmers
MODIFY first_name varchar2(20);
but didn't solve the issue
EDIT 3:
Changing NOT LIKE to WHERE first_name LIKE '%a' returns no rows. I believe issues lies inside data type and empty spaces from the end of the first_name to the end of reserved space for string (names has ~10 characters and I use `varchar2(20))
Appearantly oracles is adding spaces behind the name and that's why '%a' doesn't work, either change your column definition to a varchar (so oracle doesn't add the extra spaces) or trim the spaces out of your name like this:
SELECT first_name, last_name
FROM swimmers
WHERE NOT trim(first_name) LIKE '%a' AND gender = 'Female';

How Do I SELECT when needing to use LIKE and change column Name

Here is the problem I am working on:
Select all the Oracle database employees whose last names end with ā€œsā€ Change the
heading of the column to read Possible Candidates.
I tried
SELECT last_name AS possible_candidates FROM * WHERE last_name LIKE '%s';
That returned the error :
ORA-00903: invalid table name
Unless I am reading the question wrong how do I check the entire database for something like this?
To answer the question which is asking for employee names not just last names you would select all employees with a last name that ends in S.
SELECT *
FROM employees
WHERE last_name LIKE '%s'
If the table has more than a first_name and last_name column you can do
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%s'
Now to combine your two parts (Select employees and rename column)
Run one of the two queries above but add an AS statement as show in the example below.
SELECT *
AS Possible_Candidates
FROM employees
WHERE last_name LIKE '%s'
Based on feedback: this appears to be your answer...
Is there a table called employees? if that I would interpret the question as from the employees table. Otherwise, you could select table_name from All_TAB_COLS where column_name like = 'LAST_NAME' and then build a union from the resulting tables all of which have a field called 'LAST_NAME'