Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm SORRY everyone, It wasn't allowing me to comment. you are right. It's the ALTER I'm having issues with. How do I add the Cumulative GPA column to the table (using the alter statement) where the gpa is displayed between 0.00 and 4.00
CREATE database "IS4440_DuBoseJasmine"
CREATE TABLE StudentInformation (
StudentID CHAR(7) not null,
StudentSSN CHAR(9) null,
StudentFirstName VARCHAR(50) null,
StudentLastName VARCHAR(50) null,
StudentMiddleName VARCHAR(50) null,
StudentHomeCountry CHAR(2) not null
)
/*2*/
ALTER TABLE StudentInformation ADD Cumulative GPA ;
/*3*/
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1352154', 'DuBose', 'Jasmine', 'Leigh', 'US')
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1234565', 'Smith', 'Johnny', 'Apple', 'GB');
/*4*/
UPDATE StudentInformation SET StudentSSN = 123456789
WHERE StudentID = 1352154;
The update works fine (demonstrated on SQL Fiddle), it is the ALTER TABLE statement that fails. This line:
ALTER TABLE StudentInformation ADD Cumulative GPA;
should be:
ALTER TABLE StudentInformation ADD [Cumulative GPA] INT -- OR WHATEVER TYPE IT SHOULD BE;
ALTER TABLE Documentation
As an aside, although this may just be an example, if it isn't you will want to USE your database before you create the table:
CREATE database "IS4440_DuBoseJasmine";
GO
USE IS4440_DuBoseJasmine;
CREATE TABLE ...
Otherwise you will just be creating your table in whatever database you are connected to.
Your update query is fine, Your ALTER statement what fails, You are adding new column to your table without any datatype or constrains, try this to alter your table
You can use Numeric(3,2) or Decimal(3,2), that means total three digits two after decimal point so a max of 9.99 .
ALTER TABLE StudentInformation ADD CumulativeGPA Numeric(3,2) NULL
To alter your table to add new column make that column NULLABLE and while naming your column do not give spaces.
To make column type Not Nullable
ALTER TABLE StudentInformation ALTER COLUMN CumulativeGPA DECIMAL(3,2) NULL ;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am working with a postgreSQL database in VSCode via the SQL Tools extension. I have the following query, but it does not execute. I suspect the reason is because I am not "chaining" the queries properly.
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
)
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
I receive the following error: syntax error at or near "INSERT" at character 146. If I were to separate the commands and then run each separately, this error does not arise.
Ideally, I could execute the one query instead of having to first execute a CREATE TABLE and then executing an INSERT. Is there a proper terminology for doing this kind of chaining of queries?
missing semicolon";" after create statement :
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
); --<-- here
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
You just missed the semicolon(;) after create table statement.
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
Db-Fiddle:
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
Select query:
select * from test_table;
Output:
id
first_name
last_name
1
Samantha
Gray
db<fiddle here
You can run multiple queries at once but each query still need to be defined properly and especially ending with a ;.
How to run multiple queries at once depends a lot on the way you run them but from VSCode I suppose you have to select both queries and click on some button (as opposed to not selecting the query or only selecting one).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is the query I have to create a table and insert a record. I have some fields that are nullable here. If I want to create an insert statement do I have to define a value for the nullable parameters placeholder? For example, in my insert statement I'm defining active having an ID of 1. The last 4 nullable columns insertDt, InsertPartyId, UpdateDt, UpdatePartId are all empty for this record statement insert. Is this fine and the proper way of creating an insert with nullables?
![Me getting ready to query said statement.][1]
Create Table RecognitionStatusDomain
Id integer,
Description nvarchar(100),
InsertDt DateTime,
InsertPartyId integer,
UpdateDt DateTime,
UpdatePartId integer);
Insert into RecognitionStatusDomain (Description)
(Values 1, 'Active', Null);
So what I think you're asking is can you update columns that are defined as null later. The answer is yes. For your situation you could create the table:
create table RecognitionStstusDomain
(
Id int not null
,Description nvarchar(100) not null
,InsertDt datetime
,InsertPartyId int
,UpdateDt datetime
,UpdatePartyId int
);
insert into recognitionststusdomain (Id, Description)
values (1, 'active');
Again, from what I can understand you are wanting Id and Description to be not null since you know what values you want them to be and want to update the others later.
I defined the Id and Description columns as not null which will make you enter a value in those fields. The rest of the columns allow nulls and you will be able to update later. You don't have to define the rest of the values as null they will just show null with no input.
here is a link showing what I did and what the results will be http://sqlfiddle.com/#!18/8da61/1/0
OK, first things first. You actually can perform the row insertion as you pretend to. Nothing will prevent you from inserting nulls in the whole row if you wanted to.
Now, the practical thing. How are you going to retrieve that row (later in life) in order to (for example) add more information to it? You'll need a key.
So, to make it practical, add a key to that table. Since you don't want to specify its value, use an auto-generated key (auto increment, or identity by default/always). This way you will be able to identify the rows you inserted inequivocally.
Null fields are any columns that are nullable.
They are specified in the table column definition as "null" or "not null". If they are nullable you can insert nulls to the column. These nulls represent an "unkown" state. INSERT INTO TABLE (myNullableCol) VALUES (NULL) as you have in your image works perfectly fine.
If you try to insert null into a column that doesn't allow it, SQL will fail and show you an error.
If you don't want to insert some value to a column you can leave in your insert statement or you can just put NULL to that column.
insert into recognationstatusdomain (id, description) values (1, 'active');
OR
insert into recognationstatusdomain (id, description, insertDt, insertPartyId, updateDt, updatePartyId) values (1, 'active', null, null, null, null);
OR
insert into recognationstatusdomain values (1, 'active', null, null, null, null);
If you assign any column a primary key or assign a constraint NOT NULL then you can't insert NULL values to that column.
Create Table Table_Name(id int NOT NULL, Name varchar(10))
Insert into Table_Name (id,Name) values(NULL,'Some_Name') --Give you an error
This question already has answers here:
Set ORACLE table fields default value to a formular
(2 answers)
Closed 8 years ago.
I got a table which I used the below code to create.
create table Meter (MeterID CHAR(8) CONSTRAINT MeterPK PRIMARY KEY,
Value CHAR(8) CONSTRAINT ValueNN NOT NULL,
InstalledDate Date CONSTRAINT InDateNN NOT NULL);
Then I tried adding a derived column that adds 6 months to the installeddate.
alter table meter add ExpiryDate as (add_months(installedDate,6)) not null;
This returns an error of invalid datatype.
I read somewhere that I do not have to specify the datatype of ExpiryDate as it can be derived from the function. So where did I go wrong?
EDIT: Turns out Mike was right. I used the trigger method to get things going, but I was confused whether I'm using mysql or oracle. Think in the end I'm using oracle actually. Have problems with the trigger but turns out I do not need to have the command "set" in the trigger. Below is the code that works.
CREATE OR REPLACE
TRIGGER trigexpdate1
BEFORE INSERT ON Meter
FOR EACH ROW
BEGIN
:NEW.ExpiryDate := ADD_MONTHS(:NEW.InstalledDate, 6);
END;
If I don't have the begin and end in the statement, it will throw an error saying illegal trigger specification.
MySQL doesn't support
derived columns in table definitions,
a function named add_months(), or
inline constraints.
This is a more or less standard way to write that statement in MySQL.
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
primary key (`MeterID`)
);
You have two options for a derived column like "ExpiryDate".
Create a view, and do the date arithmetic in the view. Use date_add().
Add the column "ExpiryDate" to the table, and keep it up-to-date with a trigger.
BEFORE INSERT trigger example
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
`ExpiryDate` Date not null,
primary key (`MeterID`)
);
create trigger trigexpdate1
before insert on `Meter`
FOR EACH ROW
SET NEW.`ExpiryDate` = date_add(NEW.`InstalledDate`, interval 6 month);
Note how ExpiryDate changes from the insert statement to the select statement below.
insert into Meter
values ('1', '1', '2014-07-01', '2014-07-01');
select * from Meter;
MeterID Value InstalledDate ExpiryDate
--
1 1 2014-07-01 2015-01-01
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created the tables with this:
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40)
DESTINATION VARCHAR(40)
);
CREATE TABLE TAXI(
TAXI_ID INT,
COSTUMER_ID INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_PHONE_NUMBER INT,
DRIVER_NAME INT,
DESTINATION
);
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
);
After this my table appeared with all the attributes. Then I set my PKs and FKs manually.
now am trying to insert some data to each of the tables but I keep getting error 213:
Column name or number of supplied values does not match table definition
I searched and searched and still don't understand.
also when I insert the code for such as SELECT * FROM Costumer (that's the table name) it won't show in the small suggestion box as if it doesn't exist???
The below code should work for your requirement unless you have specified an identity column manually as you said that you made some changes manually.
In case you specified an identity column manually then you should not insert the value for the Identity column (say Customer_ID) or you should set SET IDENTITY_INSERT COSTUMER ON before inserting the ID column
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
SELECT * FROM COSTUMER
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID,COSTUMER_PHONE_NUMBER,COSTUMER_NAME,DESTINATION) VALUES (1, 33,123211241, 'John','Mexico' )
I think you are trying to insert more/less input to table
INSERT INTO [TableName] (Column1, Column2, Column3)
VALUES (Value1, Value2, Value3)
Number of columns should be equal to Number of Values. Please check your INSERT statement.
If you not included (Column1, Column2, Column3) in your statement, include it.
Post the insert statement you used. I see a few problems with the current code you posted.
You don't provide datatypes for a few of your attributes, for instance in the Booking table you need to provide some type for the CUSTOMER_ID attribute. Also you need the attributes and datatypes to be comma separated, except the last one doesn't need a trailing comma. For instance you have:
...
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
...
This will throw an error probably saying incorrect syntax near COSTUMER_NAME.
You need something like:
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID INT,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40),
COSTUMER_NAME VARCHAR(40),
TIME_OF_ARRIVAL DATETIME
);
For an insert try something along the lines of:
INSERT INTO <table_name> (<column1>,<column2>,<column3>)
VALUES (<col1Value>,<col2Value>,<col3Value>)
If you aren't getting auto completion you might try and refresh the DB in the management studio by right click > refresh, but I wouldn't rely on auto completion at all in SQL Server nor IntelliSense unless you use someting like this.
If you post the insertion script I might be able to help more.
Update: So, the problem you got was with arithmetic overflow, specifically trying to put 07854625781 into a int. All datatypes have maximum/minimums or other "constraints", an int can be in the range of -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) (4 bytes). The value you enter exceeds that, it is being interpreted as 07,854,625,781 (7.8 billion) while the maximum for an int is only 2.147 billion. If you change the datatype of phone number to bigint that can hold -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807) (8 bytes) it will work, However I would advise you not to store a phone number in that way, rather, use char(length) if you know that the size and format will always be the same, if you think the length could vary (maybe from extensions or international numbers) I would use something along the lines of varchar(40).
Through this datatype change you will just have to surround input in 'single quotes' when you insert.
Cheers
The Problem that you are facing in the below insert query is the value 07854625781 has become too large to handle for int type. Int range 2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). So change the datatype of COSTUMER_PHONE_NUMBER to Bigint.
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport')
use the below query to change the datatype to bigint and then try insert(above command)
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
Steps to Follow
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport');
Now do
Select * from COSTUMER
you will find the record inserted
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am very new to this. I have created this table and I had no problems doing it:
CREATE TABLE [dbo].[Urban_Rail] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Country] VARCHAR (50) NOT NULL,
[City] VARCHAR (50) NOT NULL,
[Type] VARCHAR (50) NOT NULL,
[Gauge] NCHAR (10) NULL,
[Year] NCHAR (10) NULL,
[Status] VARCHAR (50) NULL,
[Notes] VARCHAR (500) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Now I am trying to fill in few thousand rows of data using this statement:
INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Algiers','Metro','1435','2011','Open','');
INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Algiers','Tram','1435','2011','Open','');
INSERT INTO Urban_Rail ('Id','Country','City','Type','Gauge','Year','Status','Notes') VALUES ('','Algeria','Batna','Tram','','','Planned','');
But I get errors saying that the name of each column is not valid. That does not look true to me. I am wondering if it has something to do with the Id instead which is an auto increment identity which I have left without value in my query. What am I doing wrong here? Some help will be appreciated.
Either remove the ID from the Insert (as it is an Identity) or if you want specific ID's turn Identity_Insert on before running the insert and off again afterwards.
SET IDENTITY_INSERT [dbo].[Urban_Rail] ON
INSERT INTO Urban_Rail (Id,Country,City,Type,Gauge,Year,Status,Notes) VALUES (1,'Algeria','Algiers','Metro','1435','2011','Open','');
SET IDENTITY_INSERT [dbo].[Urban_Rail] OFF
An IDENTITY column should not be included in the INSERT statement. It will automatically populate with the next value.
The column "ID" is an identity column and you're not allowed to insert it since the database assigns it. Take it off the column name and values list in your inserts.
If you DO need to insert the identity column, you can use
SET IDENTITY_INSERT <table> ON
But am guessing you don't need that since your ID's look blank.