PostgreSQL, how can i populate this table which has two foreign keys as it's fields - sql

I have a problem when trying to populate a table using a script using the psql terminal window and the -f option (this executes the script).
I can populate the piste and lift tables fine no problems at all, but i have another table which contains foreign key's to these tables. I can create the table fine, but i cannot add anything to the table, i simply have no idea how to do this.
My tables:
piste {piste_name {PK}, grade, length, fall, open}
lift {lift_name {PK}, lift_type, summit, rise, length, operating}
lift_location {piste_name*{PK}, lift_name*{PK}}
My script:
CREATE TABLE piste (
piste_name varchar(30) PRIMARY KEY NOT NULL,
grade varchar(10) NOT NULL,
length decimal NOT NULL,
fall smallint NOT NULL,
open boolean NOT NULL
);
CREATE TABLE lift (
lift_name varchar(20) PRIMARY KEY NOT NULL,
lift_type varchar(10) NOT NULL,
summit smallint NOT NULL,
rise smallint NOT NULL,
length smallint NOT NULL,
operating boolean NOT NULL
);
CREATE TABLE lift_location (
piste_name varchar(30) REFERENCES piste(piste_name),
lift_name varchar(20) REFERENCES lift(lift_name),
PRIMARY KEY(piste_name, lift_name)
);
so if i insert some values into these tables:
INSERT INTO lift (lift_name, lift_type, summit, rise, length, operating) VALUES
('test lift', 'gondola', 1920, 440, 1600, true);
INSERT INTO piste (piste_name, grade, length, fall, open) VALUES
('test piste, 'medium', 3, 440, true);
These tables will one row with the specified information. based on the above i want my lift_location table to have the following information:
piste_name | lift_name
________________________
test piste | test lift
How can i accomplish this?
Thanks.
Chris.

You need to run an additional query. You need add a new row to the lift_location table to create the relationship between the two rows in the two tables.
INSERT INTO lift_location (piste_name, lift_name) VALUES ('test piste', 'test lift');
Also make sure both lift and piste tables have the required test data. You're missing a ' in your second INSERT query.

Related

Constraint not working as desired for my INSERT?

I am creating a Table named "Cliente" with some constraints on it as it follows:
CREATE TABLE public."Cliente" (
"K_CODCLIENTE" numeric(5) NOT NULL,
"N_NOMBRE1" varchar(15) NOT NULL,
"N_NOMBRE2" varchar(15) NOT NULL,
"N_APELLIDO1" varchar(15) NOT NULL,
"N_APELLIDO2" varchar(15),
"N_DIRECCION" varchar(50) NOT NULL,
"Q_TELEFONO" numeric(10) NOT NULL,
"K_CODREF" numeric(5),
"I_TIPOID" varchar(2) NOT NULL,
"Q_IDENTIFICACION" varchar(10) NOT NULL,
CONSTRAINT "PK_Cliente" PRIMARY KEY ("K_CODCLIENTE"),
CONSTRAINT "UQ_ID_TIPOID_CLIENTE" UNIQUE ("I_TIPOID","Q_IDENTIFICACION"),
CONSTRAINT "CK_CODCLIENTE" CHECK ("K_CODCLIENTE" >= 100),
CONSTRAINT "CK_Q_IDENTIFICACION" CHECK ("Q_IDENTIFICACION" IN ('CC', 'PA', 'CE', 'NI', 'OT'))
);
When I try to insert some values on it:
INSERT INTO "Cliente"
VALUES ('101','Juan','Felipe','Ortiz','Rojas','AK 15 no. 28-05','3101125507',null,'CC','51111111');
I get the following error (in PostgreSQL 14, on Fedora):
[23514] ERROR: new row for relation "Cliente" violates check constraint "CK_Q_IDENTIFICACION"
Detail: Failing row contains (101, Juan, Felipe, Ortiz, Rojas, AK 15 no. 28-05, 3101125507, null, CC, 51111111).
I am trying to restrict the "Q_IDENTIFICACION" column so it can only be filled with 'CC', 'PA', 'CE, 'NI' or 'OT'.
Maybe I'm doing something wrong when declaring the constraint "CK_Q_IDENTIFICACION"?
Seems like you messed up the mapping of values and are trying to insert '51111111' to "Q_IDENTIFICACION".
Consider this more revealing variant with a formatted list of target columns:
INSERT INTO "Cliente"
("K_CODCLIENTE", "N_NOMBRE1", "N_NOMBRE2", "N_APELLIDO1", "N_APELLIDO2", "N_DIRECCION" , "Q_TELEFONO", "K_CODREF", "I_TIPOID", "Q_IDENTIFICACION")
VALUES ('101' , 'Juan' ,'Felipe' , 'Ortiz' , 'Rojas' , 'AK 15 no. 28-05', '3101125507', NULL , 'CC' , '51111111'); -- !
Maybe you want to switch the last two column names in the table definition - and (not) adapt the VALUES list in the INSERT accordingly? (varchar(2) vs. varchar(10) seems switched as well.)
For persisted code, it's generally advisable to spell out target columns in an INSERT command in any case.
Asides:
Reconsider all these pesky double-quoted upper case identifiers. See:
Are PostgreSQL column names case-sensitive?
Consider plain type text instead of varchar(n) with strikingly tight character limits. See:
Any downsides of using data type "text" for storing strings?

SQL table display error

I am encountering a display issue with my SQL code and was hoping someone could help me figure out whats going on. When I create my CUSTOMER table then INSERT a line of values it works successfully... however, when I type select * from customer; then it displays horrible output where none of the data is lined up in the columns properly. Can you please take a look at my code and tell me what I can do to fix this.
I have multiple tables in this database and none of the other tables have this issue and display properly. My window was configured using these two lines of code:
SET LINESIZE 132
SET PAGESIZE 50
My table creation code:
CREATE TABLE Customer
(
CustomerID NUMBER(5) NOT NULL CONSTRAINT PK_Customer_CustomerID PRIMARY KEY,
BillingID NUMBER(5) NOT NULL,
CustomerFName VARCHAR2(40) NOT NULL,
CustomerLName VARCHAR2(40) NOT NULL,
CustomerPhone VARCHAR2(10) NOT NULL,
CustomerStreet VARCHAR2(30)NOT NULL,
CustomerCity VARCHAR2(30) NOT NULL,
CustomerState CHAR(2) NOT NULL,
CustomerZip VARCHAR2(9) NOT NULL,
CustomerEmail VARCHAR2(75) NOT NULL,
SignUp_Date DATE DEFAULT sysdate NOT NULL,
CustomerStatus CHAR(1) NOT NULL CONSTRAINT CC_Customer_CustomerStatus CHECK (CustomerStatus IN ('A', 'I')),
InactiveDate DATE,
InactiveReason VARCHAR2(200),
CustomerBillingCycle CHAR(1) NOT NULL CONSTRAINT CC_Customer_CustomerBC CHECK (CustomerBillingCycle IN ('A', 'B'))
);
My line of values being inserted into the table:
INSERT INTO Customer VALUES (01234, 99012, 'Michael', 'Huffaker', '6235551414', '65 N 35th Ln', 'Glendale', 'AZ', '85308', 'm.huffaker#quickmail.com', '29-MAY-2010', 'A', NULL, NULL, 'A');
As I stated above, both of these work successfully and the problem appears when I display the data in the table. Check out the screen shot link below to see the messed up output:
http://i.stack.imgur.com/uMu4S.png
It's not messed up at all; the output lines are just "wrapping" the output after 132 characters. It's very normal. I don't use command line often for running selects, but try routing the output to a file. Or perhaps try with a very large LINESIZE setting (like 1000 or so). Your terminal windows may not support a line that wide.

Cannot insert the value NULL into column 'id', even though Column has IDENTITY property

CREATE TABLE Type1
(
TypeID TINYINT NOT NULL IDENTITY(1,1),
TypeName VARCHAR(20) NOT NULL,
Speed VARCHAR(10) NOT NULL
CONSTRAINT TypeID_pk PRIMARY KEY (TypeID)
);
CREATE TABLE Splan
(
PlanID TINYINT NOT NULL IDENTITY(1,1),
TypeID TINYINT NOT NULL,
PlanName VARCHAR(20) NOT NULL,
Quota SMALLINT NOT NULL
CONSTRAINT PlanID_pk PRIMARY KEY (PlanID)
CONSTRAINT TypeID_fk FOREIGN KEY (TypeID) REFERENCES Type1(TypeID)
);
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps'),
('Other type', '50Mbps');
^Up until there its fine
and then when I enter the following it returns "Msg 515, Level 16, State 2, Line 8
Cannot insert the value NULL into column 'TypeID' ..... column does not allows. INSERT fails." Statement terminates
INSERT INTO Splan(PlanName, Quota)
VALUES ('Some sample name', '500GB'),
('sample2, '250GB');
I've tried creating the constraints at both column and table level but the second INSERT statement still refused to enter. Double checked via the GUI and 'TypeID' definitely has an IDENTITY property.
I've looked about everywhere and this errors seems to stem from the lack of an IDENTITY property, yet its present in my creation statements and the error still comes up. Tried removing the seed and increment from IDENTITY, still nothing. Also tried inserting the data one row at a time, nothing there either.
P.S If you haven't noticed the actual names have been substituted and other columns rows have been omitted.
Since you created typID as NOT NULL, Sql is complaining that the default value (NULL) is not acceptable.
Try
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Where corresponding records with TypeID = 1 and TypeID = 2 are in your Type1 table.
You are creating 2 tables: Type1 which has a primary key TypeId that is auto generated
and SPlan which has a primary key PlanId that is also auto generated and a foreign key TypeId that must be supplied and cannot be null.
As written you must enter 1 or more records into Type1 first, obtain their TypeIds, then enter those TypeIds into new records in SPlan.
Incidentally, using TINYINT for your primary key data types is perfectly legal but probably a really bad idea if this is anything other than homework.
You need to supply a value for TypeID in your second query because you have a foreign key relationship with the Type1 table and because the TypeID column in the Splan table is also declared NOT NULL.
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Try inserting both records in a transaction using SCOPE_IDENTITY
begin tran
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps')
DECLARE #id INT = SCOPE_IDENTITY();
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (#id, 'Some sample name', '500GB')
commit

oracle error: not enough values

i have a table donor_master:
create table donor_master
(
donor_id number(10) primary key not null,
dob date not null,
age number(3) not null,
gender char(1) not null,
blood_group char(3),
contact_no number(10),
address varchar(50) not null,
city varchar(10) not null,
pin number(10) not null,
state varchar(10) not null,
branch_registration_id number(5) references branch_master(branch_id)
);
when i try to insert into the table in a procedure insert_donor_master, i get "not enough values" error on compilation.
this is the procedure:
create or replace procedure insert_donor_master(
vdob donor_master.dob%type,
vage donor_master.age%type,
vgender donor_master.gender%type,
vblood_group donor_master.blood_group%type,
vcontact_no donor_master.contact_no%type,
vaddress donor_master.address%type,
vcity donor_master.city%type,
vpin donor_master.pin%type,
vstate donor_master.state%type,
vbranch_registration_id donor_master.branch_registration_id%type
)
is
begin
insert into donor_master values (sq_donor_master.nextval, vdob, vage, vgender, vblood_group, vcontact_no, vaddress, vcity, vpin, vstate, vbranch_registration_id);
commit;
end;
What is the problem?
Thanks.
Oracle hurls ORA-00947 when we specify an INSERT statement which doesn't have a value for every column in the table.
Now, the CREATE TABLE statement you posted shows a table with eleven columns. And the stored procedure code you posted shows an insert statement with eleven values in the VALUES (...) clause.
So, the explanations are:
you have a configuration management issue, and you're running the wrong version of the stored procedure or the wrong version of the table
you have a configuration management issue, and the actual structure of the table isn't what you think it is (doesn't match your CREATE TABLE script)
you aren't really getting an ORA-00947 error
Note that if you don't want to populate every row you can specify a projection of the relevant columns before the VALUES clause. For instance, if you just wanted to populate the mandatory columns you would code this:
insert into donor_master
(donor_id, dob, age, gender, address, city, pin, state )
values (sq_donor_master.nextval, vdob, vage, vgender, vaddress, vcity, vpin, vstate)
All that matters is that the number of values matches the number of columns.
The complete syntax for INSERT statements is in the documentation. enter link description hereFind out more.

Ensuring uniqueness of multiple large URL fields in MS SQL

I have a table with the following definition:
CREATE TABLE url_tracker (
id int not null identity(1, 1),
active bit not null,
install_date int not null,
partner_url nvarchar(512) not null,
local_url nvarchar(512) not null,
public_url nvarchar(512) not null,
primary key(id)
);
And I have a requirement that these three URLs always be unique - any individual URL can appear many times, but the combination of the three must be unique (for a given day).
Initially I thought I could do this:
CREATE UNIQUE INDEX uniques ON url_tracker
(install_date, partner_url, local_url, public_url);
However this gives me back the warning:
Warning! The maximum key length is 900 bytes. The index 'uniques' has maximum
length of 3076 bytes. For some combination of large values, the insert/update
operation will fail.
Digging around I learned about the INCLUDE argument to CREATE INDEX, but according to this question converting the command to use INCLUDE will not enforce uniqueness on the URLs.
CREATE UNIQUE INDEX uniques ON url_tracker (install_date)
INCLUDE (partner_url, local_url, public_url);
How can I enforce uniqueness on several relatively large nvarchar fields?
Resolution
So from the comments and answers and more research I'm concluding I can do this:
CREATE TABLE url_tracker (
id int not null identity(1, 1),
active bit not null,
install_date int not null,
partner_url nvarchar(512) not null,
local_url nvarchar(512) not null,
public_url nvarchar(512) not null,
uniquehash AS HashBytes('SHA1',partner_url+local_url+public_url) PERSISTED,
primary key(id)
);
CREATE UNIQUE INDEX uniques ON url_tracker (install_date,uniquehash);
Thoughts?
I would make a computed column with the hash of the URLs, then make a unique index/constraint on that. Consider making the hash a persisted computed column. It shouldn't have to be recalculated after insertion.
Following the ideas from the conversation in the comments. Assuming that you can change the datatype of the URL to be VARCHAR(900) (or NVARCHAR(450) if you really think you need Unicode URLs) and be happy with the limitation on the length of the URL, this solution could work. This also assumes SQL Server 2008 or better. Please, always specify what version you're working with; sql-server is not specific enough, since solutions can vary greatly depending on the version.
Setup:
USE tempdb;
GO
CREATE TABLE dbo.urls
(
id INT IDENTITY(1,1) PRIMARY KEY,
url VARCHAR(900) NOT NULL UNIQUE
);
CREATE TABLE dbo.url_tracker
(
id INT IDENTITY(1,1) PRIMARY KEY,
active BIT NOT NULL DEFAULT 1,
install_date DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
partner_url_id INT NOT NULL REFERENCES dbo.urls(id),
local_url_id INT NOT NULL REFERENCES dbo.urls(id),
public_url_id INT NOT NULL REFERENCES dbo.urls(id),
CONSTRAINT unique_urls UNIQUE
(
install_date,partner_url_id, local_url_id, public_url_id
)
);
Insert some URLs:
INSERT dbo.urls(url) VALUES
('http://msn.com/'),
('http://aol.com/'),
('http://yahoo.com/'),
('http://google.com/'),
('http://gmail.com/'),
('http://stackoverflow.com/');
Now let's insert some data:
-- succeeds:
INSERT dbo.url_tracker(partner_url_id, local_url_id, public_url_id)
VALUES (1,2,3), (2,3,4), (3,4,5), (4,5,6);
-- fails:
INSERT dbo.url_tracker(partner_url_id, local_url_id, public_url_id)
VALUES(1,2,3);
GO
/*
Msg 2627, Level 14, State 1, Line 3
Violation of UNIQUE KEY constraint 'unique_urls'. Cannot insert duplicate key
in object 'dbo.url_tracker'. The duplicate key value is (2011-09-15, 1, 2, 3).
The statement has been terminated.
*/
-- succeeds, since it's for a different day:
INSERT dbo.url_tracker(install_date, partner_url_id, local_url_id, public_url_id)
VALUES('2011-09-01',1,2,3);
Cleanup:
DROP TABLE dbo.url_tracker, dbo.urls;
Now, if 900 bytes is not enough, you could change the URL table slightly:
CREATE TABLE dbo.urls
(
id INT IDENTITY(1,1) PRIMARY KEY,
url VARCHAR(2048) NOT NULL,
url_hash AS CONVERT(VARBINARY(32), HASHBYTES('SHA1', url)) PERSISTED,
CONSTRAINT unique_url UNIQUE(url_hash)
);
The rest doesn't have to change. And if you try to insert the same URL twice, you get a similar violation, e.g.
INSERT dbo.urls(url) SELECT 'http://www.google.com/';
GO
INSERT dbo.urls(url) SELECT 'http://www.google.com/';
GO
/*
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'unique_url'. Cannot insert duplicate key
in object 'dbo.urls'. The duplicate key value is
(0xd111175e022c19f447895ad6b72ff259552d1b38).
The statement has been terminated.
*/