How do i create in a table in phpmyadmin? - mysql

Whenever I go to sql in phpmyadmin to create a table, I put this code in:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
it says:#1046 - No database selected
but when I put:
CREATE DATABASE data;
USE data;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
it says that it could not create the database?

Step 1: Choose an existing Database or create a new Database.
Step 2: Choose the SQL tab and add your code to create a table.

One server can contain multiple databases. If you login into PHPMyAdmin, you choose a server to login to. After you've logged in, you have to select your database to be able to make changes to it.
So you don't need to create a database. You just need to select it. You can do that with the USE <databasename> statement, or though the PHPMyAdmin interface. In your second snippet, you already used have the USE statement. You just need to remove the CREATE DATABASE statement.
Same goes for creating the table, by the way. PHPMyAdmin provides an interface to add a table field by field without having to write the SQL for it. Although it can't hurt to know the syntax, of course.

Since you have got your sql statement, then just go to
phpMyAdmin > login (if there is need for it) > select database you want to use,
find it in Right Hand Side , example 'test'. tehn simply just find the
tab named sql and click it, after that just paste your statements

Related

Notice in phpmyadmin 5.0.1: Trying to access array offset on value of type bool, notice in Designer mode

I'm doing some very basic database exercises in phpMyAdmin like creating tables using SQL queries and observing it in designer mode.
But I realized there is an error shown there but when I was typing the commands to create the tables, there are no mistakes. But in designer mode, there is one line in each of my tables that is in red. The notice in the designer mode says "trying to access array offset on value of type bool".
When I ignored it and proceeded to create relations to form an ERD, I can't because the line that is in red was part of the constraint. I wonder what could be the problem. I've double-checked my queries and cross-checked the queries with my friends, drop all tables and re-create them and even re-installing xampp and phpMyAdmin. I'll add a query for one of my tables. For this particular table, the line customerName appears red in designer mode.
DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) DEFAULT NULL,
`postalCode` varchar(15) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11) DEFAULT NULL,
`creditLimit` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`customerNumber`),
KEY `salesRepEmployeeNumber` (`salesRepEmployeeNumber`))
ENGINE=InnoDB DEFAULT CHARSET=latin1;

MYSQL View Slowed Down

I have a mysql view that slowed down when the database it was interpreting became overloaded due to a programming flaw. I fixed that flaw and cleared the database back down to a very small size.
I have a database with identical tables and identical data on the same host/server. It has the same view. The query for that view finishes well under 1sec, but the query of the problem view takes 20+sec.
Is there a cache or something else that I may need to clear to fix this?
select
`income`.`id` AS `id`
from
(`clientsWithIncome`
left join `income` ON (`income`.`client` = `clientsWithIncome`.`client`))
The above is the most simplified form of the query that gives me the problem. I can perform this on one database and get results in under one second, and then on the problem database it takes 20 seconds.
delimiter $$
CREATE TABLE `income` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`index` varchar(45) DEFAULT NULL,
`client` varchar(45) DEFAULT NULL,
`entity_type` varchar(45) DEFAULT NULL,
`date_received` varchar(45) DEFAULT NULL,
`preparer` varchar(45) DEFAULT NULL,
`date_began` varchar(45) DEFAULT NULL,
`date_prepared` varchar(45) DEFAULT NULL,
`date_assembled` varchar(45) DEFAULT NULL,
`date_signed` varchar(45) DEFAULT NULL,
`date_fed` varchar(45) DEFAULT NULL,
`date_state` varchar(45) DEFAULT NULL,
`notes` text,
`status` varchar(45) DEFAULT NULL,
`year` varchar(45) DEFAULT NULL,
`highlight` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=93442 DEFAULT CHARSET=latin1$$
And the VIEWs:
delimiter $$
CREATE ALGORITHM=UNDEFINED DEFINER=`xxxxxxxxxxx`#`localhost`
SQL SECURITY DEFINER VIEW `clientsWithIncome` AS
select `income`.`client` AS `client`
from `income`
where (`income`.`year` = (year((curdate() + interval 1 month)) - 1))
union
select `contacts`.`client` AS `client`
from `contacts`
where (`contacts`.`tax_returns` = 1)$$
delimiter $$
CREATE ALGORITHM=UNDEFINED DEFINER=`xxxxxxxxxxxx`#`localhost`
SQL SECURITY DEFINER VIEW `currentIncome` AS
select `income`.`id` AS `id`,
`income`.`index` AS `index`,
`clientsWithIncome`.`client` AS `client`,
`income`.`entity_type` AS `entity_type`,
`income`.`date_received` AS `date_received`,
`income`.`preparer` AS `preparer`,
`income`.`date_began` AS `date_began`,
`income`.`date_prepared` AS `date_prepared`,
`income`.`date_assembled` AS `date_assembled`,
`income`.`date_signed` AS `date_signed`,`income`.`date_fed` AS `date_fed`,
`income`.`date_state` AS `date_state`,`income`.`notes` AS `notes`,
`income`.`status` AS `status`,`income`.`year` AS `year`,
`income`.`highlight` AS `highlight`
from (`clientsWithIncome`
left join `income`
on(((`income`.`client` = `clientsWithIncome`.`client`)
and (`income`.`year` = (year((curdate() + interval 1 month)) - 1))))
)$$
My answer turned out to be what I thought it was. Some sort of caching.
I performed the following from PHPmyAdmin:
Check table
Repair table
Optimize table
Flush table
Now the queries are performing wonderfully.
year varchar(45) DEFAULT NULL,
There is a YEAR datatype; use it.
Add INDEX(year)
Add INDEX(client)
Add INDEX(tax_returns)
If those suggestions don't help enough, come back with...
Please provide SHOW CREATE TABLE for the other table.
Please provide EXPLAIN SELECT ...
Use NOT NULL on columns that you will always set.

