I've been trying to simply create and display information from these tables on an interpreter that uses sql.js to run.
I've looked through assignment forums and tried to assign primary keys in varying formats based on what was provided on w3schools and also tried to explicitly create a database to put the tables into. no changes.
DROP TABLE IF EXISTS employees;
CREATE TABLE pledges( donorId integer , donor text,
pledge integer, AmountPaid integer,
);
INSERT INTO pledges VALUES (1,'JOHNSON',6,30);
INSERT INTO pledges VALUES (2,'ROGERS',5,100);
INSERT INTO pledges VALUES (1,'RODDUCK',10,50);
INSERT INTO pledges VALUES (1,'PETERS',2,20);
INSERT INTO pledges VALUES (1,'ALBERTSON',7,56);
SELECT * FROM pledges;
The expectation is just for me to create the simple tables and test the queries but it just keeps saying "fetching results"
Your online tool probably swallows the error due to the incorrect CREATE TABLE statement. You have a dangling , after the amountpaid integer definition:
Once that error is fixed (and the pledges table is dropped instead of the employees table), your script runs fine:
DROP TABLE IF EXISTS pledges;
CREATE TABLE pledges
(
donorId integer,
donor text,
pledge integer,
amountpaid integer --<< you had a comma here
);
INSERT INTO pledges VALUES (1,'JOHNSON',6,30);
INSERT INTO pledges VALUES (2,'ROGERS',5,100);
INSERT INTO pledges VALUES (1,'RODDUCK',10,50);
INSERT INTO pledges VALUES (1,'PETERS',2,20);
INSERT INTO pledges VALUES (1,'ALBERTSON',7,56);
SELECT *
FROM pledges;
https://rextester.com/BJJGC94351
Related
I'm running the following SQLite workaround to add a primary key to a table that did not have one. I am getting a datatype mismatch on
INSERT INTO cities
SELECT id, name FROM old_cities;
However, the fields have exactly the same type. Is it possible that his happens due to running the queries from DbBrowser for SQLite?
CREATE table cities (
id INTEGER NOT NULL,
name TEXT NOT NULL
);
INSERT INTO cities (id, name)
VALUES ('pan', 'doul');
END TRANSACTION;
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE cities RENAME TO old_cities;
--CREATE TABLE cities (
-- id INTEGER NOT NULL PRIMARY KEY,
-- name TEXT NOT NULL
--);
CREATE TABLE cities (
id INTEGER NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (id)
);
SELECT * FROM old_cities;
INSERT INTO cities
SELECT id, name FROM old_cities;
DROP TABLE old_cities;
COMMIT;
You have defined the column id of the table cities to be INTEGER, but with this:
INSERT INTO cities (id, name) VALUES ('pan', 'doul');
you insert the string 'pan' as id.
SQLite does not do any type checking in this case and allows it.
Did you mean to insert 2 rows each having the names 'pan' and 'doul'?
If so, you should do something like:
INSERT INTO cities (id, name) VALUES (1, 'pan'), (2, 'doul');
Later you rename the table cities to old_cities and you recreate cities but you do something different: you define id as INTEGER and PRIMARY KEY.
This definition is the only one that forces type checking in SQLite.
So, when you try to insert the rows from old_cities to cities you get an error because 'pan' is not allowed in the column id as it is defined now.
I'm using a Trigger on SQL server to update a table stock when a sell is inserted into another table, but the trigger is not doing anything to the table, I suspect I must have an error I can't decipher. When I execute the test Inserts it shows no change to the first table.
The tables are:
Sku VARCHAR (50) PRIMARY KEY,
Stock NUMERIC (38)
);
CREATE TABLE dbo.Salida_Producto (
Numero_Salida INT PRIMARY KEY,
Sku VARCHAR (50),
Cantidad_Salida INT,
FOREIGN KEY(Sku) REFERENCES Stock(Sku)
);
--Test Tabla Stock. Test Values.
INSERT INTO dbo.Stock VALUES ('El Mitchies',100);
INSERT INTO dbo.Stock VALUES ('La Karencilla',200);
INSERT INTO dbo.Stock VALUES ('Perro',300);```
The Trigger:
CREATE TRIGGER [dbo].[tr_for_insert]
ON [dbo].[Salida_Producto]
AFTER INSERT
AS
BEGIN
DECLARE
#Item varchar,
#Cuantos numeric
SELECT #Item = INSERTED.Sku,
#Cuantos = INSERTED.Cantidad_Salida
FROM INSERTED
UPDATE Stock
SET Stock = Stock - #Cuantos
WHERE Sku = #Item
END;
GO
Test Inserts
INSERT INTO dbo.Salida_Producto VALUES (1, 'El Mitchies',3);
INSERT INTO dbo.Salida_Producto VALUES (2, 'La Karencilla',6);
INSERT INTO dbo.Salida_Producto VALUES (3,'Perro',130); ```
The problem I have is that the message box says:
(0 row(s) affected)
(1 row(s) affected)
You have thought your trigger with a row by row approach. It is not good practice. You must learn to think with a "by set" approach.
So a better way to achieve your goal should be something like :
Update S
Set S.Stock = S.Stock - I.Cantidad_Salida
From Stock S
Inner Join inserted I
On S.Sku = I.Sku
I have started my journey in learning SQL and right I am having trouble creating and inserting data into tables. Here is the code that I have tried, I get an error message saying that there aren't enough values. I am using Oracle.
Create table project
(
proj_id number(10),
medic_name varchar2(10),
purpose varchar2(12),
start_date date,
end_date date,
pi_id null,
CONSTRAINT pkprojid primary key (proj_id),
CONSTRAINT fkproject foreign key (pi_id) references researcher
);
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Insert into project values (PR002, 'Medic1', 'diabetes', '10/01/2016', '07/31/2020);
Insert into project values (PR003, 'Medic3', 'lung', '11/1/2014', '12/31/2020');
Insert into project values (PR004, 'Medic3', 'blood', '01/10/2017', '07/31/2019');
Insert into project values (PR005, 'Medic5', 'blood', '07/10/2018', '01/31/2020');
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Issues:
Your table has 6 columns, you are only passing 5 for insert; it seems like you are missing last column (pi_id), hence the error message that you are getting. If you want to skip the last column (which is possible since it is declared as nullable), you can explictly list the column when inserting
first column (proj_id) is of number datatype; PR001 is not a number (neither a string, since it is not quoted: this is a syntax error); did you mean 1 instead? Or, if you want to insert string values, you need to change the datatype of column proj_id to varchar(N) (N being the maximum length of the string, in bytes).
Here is an insert statement that should work for your current table definition:
insert into project(proj_id, medic_name, purpose, start_date, end_date)
values (1, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Note: there is a missing quote at the end of the date on the second insert statement; I assume that this is a typo.
CREATE TRIGGER logaction ON temployeelog
AFTER INSERT
AS
BEGIN
INSERT INTO TABLE temployee(ename, experience)
SELECT ename,experience FROM INSERTED
END
The structure of temployee
CREATE TABLE temployee
(
ename VARCHAR(20),
experience INT NOT NULL
)
ALTER TABLE temployeeADD DEFAULT (0) FOR experience
When I don't pass data in the experience column WHILE INSERT I get an error.
Cannot insert the value NULL into column 'experience', table
'temployee'; column does not allow nulls. INSERT fails. The statement
has been terminated.
I wanted to pass NULL Values temployeelog table AND wanted those situation to be handled by 'DEFAULT VALUES kept in temployee'
How can I achieve that?
The table default only comes into play if you don't insert it, so split the insert into one which handles a non-null experience and one which handles a null experience
INSERT INTO TABLE temployee (ename, experience)
SELECT ename, experience
FROM INSERTED
WHERE experience IS NOT NULL;
INSERT INTO TABLE temployee (ename)
SELECT ename
FROM INSERTED
WHERE experience IS NULL;
I have a table that has 60 million rows of data. I would like to introduce a new column say "id" for the table that is an auto incremented sequence.
For example:
CREATE TABLE Persons (
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO Persons VALUES ('abc', 'def');
INSERT INTO Persons VALUES ('abcd', 'ghi');
CREATE SEQUENCE "PERSON_SEQUENCE" START WITH 1 INCREMENT BY 1;
ALTER TABLE PERSONS ADD (PERSONID NUMBER);
UPDATE persons SET personid = PERSON_SEQUENCE.NEXTVAL;
In the above sql statements, I am able to create a sequence then alter the table and update it.
Since the amount of data I need to update is large.. I would like to perform this with as much low cost as possible.
I am trying to do so something like this:
ALTER TABLE PERSONS ADD (PERSONID NUMBER DEFAULT(PERSON_SEQUENCE.NEXTVAL));
but the above does not work. Oracle throws me the below error:
Error starting at line :
1 in command - ALTER TABLE PERSONS ADD (PERSONID NUMBER
DEFAULT(PERSON_SEQUENCE.NEXTVAL)) Error report -
ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
However this works:
ALTER TABLE PERSONS ADD (PERSONID NUMBER DEFAULT(0));
Could some one help me with how I can achieve to alter a table (create a new column) and populate the column with a seq id both in a single sql. Thank you!
For a table with 60 million rows, I would not do an add column + insert, but create the table new:
RENAME persons TO persons_old;
CREATE TABLE Persons (
personid number,
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO persons (personid, lastname, firstname)
SELECT person_sequence.nextval, lastname, firstname
FROM persons_old;
DROP TABLE persons_old;
If this is still taking too long, speak to your DBA about ALTER TABLE NOLOGGING and INSERT /*+ APPEND */ and PARALLEL DML.
EDIT: Ah, yes, for 60 million you could even increase the cache size of the sequence for the initial assignment:
ALTER SEQUENCE PERSON_SEQUENCE CACHE 1000;
This worked for me:
alter table PERSONS add (PERSON_ID number default PERSON_SEQ.nextval);