I added a column to my table :
ALTER TABLE venues ADD decent BOOLEAN NOT NULL DEFAULT 0;
.schema venues returns :
CREATE TABLE [venues] (
[id] INTEGER PRIMARY KEY,
[foursquare_id] TEXT NOT NULL,
[pick] INTEGER NOT NULL DEFAULT 0,
[foursquare_data] TEXT NOT NULL,
[lat] FLOAT NOT NULL,
[lng] FLOAT NOT NULL,
[name] TEXT NOT NULL,
[closed] INTEGER NOT NULL DEFAULT 0,
[google_data] TEXT NOT NULL,
[google_place_id] TEXT NOT NULL,
[google_name] TEXT NOT NULL
, decent BOOLEAN NOT NULL DEFAULT 0);
Column I added does not have square brackets while all others do. What does this mean? Do I need to do this some other way?
The dot-command .schema venues returns the sql column of the Schema Table for the row that corresponds to the table venues.
The equivalent SQL statement would be:
SELECT sql FROM sqlite_schema WHERE name = 'venues';
As it is explained in Interpretation Of The Schema Table:
The text in the sqlite_schema.sql column is a copy of the original
CREATE statement text that created the object, except...
which means that if you used square brackets in all or some column names in the CREATE TABLE statement then this is what you get when you execute .schema venues.
If you add a new column, then its definition is added in the original CREATE TABLE statement inside the sql column as it is written in the ALTER TABLE statement.
If you want square brackets for all the column names then use them when you add the column:
ALTER TABLE venues ADD [decent] BOOLEAN NOT NULL DEFAULT 0;
See the demo.
Related
What do I need to add to make sure this table is created with the 'name' field as non null but with a value of ""?
CREATE TABLE stuff (id serial primary key, name varchar(64) <-- what goes here??
Standard SQL applies:
CREATE TABLE stuff (
id serial primary key,
name varchar(64) not null default ''
);
It's possible you attempted to use double quotes to specify the text literal like this "", which will explode. Postgres uses single quotes to delimit text literals, like this ''.
See SQLFiddle.
I am new to MSSQL 2014 Server, my professor listed these steps to make a table, I don't know the proper steps to create tables in the pictures listed below, please help.
Create and populate (insert values) the following tables per table description and data values provided
DEPARTMENT
EMPLOYEE
PROJECT
ASSIGNMENT
Add a SQL Comment to include /* * Your First Name_Your Last Name* */ when inserting corresponding values for each table.
What I tried so far:
CREATE TABLE DEPARTMENT(
DepartmentName Text(35) PRIMARY KEY,
BudgetCode Text(30) NOT NULL,
OfficeNumber Text(15) NOT NULL,
Phone Text(12) NOT NULL, );
I have put this to my query and the error is
Msg 2716, Level 16, State 1, Line 1 Column, parameter, or variable #1: Cannot specify a column width on data type text.
Try this(I assume that your table exists in dbo schema):
IF OBJECT_ID(N'dbo.DEPARTMENT', N'U') IS NOT NULL
BEGIN
DROP TABLE DEPARTMENT
END
GO
CREATE TABLE DEPARTMENT(
DepartmentName varchar(35) PRIMARY KEY,
BudgetCode varchar(30) NOT NULL,
OfficeNumber varchar(15) NOT NULL,
Phone varchar(12) NOT NULL
);
You can not define width for Text data type. In case which you need to define width you can use char or varchar data types. Also keep in mind that if you need to work with Unicode characters then you will need to use nchar or nvarchar instead.
Searching on google the SQL state: 22001 is because:
the sql statement specifies a
string that is too long
but both tables have the same definition of the column.
this is the table i want to have the data copied:
CREATE TABLE wise_estado
(
id_estado serial NOT NULL,
>> cvgeo_estado character varying(2),<<
nombre_estado character varying
)
this the table with data i want to copy:
CREATE TABLE estados
(
gid serial NOT NULL,
>> "CVE_ENT" character varying(2),<<
"NOM_ENT" character varying(80),
geom geometry(MultiPolygon,4326),
CONSTRAINT estados_pkey PRIMARY KEY (gid)
)
my SQL statement:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT 'CVE_ENT', 'NOM_ENT'
FROM estados
What i'm missing in my SQL statement?
You are sending string literals instead of field names in your SQL statement. Instead:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT "CVE_ENT", "NOM_ENT"
FROM estados;
Or, more succintly:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT CVE_ENT, NOM_ENT
FROM estados
I have a table viz. expenses with three columns as under
ExpenseId int NOT NULL,
ExpenseName varchar(50) NOT NULL,
Invalid bit NOT NULL
To add a new column (OldCode char(4) not null), I used design feature for tables in Microsoft SQL Server Management Studio. But I get following error
'Expenses' table
- Unable to modify table. Cannot insert the value NULL into column 'OldCode', table 'TransportSystemMaster.dbo.Tmp_Expenses'; column does not allow nulls. INSERT fails. The statement has been terminated.
Incidentally I have been able to add same column with same specifications to other tables of the same database.
Any help?
Your Table Consist of Existing Records
and you are pushing a new column of type NOT NULL.
so for older records the data have to be something.
try something like this
ALTER TABLE MY_TABLE ADD Column_name INT NULL
GO
UPDATE MY_TABLE <set valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN Column_name INT NOT NULL
GO
Since OldCode is NOT NULL, you should specify a default value for it.
when you have some rows on your table you can't add a column that is not nullable you should provide a default value for it
Alter Table table_name add OldCode int not null DEFAULT(0);
You have to specify values for all the 4 fields of the table, its purely because, while designing the table you set the definition of the columns to be not null. Again you are adding a new column called OldCode and setting to be not null, all ready existing records hasn't got a value. So that is the reason its complains
I am trying to create tables in an SQLite database with sqlite3.
The command $ sqlite3 mydb < mytables.sql produce the following error: Incomplete SQL: ??C.
mytables.sql is:
CREATE TABLE SizeCulture (
SizeCultureID INTEGER PRIMARY KEY ASC,
SizeID INTEGER NULL,
CultureID TEXT NULL,
Name TEXT NULL,
Description TEXT NULL,
Abbreviation TEXT NULL,
);
CREATE TABLE Size(
SizeID INTEGER PRIMARY KEY ASC ,
Creation TEXT NOT NULL,
Modification TEXT NOT NULL,
Deleted INTEGER NOT NULL,
);
/****** Object: Table [Ordering].[BarCode] Script Date: 11/09/2011 14:58:19 ******/
CREATE TABLE BarCode(
BarCodeID INTEGER PRIMARY KEY ASC NOT NULL,
BarCodeValue TEXT NOT NULL,
);
This was modified from a script generated by SQL Server, where some tables need to be replicated on an Android device.
The above is just a set of repeating create table statements. From what I understand, SQLite follows standard SQL (like MySQL or postgres).
Though I can't test it at the moment, I think it's the trailing commas that are confusing it (for example, the comma at the end of Abbreviation TEXT NULL,). Try removing all those trailing commas.
Edit: To be clear, I'm talking about all of these commas:
Abbreviation TEXT NULL,
...
Deleted INTEGER NOT NULL,
...
BarCodeValue TEXT NOT NULL,
I had the same problem, but for a different reason (so I'm commenting because Google led me here). Turns out you can also encounter this error if your file has a weird encoding (like UCS-2 instead of UTF8).