I'm using WampServer with phpmyadmin, but have also tried Xamp and same results.
I have 6 entries in the table called employees:
I have a table called employee with entries:
empId, lastName, firstName, department, position, and salary.
empId is primary key w/ auto increment. I'm following a tutorial online where they are using a mac computer, I'm on windows, in case that has something to do with the issue.
When I look at the SQL this produces it shows this:
SELECT * FROM `employee` WHERE 1
I don't see any CreateTable or other SQL.
Can anyone shed some light as to what is going on here?
Thank you.
UPDATE:
So if I go to > Export to SQL:
And this is what I get:
CREATE TABLE IF NOT EXISTS employee (
empId int(11) NOT NULL,
lastName varchar(40) NOT NULL,
firstName varchar(20) NOT NULL,
department int(2) NOT NULL,
position varchar(20) NOT NULL,
salary int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE employee
ADD PRIMARY KEY (empI);
Which looks like the correct SQL.
So at least I know it is creating the correct SQL even though its not showing on the phpmyadmin web gui page.
A CREATE TABLE statement is just a command to create a table. You can also create a table using the GUI of PHPMyAdmin, in which case it performs the necessary actions for you.
But those are just commands, blue-prints if you like. In the database itself the 'Create table' statement doesn't exist. The database contains an actual table which was created using such a statement. When you export the database (like you did) or ask for the create table statement, it just reverse-engineers the actual table structure into a statement, like drawing a blue-print based on an actual object you have.
PHPMyAdmin will often show the statement that it generated to build -for instance- a table, but in some cases you may not see it. It might be that the guy of the tutorial has a slightly different version of PHPMyAdmin, or he activated a feature that shows the statement.
Related
I recently started learning some languages: html, css and now PhP and MySql. I created a sign up, log in and log out system using this tutorial:
http://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
I'm using XAMPP to run Apache server, MySql and PhPMyAdmin. Everything seems to work fine, except for an issue with the primary key. When my form was completed I started adding some fictional user accounts to test it out. After that I deleted them. The username and password were deleted, but the Primary Key (ID) won't change. Even though the first row should be the first ID of 1, it is stuck at 3 because the rows with ID's 1 and 2 were deleted. With this as a result:
image of issue.
Can anyone help me with this?
That's the AUTO_INCREMENT behavior. As you can see here, you can modify your auto_increment setting something like this:
ALTER TABLE foo AUTO_INCREMENT=1
But this isn't recommended.
In the table creation the id is set to auto_increment
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
This causes the numbers to keep incrementing even though they may not be in the database.
You can reset the auto_increment value:
ALTER TABLE `table_name` AUTO_INCREMENT=1
You can either truncate your table (Operations tab in PHPMyAdmin) or run the following query:
ALTER TABLE `mytable` AUTO_INCREMENT = 1;
But just truncate it, it's best to just do that.
I am using PHP to update/insert/delete records in MySQL innodb. I have about 4 millions vcard data which are stored in the MySQL innodb. But there is a problem for only one recored.
For this special record, I can not update, select, delete that record by the primary key. The error return is:
ERROR 2013 (HY000): Lost connection to MySQL server during query
MySQL version: mysql版本:5.5.21-log
I do not even know what's the potential problem, any hint?
The table structure is:
CREATE TABLE vcard (
username varchar(250) PRIMARY KEY,
vcard mediumtext NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB CHARACTER SET utf8;
The table is designed from here
The search sql is: select * from rosterusers where username = '3781353';
I can't comment because of my less reputation,so have to post in answer only.
This error appears when the query runs in an infinite loop. so please share your table structures and your query that you wrote.
Anyway error is probably in your where clause.
So,please share the information so that we can help you out with proper query.
I'm learning SQL. I find very few information about command line on the web. In every tutorial I've seen on the net they stay for the create/drop/select commands and then pass to phpmyadmin.
I would like to know how to do the following by command line:
- I have 3 tables, imagine: users, cars, fuel;
- Every user has a car (from cars table) and every car has a fuel (from fuel table).
I would like to create relations between them. Not only the user could only select the cars from Cars table but I could check (by join command) who has that car type and what is spending each user for the fuel of that car.
I just want to learn how to do this by command line, it's a self made exercise to be able to solve larger problems, because relationship is one of the most important things on databases and I don't know how to use it and can't find it anywhere.
If you like do work in command line you can do it by
shell> mysql --user=[user_name] --password=[your_password db_name]
this will start the mysql command line tool. It looks like this:
mysql>
now you can do some queries like this:
mysql> show databases; // to list all your dbs
mysql> use [db_name]; // to change to your db
mysql> show tables; // to list all tables of your db
see the manual: http://dev.mysql.com/doc/refman/5.6/en/mysql.html
You can do what you want in command line but it is much more comfortable to use a gui tool like sqlyog, heidisql or the webbased phpmyadmin tool. You can see and edit your data/databases/tables here and you are able to send the same queries via your favourite gui tool.
Using constraints
If you ask for creating relations it sounds like you want to create constrains.
You dont really need constrains for a database schema. It is possible to build what you want without any constraint. The relations itself are only in your brain.
Little example with users and cars (MYISAM tables)
table user
- id_user (int)
- name (varchar)
table car
- id_car (int)
- name (varchar)
- fi_user (int)
Create Query:
CREATE TABLE `user` (
`id_user` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 64 ) NOT NULL
) ENGINE = MYISAM ;
CREATE TABLE `car` (
`id_car` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 64 ) NOT NULL ,
`fi_user` INT NOT NULL
) ENGINE = MYISAM ;
This is all you need. Simple put the id of your user into the fi_user field of your car and you have a relation.
INSERT INTO `user` (`id_user`,`name`) VALUES (1, 'testuser');
INSERT INTO `car` (`id_car`, `name`, `fi_user`) VALUES (1, 'testcar', 1);
Now you can do:
SELECT * FROM `car` c JOIN `user` u ON (u.id_user=c.fi_user) WHERE 1
It is possible to use constraints with INNODB but you dont have to use them.
I prefere MYISAM because it is faster. Please read this to find out what constraints are and why you may like to use it
http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
Database table schema.
CREATE TABLE `stackoverflow`.`automatic` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`values` VARCHAR( 200 ) NOT NULL ,
`counts` BIGINT NOT NULL
) ENGINE = InnoDB;
now i want to update counts everytime data is updated automatically in mysql without hitting database.
I mean i know i can select that row and get old value and then increament it by one but my concern is that whether there's any way that mysql handle such things.
I didn't tried any code.
I just wanted to know from experts out there if they know anything about it.
As i am running on deadline i don't wanna use lengthy approach and i thought it would be cool if mysql already has something that could help me now.
Thanks.
You can do it in your update query itself
UPDATE `stackoverflow`.`automatic` set values='xyz', counts=(counts+1) where id=1;
I am creating a two MySQL tables in PHP, using the code as given below:
$sql = "CREATE TABLE qotwMember
(
MemberId NOT NULL PRIMARY KEY,
Name varchar(255),
Passwork varchar(255),
emailId varchar(255),
)";
$sql = "CREATE TABLE qotwQuestion1111
(
QuestionId NOT NULL AUTO_INCREMENT,
Question varchar(5000),
MemberId varchar(255) FOREIGN KEY fkname REFERENCES qotwMember(MemberId),
PostDate date,
Vote int,
PRIMARY KEY (QuestionId)
)";
mysql_query($sql,$con);
Then i try to insert data into these tables. In the qotwMember table, the data gets entered, but when I try to insert data into the qotwQuestion1111 table, it gives me the error "Error: Table 'database1.qotwQuestion1111' doesn't exist"
I can not figure out what I am doing wrong here. Please help me with this problem.
Note: Both the tables have been created in a different php.
Regards
Zeeshan
Are you sure you are selecting the right database each time? See: mysql_select_db()
I suspect you're not giving us the real SQL because neither of those statements will actually work - you're missing the datatype for the primary column and have some extra commas.
If that is your real SQL, then make sure you put or die(mysql_error($con)); after calls to mysql_query
When you are creating your tables, it is probably easier to use a MySQL front end such as MySQL query browser instead of trying to run the CREATE TABLE statements inside PHP. My guess is there is a syntax error in your second statement, so the table is not getting created. The front end will show you what the syntax error is.
Alternatively, you could check the return value of mysql_query to see if there is an error, and then use mysql_error() to read it out.
I have had the same problem as you with foreign key creation in MySQL (which is what your error is about).
When creating foreign keys, both the foreign key column and the reference column must be of the same data type and size. I noticed you did not give your primary key column any datatype or size. This is probably what is causing your error. Also, as others have pointed out, what engine you are using also will dictate if you can use foreign keys.
If you declare 'MemberID' as 'MemberID varchar(255) NOT NULL PRIMARY KEY' it should work as you have it now. I would suggest always giving your primary key columns a datatype and possible size. I don't know what your tables are for, but for a primary key column that is just an ID, i would recommend making it an INT of some sort (just remember to change your foreign key column to reflect that change).