question

FragosoArianaM-2038 avatar image
0 Votes"
FragosoArianaM-2038 asked FragosoArianaM-2038 commented

sql database, and table set up, can some one help me see what the issue here is

CREATE DATABASE TableInsert;

CREATE TABLE employee (
PersonID int,
FirstName varchar(255),
LastName varchar(255),
Age int,
HiredDate varchar(255),
);
Insert into tbl_employee(Fristname, Last name ,age, HiredDate)
values( ‘Alan’, ‘Palmer’, 32 , ‘2019-12-15),
( 'Susan', ‘Shepard’, 28 , ‘2015-07-21'),
( 'Justin', 'Ward', 43 , '2017-08-24'),

sql-server-generalsql-server-transact-sql
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is the problem?

Are you getting an error?

1 Vote 1 ·

Your post has "reverse single quotes" around "Alan" and "Palmer". Stings in SQL Server must use single quote '.

1 Vote 1 ·
JingyangLi avatar image JingyangLi TomPhillips-1744 ·

You need the following syntax to switch database context after the creation:

CREATE DATABASE TableInsert;
Use TableInsert;
--the following fixes, you can use Noami's code.

CREATE TABLE employee (
......

1 Vote 1 ·

wait so I should use " instead of ' ?

0 Votes 0 ·
JingyangLi avatar image JingyangLi FragosoArianaM-2038 ·

are you using SQL Server?

0 Votes 0 ·
Show more comments
NaomiNNN avatar image
1 Vote"
NaomiNNN answered FragosoArianaM-2038 commented

I just tried the script (originally didn't test). This works fine in SQL Server:

 CREATE DATABASE TableInsert;
 GO
 USE TableInsert;
  CREATE TABLE employee (
  PersonID int identity(1,1) primary key,
  FirstName varchar(30) NULL,
  LastName varchar(50) NULL,
  Age int,
  HiredDate date
  );
  Insert into employee(Firstname, LastName, age, HiredDate)
  values( 'Alan', 'Palmer', 32 , '2019-12-15'),
  ( 'Susan', 'Shepard', 28 , '2015-07-21'),
  ( 'Justin', 'Ward', 43 , '2017-08-24') ;
    
  SELECT * FROM employee;


Few observations: Note that I changed the types and I used date type for the HiredDate. It is always a good idea to use proper SQL data type in order to avoid bad data. Now, there is a debate if we want to use singular or plural for the table name. I would probably use plural. I also specified that Id colum is a primary key and it's incremented automatically.

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much this worked out, or at least no error msg is coming back. Would you be able to help me this is a project I'm working on to apply to a bootcamp, but have been stuck on this step for a while.

0 Votes 0 ·
NaomiNNN avatar image NaomiNNN FragosoArianaM-2038 ·

You can ask more questions as needed, but make sure to mention your platform as some syntax may differ for Postgre-SQL. This code is very basic, there were just couple of typos which I corrected. I suggest to accept my answer and ask new questions in a different thread if they are new issues.

0 Votes 0 ·

wheres the thread for Postgre-SQL

0 Votes 0 ·
Show more comments
NaomiNNN avatar image
0 Votes"
NaomiNNN answered BertZhoumsft-7490 commented

Try fiew corrections (not tested, but I can see these mistakes):

 CREATE DATABASE TableInsert;
    
 CREATE TABLE employee (
 PersonID int identity(1,1) primary key,
 FirstName varchar(30),
 LastName varchar(50),
 Age int,
 HiredDate date
 );
 Insert into tbl_employee(Fristname, LastName ,age, HiredDate)
 values( 'Alan', 'Palmer', 32 , '2019-12-15'),
 ( 'Susan', 'Shepard', 28 , '2015-07-21'),
 ( 'Justin', 'Ward', 43 , '2017-08-24') ;
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Im seeing this with your code

SyntaxError: Parse error on line 8: ..., HiredDate date, ); Insert into tbl_ ---------------------^ Expecting 'LITERAL', 'BRALITERAL', 'UNIQUE', 'INDEX', 'CONSTRAINT', 'CHECK', 'PRIMARY', 'KEY', 'FOREIGN', got 'RPAR'


And with my code
CREATE DATABASE TableInsert;


CREATE TABLE employee (

PersonID int,

FirstName varchar(255),

LastName varchar(255),

Age int,

HiredDate varchar(255),

);

Insert into table_employee(Fristname, Last name ,age, HiredDate)

values( ‘Alan’, ‘Palmer’, 32 , ‘2019-12-15),


I get
SyntaxError: Parse error on line 7: ...Date varchar(255), ); Insert into tabl ----------------------^ Expecting 'LITERAL', 'BRALITERAL', 'UNIQUE', 'INDEX', 'CONSTRAINT', 'CHECK', 'PRIMARY', 'KEY', 'FOREIGN', got 'RPAR'

0 Votes 0 ·
NaomiNNN avatar image NaomiNNN FragosoArianaM-2038 ·

There was an extra comma at the end of the create table statement, you need to remove it. See the code I just posted and tested in SQL Server (Transact-SQL syntax)

1 Vote 1 ·

Yes, I'm also guessing that the OP is using some other type of database, otherwise this basic syntax issue is hard to come by.

1 Vote 1 ·
BertZhoumsft-7490 avatar image
0 Votes"
BertZhoumsft-7490 answered

Hi,@FragosoArianaM-2038

Welcome to Microsoft T-SQL Q&A Forum!

A simple method is to add '' directly. SQL will treat it as a string, or you can use Ctrl + 0 to implement it.
First of all , please make sure that the database you are using is SQL Server , the syntax will be different if you are using other databases . like this:

  select @@version

From the code you posted, you have several basic syntax errors:

1) Although PersonID is defined, but you do not assign a value when inserting data into the table, naomi's identity self-increment can solve this problem;

2) Carefully observe, you use the wrong reference symbols, I guess it is the problem of your input method, such as ' and ' are different;

3) The last field cannot be added when the table is created, I think you should know why.

Hope it helps you.


Best regards,
Bert Zhou


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




image.png (15.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.