6 Replies - 2232 Views - Last Post: 13 January 2013 - 03:29 AM

#1 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 31-October 12

syntax for creating a table in mysql

Posted 04 January 2013 - 11:38 PM

This is an easy one for those with more experience:) I decided tonight in my free time to start playing around. I installed apache on CentOS, then installed mysql. Created database on mysql. But when I try to create a table, it either gives me the > prompt again like it's waiting for something or gives me a syntax error. I looked on mysql's website, which would be the obvious, but I'm better at examples instead of what they have. But here is my code.

First I use USE test;


CREATE TABLE entries {
'id' int NOT NULL AUTO_INCREMENT, 'title' NOT NULL, 'date' date NOT NULL default '0000-00-00', 'title' NOT NULL, 'subject' NOT NULL, 'context' NOT NULL, 'PRIMARY KEY(id) };



I don't know if it is 100% needed to put the varchar() with the fields since it should be implied to put anything there. But maybe I'm wrong in this?

Maybe most people use phpmyadmin. But I want to do it from the command line. Thanks!

This post has been edited by macosxnerd101: 04 January 2013 - 11:40 PM
Reason for edit:: Code tags are BBCode, not XML. Please [code] Code goes here [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: syntax for creating a table in mysql

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,335
  • Joined: 27-December 08

Re: syntax for creating a table in mysql

Posted 04 January 2013 - 11:43 PM

The SQL Syntax uses parentheses instead of curly braces. Also, it is correct to put the varchar with the field definitions, as you are defining their types.

Also, formatting your code properly will make it easier to read (with the fix from braces to parentheses):
CREATE TABLE entries (
    'id' int NOT NULL AUTO_INCREMENT, 
    'title' NOT NULL, 
    'date' date NOT NULL default '0000-00-00', 
    'title' NOT NULL, 
    'subject' NOT NULL, 
    'context' NOT NULL, 
    'PRIMARY KEY(id) 
);



Beyond that, if you are encountering errors, please post them exactly as they appear.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: syntax for creating a table in mysql

Posted 05 January 2013 - 05:46 AM

it’s also a good idea to have a look at the appropriate Manual section. it might be somewhat confusing at first, but follows a definite logic.

@mac: are you sure ' are valid for column names?

This post has been edited by Dormilich: 05 January 2013 - 05:47 AM

Was This Post Helpful? 3
  • +
  • -

#4 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 31-October 12

Re: syntax for creating a table in mysql

Posted 05 January 2013 - 06:43 PM

I tried it with the () and also with and without the '. But still syntax error.
Error 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near line 2

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 codespook  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 31-October 12

Re: syntax for creating a table in mysql

Posted 05 January 2013 - 08:43 PM

Fixed it. I forgot to put the varchar like it needs. ONce I did that and left out the ' it went fine:)
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,335
  • Joined: 27-December 08

Re: syntax for creating a table in mysql

Posted 06 January 2013 - 12:53 PM

Quote

@mac: are you sure ' are valid for column names?

I don't remember off the top of my head. I was just indenting code. :)
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: syntax for creating a table in mysql

Posted 13 January 2013 - 03:29 AM

View PostDormilich, on 05 January 2013 - 12:46 PM, said:

@mac: are you sure ' are valid for column names?

For the record: No, they are not. Those are only used for values, not identifiers. Only the back-ticks, `, are valid quotes for identifiers. (Except when in ANSI_QUOTES mode, but that's not enabled by default.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1