At the site Sea Quail Database Diagram Tool which is great for creating database diagrams I just created a MySQL database schema diagram - very basic one to get a feel of it.
I created the script to run from PHPMyadmin to add this all to a database created there to see if importing would work OK, but I ran into this 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 '--------------------------------------------------------------------------------' at line 2
Here is a snippet of the SQL data that was generated for me.
-- Create Table: name_fields
--------------------------------------------------------------------------------
CREATE TABLE name_fields
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_api
--------------------------------------------------------------------------------
CREATE TABLE name_api
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_users
--------------------------------------------------------------------------------
CREATE TABLE name_users
(
`ID` BIGINT NULL
,`user_login` VARCHAR(250) NULL
,`user_name` VARCHAR(250) NULL
,`user_password` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_options
--------------------------------------------------------------------------------
CREATE TABLE name_options
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_ideal
Not sure what is wrong with the syntax here. Is there an easy way to debug this in MySQL?
Yes you have syntax error
First of all the
-- Create Table: name_fields
-------------------------------------------------------------------------------
Should be in one line you need to check this
something like
-- Create Table: name_fields ------------------------------------------------------
2nd thing
CREATE TABLE name_fields
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
Where is the column name ? you have just specified ``. The same is for name_api table too, correct these and it should work.
Related
I have a php page where the user enters the following details
Name
Age
But in the database, I want to have an additional column that automatically inserts data with the combination of entered data as shown below for Code column.
Key
Name
Age
Code
1
Mike
28
MIKE128
2
Raj
32
RAJ232
So I have altered my table as follow
ALTER TABLE Employee ADD COLUMN Code;
UPDATE Employee SET Code = CONCAT(Key,Name,Age);
But this updates only the existing data for existing data,and shows NULL for newly entered data. What is the command I shall use to change the schema so that whenever I add new data automatically it updates the Code column
Below is my try, which is failed
CREATE TABLE `Employee` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`Age` int(2) NOT NULL,
`Combined` varchar(10) AS (`Name` + `id` + `Age`) Code,
PRIMARY KEY (`id`)
);
ERROR 1064 (42000) at line 2: 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 'Code,
PRIMARY KEY (id)
)' at line 4
Note: I DO NOT want to concate and supply data from the front end.
Note: This is just example as I can not post real data.
You can setup trigger for dynamic default value
CREATE TRIGGER Employee_code_trigger
BEFORE INSERT ON Employee
FOR EACH ROW
SET NEW.Code = IFNULL(NEW.Code, CONCAT(NEW.Key, NEW.Name, NEW.Age));
I am trying to create database android_api and table users but I am getting error
#1064 - Something is wrong in your syntax near 'use android_api
create table 'users'(
id int(11) NOT NULL primary KEY AU' w linii 3
Here is the code
create database android_api
use android_api
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
You should separate each SQL statement with ;, otherwise your syntax is incorrect.
Here is the correct syntax:
create database android_api;
use android_api;
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
In your code what you did was actually:
create database android_api use android_api
(Which is not a valid CREATE statement).
Actually, the accepted answer is enough. This is a very small idea to share after running into the same problem as you report it, but in a beginner's tutorial Load PostgreSQL Sample Database in an SQL Shell.
In general, if you have the reported error in an SQL shell, and if you are not sure whether you have typed ; at the end of the last line, just type ; as a new command and press Enter to be sure that you type your next command in LINE 1 and not in LINE 2.
Without this "trick", some stupidity like this can happen:
postgres=# CREATE DATABASE dvdrental
postgres-# show databases
postgres-# create database dvdrental;
ERROR: syntax error at or near »show«
LINE 2: show databases
^
postgres=# create database dvdrental
postgres-# CREATE DATABASE dvdrental;
ERROR: syntax error at or near »CREATE«
LINE 2: CREATE DATABASE dvdrental;
^
postgres=# CREATE DATABASE dvdrental;
CREATE DATABASE
Finally working, as can be seen by the output CREATE DATABASE.
I have written an sql script with the following contents :
CREATE DATABASE IF NOT EXISTS stock_trading;
USE stock_trading;
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name,passwrd),
)ENGINE=InnoDB;
and every time I try to run it in the SQL command prompt it keeps giving away the error as:
ERROR 1064 (42000) at line 5 in file: 'db_script.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')ENGINE=InnoDB' at line 5
the script appears to be correct but I don't know why it keeps giving this error.
Additional information:
Operating System : Arch Linux
Database : MariaDB
You have a stray comma at the end of your table definition:
PRIMARY KEY (user_name,passwrd),
^^^ remove this
Your full table definition:
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name, passwrd)
) ENGINE=InnoDB;
I am creating several logbooks on the same site—all with the same structure, so when I need to add a new logbook, I'd like to be able to do so with a simple function or something (since the table-structure will be exactly the same.)
DELIMITER //
CREATE FUNCTION GenerateNewLogbook(LogbookTitle varchar(15))
BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTitle (
time timestamp NOT NULL DEFAULT NOW(),
username varchar(25) DEFAULT NULL,
message text,
crossout tinyint(1) NOT NULL DEFAULT '0',
post_id mediumint(9) NOT NULL AUTO_INCREMENT,
file_id text,
PRIMARY KEY (post_id)
) ENGINE=InnoDb AUTO_INCREMENT=0 DEFAULT CHARACTER SET=latin1;
END//
DELIMITER ;
The error I'm getting is:
ERROR 1064 (42000): 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 'BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTit' at line 2
I've tried many things: Replacing the ";" with "//", creating a PROCEDURE instead of a FUNCTION, adding "#" before LogBookTitle, removing BEGIN/END, etc. and have spent quite a while searching for examples to no avail.
What's wrong with the above code?
When i try to import my DB i get this error. I don't know what I should do, please help.
--
-- Database: oddjobexchange
--
-- ---------------------------------------------------- --
--
TABLE structure FOR TABLE admin --
CREATE TABLE IF NOT EXISTS admin (
Email varchar( 30 ) NOT NULL ,
Password varchar( 35 ) NOT NULL ,
PRIMARY KEY ( Email )
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
MySQL said: Documentation
#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 'Table structure for table admin
CREATE TABLE IF NOT EXISTS admin (
`Em' at line 10
help!
There needs to be a space after the first two dashes. Otherwise it is not a valid comment:
------------------------------------------------------ --
should be:
-- ---------------------------------------------------- --
Just need to add the follwing to top of file.
CREATE DATABASE databasename;
USE databasename;