What is wrong with my SQL here? #1089 - Incorrect prefix key

CREATE TABLE `table`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`(11))
) ENGINE = MyISAM;
I'm getting the #1089 - Incorrect prefix key error and can't figure out what I'm doing wrong.
In your PRIMARY KEY definition you've used (id(11)), which defines a prefix key - i.e. the first 11 characters only should be used to create an index. Prefix keys are only valid for CHAR, VARCHAR, BINARY and VARBINARY types and your id field is an int, hence the error.
Use PRIMARY KEY (id) instead and you should be fine.
MySQL reference here and read from paragraph 4.
If you are using a GUI and you are still getting the same problem. Just leave the size value empty, the primary key defaults the value to 11, you should be fine with this. Worked with Bitnami phpmyadmin.
This
PRIMARY KEY (id (11))
is generated automatically by phpmyadmin, change to
PRIMARY KEY (id)
.
There is a simple way of doing it. This may not be the expert answer and it may not work for everyone but it did for me.
Uncheck all primary and unique check boxes, jut create a plain simple table.
When phpmyadmin (or other) shows you the table structure, make the column primary by the given button.
Then click on change and edit the settings of that or other colums like 'unique' etc.
CREATE TABLE `table`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`(11))
) ENGINE = MyISAM;
Change To
CREATE TABLE `table`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
Here the full solution step by step
First of all you have to make the table by inserting all the data. id should AI ticked.
then press go and #1089 error will be pop-up
here is the solution
theres a button near go called preview SQL
click that button and copy the sql code
then click on SQL tab on top of the window
Clear the text filed and paste that copied code there.
you will be see (id (11)) this on bottom of the code
replace (id (11)) into (id)
and click go
boom now you will be fine
In my case, i faced the problem while creating table from phpmyadmin. For id column i choose the primary option from index dropdown and filled the size 10.
If you're using phpmyadmin, to solve this problem change the index dropdown option again, after reselecting the primary option again it'll ask you the size, leave it blank and you're done.
It works for me:
CREATE TABLE `users`(
`user_id` INT(10) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE = MyISAM;
When you give id as a primary key then a pop up is come and those aske you to how many size of this primary key.
So you just leave blank because by default int value is set 11. Click then ok on those pop up without any enter a number.
in this type of error never will you face in future.
Thank you 😊
Problem is the same for me in phpMyAdmin. I just created a table without any const.
Later I modified the ID to a Primary key. Then I changed the ID to Auto-inc.
That solved the issue.
ALTER TABLE `users` CHANGE `ID` `ID` INT(11) NOT NULL AUTO_INCREMENT;
I also had this same problem.
Solution work for me:
CREATE TABLE IF NOT EXISTS `users` (
`sr_no` int(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`sr_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
I paste this code in SQL and run, it works fine.
In PHPMyAdmin, Ignore / leave the size value empty on the pop-up window.
according to the latest version of MySQL (phpMyAdmin), add a correct INDEX while choosing primary key. for example: id[int] INDEX 0 ,if id is your primary key and at the first index.
Or,
For your problem try this one
CREATE TABLE `table`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
This worked for me:
CREATE TABLE `table`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`dir` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`(id))
) ENGINE = MyISAM;
No need to put id(11) because, by default, it is equal to 11 so you leave
it as id and in phpmyadmin you leave it empty.
Drop the table.user and just use user
The lenght of the id was alread specified in the
id INT(11) and does not need to be specified in the PRIMART KEY.
CREATE TABLE users
(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
dir VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;

MySQL to Postgres syntax

I am working on a maven project and for that I am using Postgres as database. Unfortunately the database I know more is MySQL. I have the basic details of SQL file which I want to include in the database. If possible, can someone with knowledge of postgres help me to convert the syntax. One more question, for maven project, do I need to include the .sql file within the project(if yes, where). Kindly let me know.
person.sql.
CREATE TABLE `Person` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
`country` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Thank you.
CREATE TABLE Person
(
id bigserial NOT NULL primary key,
name varchar(20) NOT NULL DEFAULT '',
country varchar(20) DEFAULT NULL
);
More details in the manual: http://www.postgresql.org/docs/current/static/sql-createtable.html

Error #1064 - SQL syntax error

First and foremost, I am a total novice when it comes to SQL, and I'm trying to build a database for my web design course. Having tried a thorough google search and exploring some of the answers here, I'm still no closer to figuring out what my problem is. The error that keeps getting thrown out is the title of this question, but this is the code I have so far:
Table structure for table `members`
create database glasgowboys;
use glasgowboys;
CREATE TABLE `members`
(`ID` int(11) NOT NULL AUTO_INCREMENT,
`Email` varchar(255) NOT NULL,
`Password` varchar(50) NOT NULL,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I've been through several similar questions on stackoverflow, following their advice on replacing quotes with backticks, but with no luck.
When using auto_increment, make the field primary key. Also, remove the extra , after the last field definition.
Try this:
CREATE TABLE `members`
(`ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Email` varchar(255) NOT NULL,
`Password` varchar(50) NOT NULL,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Around table or field names, you must put "[]", not "'" and certainly not "`".
This should be
CREATE TABLE members
([ID] int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
[Email] varchar(255) NOT NULL,
[Password] varchar(50) NOT NULL,
[FirstName] varchar(255) NOT NULL,
[LastName] varchar(255) NOT NULL)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
(Maybe still not exactly correct, I only have Sql Server for testing around just now)