CREATE TABLE suppliers
(
supplier_ID NUMBER not null,
supplier_Name varchar2 (50) not null,
supplier_Addr varchar2 (100),
supplier_Town varchar2 (30),
supplier_State varchar2 (30),
supplier_Pcode varchar2 (15) not null,
supplier_Phone1 varchar2 (15),
supplier_Phone2 varchar2 (15),
supplier_Contact varchar2 (50),
supplier_Fax varchar2 (15),
supplier_Email varchar2 (40),
supplier_Renew DATE,
supplier_Creditlimit NUMBER,
supplier_Comments varchar2 (500),
PRIMARY KEY (supplier_ID)
);
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, 604-42449268, 09-4265050, 'DSHK CO', 04-2224568, 'DSHKCOMPANY#GMAIL.COM', 2024-08-23, 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');
I keep getting missing comma error for insert statement. Please guide me, tq
You need to make the phone numbers into strings as you have defined them as varchar.
70000,604-42449268,09-4265050
->
'70000', '604-42449268', '09-4265050'
I think you need to do tha same for date... that is 2024-08-23 -> '2024-08-23'
Assuming that you are using an Oracle database since you are using VARCHAR2 data types then use string and date literals for VARCHAR2 and DATE data types respectively:
insert into suppliers (
supplier_ID,
supplier_Name,
supplier_Addr,
supplier_Town,
supplier_State,
supplier_Pcode,
supplier_Phone1,
supplier_Phone2,
supplier_Contact,
supplier_Fax,
supplier_Email,
supplier_Renew,
supplier_Creditlimit,
supplier_Comments
) values (
010203,
'DSHK COMPANY',
'G 7 JLN TUNKU HASSAN ',
'Seremban',
'Negeri Sembilan',
'70000',
'604-42449268',
'09-4265050',
'DSHK CO',
'04-2224568',
'DSHKCOMPANY#GMAIL.COM',
DATE '2024-08-23',
1000,
'COMPANY WILL CALL BEFORE SENDING THE GOODS'
);
If you don't then, for example, 604-42449268 will be parsed as 604 subtract 42449268 which gives the result −42448664.
Do not use a string literal such as '2024-08-23' for the date as Oracle will implicitly try to convert the string literal to a date using the NLS_DATE_FORMAT session paramter. The default Oracle DATE format depends on the territory you are in and for most territories that implicit conversion will fail. Either use a DATE literal or explicitly call TO_DATE('2024-08-23', 'YYYY-MM-DD') with a format model.
db<>fiddle here
Regarding the create statement: NUMBER is not a valid type. You can use e.g. int(11) unsigned.
Also, instead of varchara(500) you can use text. So the create statement should be:
CREATE TABLE suppliers
(
supplier_ID int(11) not null,
supplier_Name varchar (50) not null,
supplier_Addr varchar (100),
supplier_Town varchar (30),
supplier_State varchar (30),
supplier_Pcode varchar (15) not null,
supplier_Phone1 varchar (15),
supplier_Phone2 varchar (15),
supplier_Contact varchar (50),
supplier_Fax varchar (15),
supplier_Email varchar (40),
supplier_Renew DATE,
supplier_Creditlimit int(11),
supplier_Comments text,
PRIMARY KEY (supplier_ID)
);
For your insert statement you must enclose all non-numerical values in quotes, like this:
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, '604-42449268', '09-4265050', 'DSHK CO', '04-2224568', 'DSHKCOMPANY#GMAIL.COM', '2024-08-23', 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');
Related
CREATE TABLE Pizza
(
pizza_id DECIMAL(12) NOT NULL PRIMARY KEY,
name VARCHAR(32) NOT NULL,
date_available DATE NOT NULL,
price DECIMAL(4,2) NOT NULL
);
CREATE TABLE Topping
(
topping_id DECIMAL(12) NOT NULL,
topping_name VARCHAR(64) NOT NULL,
pizza_id DECIMAL(12)
);
ALTER TABLE Topping
ADD CONSTRAINT topping_pk PRIMARY KEY(topping_id);
ALTER TABLE Topping
ADD CONSTRAINT Topping_pizza_fk
FOREIGN KEY(pizza_id) REFERENCES Pizza(pizza_id);
INSERT INTO pizza (pizza_id, name, date_available, price)
VALUES (1, 'Plain', CAST('27-Feb-2021' AS DATE), 6);
Error:
ORA-01858: a non-numeric character was found where a numeric was expected
I cannot figure out which part is wrong, I'm just a beginner for SQL, it seems related with date, can someone help me?
This works for me: SQL Fiddle. Don't use CAST to convert strings to dates. That's the only thing that looks off about your example. It may be using a different default date format than your string. Instead use TO_DATE( '27-Feb-2021', 'DD-Mon-YYYY') which converts a string to a date, or DATE '2021-02-27', which is a date literal and only takes the yyyy-mm-dd format.
Additionally, I'd suggest using NUMBER instead of DECIMAL just because it's more standard in the Oracle world. And always use VARCHAR2 instead of VARCHAR, which is officially discouraged.
I am trying to create this table and I am not sure why it wont run or let me
CREATE TABLE DRIVER (
DRIVER_ID INTEGER PRIMARY KEY,
DRIVER_NAME VARCHAR (10) NOT NULL,
DRIVER_ADDRESS VARCHAR (35) NOT NULL,
DRIVER_CITY VARCHAR (35) NOT NULL,
DRIVER_STATE VARCHAR (2) DEFAULT ‘TX’,
DRIVER_ZIP INTEGER (5) NOT NULL
);
This is the data I'm trying to insert into the table
INSERT INTO DRIVER (DRIVER_ID, DRIVER_NAME, DRIVER_ADDRESS, DRIVER_CITY, DRIVER_STATE, DRIVER_ZIP)
VALUES (‘3452342’, ‘Jennifer Kay, ‘2345 Green Ave’, ‘Dallas’, ‘TX’, ‘75201’);
VALUES (‘4323462’, ‘Alex Sanchez’ , ‘326 Main Street’, ‘Plano’ , ‘TX’, ‘75074’);
VALUES (‘7994638’, ‘Rob Joe’, ‘4315 Campbell Road’ ‘Mesquite’, ‘TX’,’75150’);
Thank you for the help
DEFAULT ‘TX’,
there are wrong quote characters. It should be DEFAULT 'TX',. The same problem in insert statement.
So I have the following table in my Oracle Database:
create table FilmStar (
filmStarID char(25) not null,
filmStarName char(50) not null,
birthplace char(50) not null,
yearBorn char(25) not null,
yearDied char(25),
primary key (filmStarID)
);
insert into FilmStar values ('0001','Tim Robbins','California, United States','1958',null);
insert into FilmStar values ('0005','Marlon Brando','Nebraska, United States','1924','2004');
I need to complete the following query: List the unique numbers, names, and ages of all film stars who are deceased.
I believe that I need to
select * from FilmStar where yearDied is not null
but how to find out the age?
I prefer to be explicit about type conversions:
select . . .,
(cast(yearDied as int) - cast(yearBorn as int) ) as approximate_age
from FilmStart
where yearDied is not null;
Relying on implicit conversions can result in very hard to find errors. Storing numbers as strings just exacerbates this problem.
Note: in Oracle, one is inclined to use to_number() rather than cast() for this purpose.
I am trying to create a trigger where is takes a several number of ratings from a table called FEEDBACK, and it creates an Average Rating on the MEMBER table. I want the trigger to update the average rating of each person whenever a new feedback is inserted.
----------- These are my tables -----------
create table Member_T(
MemberID Varchar2 (10) primary key,
MemberFirstName Varchar2 (20) NOT NULL,
MemberLastName Varchar2 (20) NOT NULL,
MemberMidleName Varchar2 (10),
MemberEmail Varchar2 (50) NOT NULL,
MemberPassword Varchar2 (20) NOT NULL,
MemberAdress Varchar2 (50) ,
MemberCity Varchar2 (20) ,
MemberState char (2) ,
MemberCountry Varchar2 (20) ,
MemberZipCode number (5,0),
MemberPhone Varchar2 (12) ,
MemberAverageRating number (3,1) check (MemberAverageRating >= 0.0 AND MemberAverageRating <= 5.0));
create table Feedback_T(
FeedbackID Varchar2 (10) primary key,
FeedbackMemberGiverID Varchar2 (10) references Member_T(MemberID),
MemberReceiverID Varchar2 (10) references Member_T(MemberID),
MemberRating number (3,1) check (MemberRating >= 0.0 AND MemberRating <= 5.0),
MemberComment Varchar2 (500),
MemberFeedbackDate Date Default(sysdate));
----------- This is my trigger -----------
create or replace trigger updateRating
after insert
on Feedback_T
for each row
Declare
rating Feedback_T.MemberRating%type;
receiver Feedback_T.MEMBERRECEIVERID%type;
averageRating Member_T.MemberAverageRating%type;
begin
select AVG(MemberRating), count(MEMBERRECEIVERID)
into rating, receiver
from Feedback_T
where Feedback_T.FEEDBACKID = :new.FEEDBACKID;
update Member_T
set averageRating = rating / receiver
where Member_T.MemberID = :new.MemberID;
end;
----------- I get this error -----------
TRIGGER UPDATERATING compiled
Errors: check compiler log
If you run "show errors" from the sql prompt, you will see your errors, or compile this in SQLDeveloper, which will show you the errors. Your problem is:
where Member_T.MemberID = :new.MemberID;
Where there is no bind variable called :new.MemberID, since MemberID is not a column in the Feedback_T table that the trigger is for. Maybe you meant to reference FeedbackMemberGiverID or MemberReceiverID?
I am using SQL Server Express 2008
When I'm trying load data from txt file in to this table
create table Clients
(
ClientID int not null IDENTITY (9000,1),
LastName varchar (30)not null,
FirsName varchar (30)not null,
MidInitial varchar (3),
DOB date not null,
Adress varchar (40) not null,
Adress2 varchar (10),
City varchar (40) not null,
Zip int not null,
Phone varchar (30) ,
CategCode varchar (2) not null,
StatusID int not null,
Hispanic BINARY default 0,
EthnCode varchar(3) ,
LangID int,
ClientProxy varchar (200),
Parent varchar (40),
HshldSize int default 1,
AnnualHshldIncome INT,
MonthlyYearly VARCHAR(7) ,
PFDs INT,
WIC BINARY default 0,
Medicaid BINARY default 0,
ATAP BINARY default 0,
FoodStamps BINARY default 0,
AgencyID int not null,
RoutID int ,
DeliveryNotes varchar (200),
RecertificationDate date not null,
Notes text,
Primary Key (ClientID)
);
I use
SET IDENTITY_INSERT Clients2 ON;
BULK INSERT Clients2
FROM 'c:\Sample_Clients.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\r\n'
)
SQL Server Express trows me errors
Msg 545, Level 16, State 1, Line 2
Explicit value must be specified for identity column in table 'Clients' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
File has only one line (for now just sample data) I check it many times its one line
Data looks like this
13144,Vasya,Pupkin,,1944-10-20,P.O. Box 52,,Wrna,99909,(907) 111-1111,SR,4,0,W,1,,,3,1198,month,0,0,1,0,1,45,,,2011-04-27
Any ideas how to fix this problem?
You need the parameter KEEPIDENTITY in your bulk insert statement. This is required to retain identity values in the load.
BULK INSERT Clients2 FROM 'c:\Sample_Clients.txt'
WITH ( KEEPIDENTITY, FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n'
)
I also think you will have a problem because you have no data or placeholder for the Notes column. A comma added to the end of the file should address this.