I have just started with IBM db2 and am running an sql query which I suppose is right. But nothing is happening when I hit the run button. Could anyone help?
Create table INSTRUCTOR
CREATE TABLE INSTRUCTOR
(ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
Under the result area it is displaying "waiting" and nothing actually happens.
Try
CREATE TABLE INSTRUCTOR
( ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
That should work for you assuming that you have ; set as the statement terminator in your query interface (it is the default in most SQL interfaces for Db2).
Alternatively, it might be that your CREATE TABLE is waiting for a lock on the system catalog. You can look at currently held lock with MON_GET_LOCKS https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0056428.html
Related
I am learning Azure and data analytics with Azure. Recently finished learning Postgresql.
My question is if there is an alternative to BIGSERIAL data type for Azure Databases. I ran the query (below the error in the following) and had an error. Note that this datatype exists in Postgresql and hence I am getting confused in Azure. Any alternative to BIGSERIAL?
Failed to execute the query. Error: Column, parameter, or variable #1:
Cannot find data type BIGSERIAL.
create table person (
ID BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);
In PostgreSQL, the SERIAL keyword is used to setup an auto increment column, this works similar to auto increment in SQL. BIGSERIAL is an auto-incremented Bigint column of 8 bytes.
Closest, I could find "bigserial"in MS docs is as here
So...you can use BIGINT instead, below works fins for me.
create table person (
ID BIGINT NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);
Can someone help me find the reason this SQL query isn't working in Microsoft SQL Server?
CREATE TABLE USER
(
USER_ID int NOT NULL PRIMARY KEY,
USER_L_NAME varchar(30) NOT NULL,
USER_F_NAME varchar(20) NOT NULL,
PASSWORD varchar(20) NOT NULL,
USERNAME varchar(20) NOT NULL,
DOB date NOT NULL,
COUNTRY char(30) NOT NULL,
STATE_REGION char(2),
EMAIL varchar(20) NOT NULL,
STREET varchar(50) NOT NULL,
CITY varchar(30),
ZIP_CODE char(5),
MOBILE_PHONE char(11)
);
USER is a reserved word in SQL Server - as in most other databases.
You can either quote this identifier by surrounding it with square brackets (CREATE TABLE [USER] (...)), or change the table name to something else - such as USERS for example.
I would recommend the latter. Using reserved words for identifiers is error-prone (you need to quote the identifier everywhere you use it later on), and can easily be avoided.
I'm trying to create a table and I'm getting ORA-00984 'Column not allowed here' message. It's a very straightforward table.
CREATE TABLE all_users
(user_id number DEFAULT user_id_seq.NEXTVAL PRIMARY KEY,
email_address VARCHAR(25) NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(30) NOT NULL);
it keeps kicking me out this error. Can someone help please?
I also have other tables I'm trying to create as well and am getting the same error.
I'm using sql server 2008 management studio. Im trying to create two seemingly simple tables but continue getting this error at execution. I have tried changing the names of these tables to many different variations. Nothing is helping. Here below is what I am typing.
USE POS410;
go
CREATE TABLE [empl]
(
employeeID INT NOT NULL,
lname VARCHAR(15) NOT NULL,
fname VARCHAR(15) NOT NULL,
address VARCHAR(20) NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(10) NOT NULL,
areacode INT NOT NULL,
tnumber INT NOT NULL,
EEO1classification VARCHAR(10) NOT NULL,
hiredate DATE NOT NULL,
salary INT NOT NULL,
gender VARCHAR(1) NOT NULL,
age INT NOT NULL,
clocknumb INT NOT NULL,
PRIMARY KEY(employeeID),
UNIQUE(employeeID),
UNIQUE(clocknumb),
FOREIGN KEY(clocknumb) REFERENCES [jb_ttl](Empnum),
);
go
CREATE TABLE [jb_ttl]
(
EEO1 VARCHAR(10) NOT NULL,
JobTitle VARCHAR(15) NOT NULL,
JobDscrpt TEXT NOT NULL,
ExemptNonExemptStatus VARCHAR NOT NULL,
Empnum INT NOT NULL,
PRIMARY KEY (Empnum),
);
go
Here is the error report:
Msg 2714, Level 16, State 6, Line 2 There is already an object named 'empl' in the database. Msg 2714, Level 16, State 6, Line 2 There is already an object named 'jb_ttl' in the database.
I would say that you've tried to run this script more than once. It would have failed the first time because you're trying to create the first table with a foreign key to the second, which didn't exist yet. I use the word "didn't" because if my guess is correct, it does now, and that's why you're getting the object already exists error.
As a side note, SQL has supported wrapping DDL (creating/modifying objects) at least going back to 2005. I normally wrap table changes in transactions, and don't commit the transaction until everything goes through with no error messages.
Give this a run to confirm if jb_ttl already exists:
select * from master.dbo.sysobjects where [name] = 'jb_ttl'
That will show you if anb object named jb_ttl already exists. Could it already exist as a procedure or trigger perhaps?
I am doing my first database project.
I would like to know which of the following ways should I use to make SQL queries in DDL.
#1
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name CHAR(50) NULL,
last_name CHAR(75) NOT NULL,
dateofbirth DATE NULL
);
#2
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name CHAR(50) NULL,
last_name CHAR(75) NOT NULL,
dateofbirth DATE NULL
);
#3
CREATE TABLE employees (
ID integer PRIMARY KEY,
FIRST_NAME char(50) NULL,
LAST_NAME char(75) NOT NULL,
DATAOFBIRTH data NULL
);
It doesn't really matter to the database, and so you should generally go for what's more readable and maintainable. There are a lot of different conventions out there, and I switch between them depending on circumstances all the time. Imo, what's good for a small query isn't the same as what's good for a more-complicated query.
In this case, I think your #3 looks most readable. But that's just my opinion and if there are conventions or existing expectations for how the code looks where you are, stick to that.
Sql coding Convections suggest :
CREATE TABLE employees
(
id INTEGER PRIMARY KEY,
first_name CHAR(50) NULL,
last_name CHAR(75) NOT NULL,
dateofbirth DATE NULL
);