So I made a change from 'Chemistry' to 'Physic' on 'major' column but as soon I executed it nothing changes and shows the same result. Why? (I used MySql Workbench)
Here's the original code:
CREATE TABLE people_data1 (
id INT,
username VARCHAR(20),
major VARCHAR(20) UNIQUE,
PRIMARY KEY(id)
);
# Insert some data into the table
INSERT INTO people_data1(id,username) VALUES(3,'Clara'); # Person with no major
INSERT INTO people_data1 VALUES(51,'Mr Bald','Chemistry'); # Change to 'Physic'
SELECT * FROM people_data1;
The new one:
CREATE TABLE people_data1 (
id INT,
username VARCHAR(20),
major VARCHAR(20) UNIQUE,
PRIMARY KEY(id)
);
# Insert some data into the table
INSERT INTO people_data1(id,username) VALUES(3,'Clara'); # Person with no major
INSERT INTO people_data1 VALUES(51,'Mr Bald','Physic');
SELECT * FROM people_data1;
Inserts add new records; you want an update here:
UPDATE people_data1
SET major = 'Physics'
WHERE id = 51;
Run this update after your first two insert statements to get your desired results.
Need help how to solve this problem...
I have created a users table which has following columns
Create table users
(
uid int(10) PRIMARY KEY AUTO_INCREMENT,
uname varchar(50),
password varchar(50),
email varchar(50)
);
when i insert values with uid it executes successfully :
Insert into users values(1,'ABC','Helloworld','ABC#gmail.com');
but when i try without uid
Insert into users values('SDC','Helloworld','SDC#gmail.com');
it does not execute successfully and gives an error
ERROR 1136 (21S01): Column count doesn't match value count at row 1
my uid has AUTO_INCREMENT so it should automatically increase..
Of course auto_increment is working correctly. You just need to learn best practices about using insert. Always list all the columns (unless you really, really know what you are doing):
Insert into users (uname, password, email)
values('SDC', 'Helloworld', 'SDC#gmail.com');
The id column will be auto-incremented. If you don't list the columns, then MySQL expects values for all columns, including the auto-incremented one.
Can't seem to figure out why this simple statement doesn't work
tx.executeSql("INSERT INTO history SELECT * FROM scan");
It works correctly if the table history is empty which is not of much use but if the table history has any data then it does not carry out the insert I must do:
tx.executeSql("DELETE FROM history", []);
tx.executeSql("INSERT INTO history SELECT * FROM scan");
Any ideas? Cheers
Edit:
Structures are the same:
tx.executeSql("CREATE TABLE IF NOT EXISTS scan(ID INTEGER NOT NULL PRIMARY KEY, sunum TEXT, binnum TEXT, userid TEXT, added_on DATETIME, upload_on DATETIME)");
tx.executeSql("CREATE TABLE IF NOT EXISTS history(ID INTEGER NOT NULL PRIMARY KEY, sunum TEXT, binnum TEXT, userid TEXT, added_on DATETIME, upload_on DATETIME)");
The problem is that you're attempting to insert a duplicate primary key value into the History table. From your structure, they both have ID listed as a PRIMARY KEY, which cannot contain duplicate values.
Try specifying all columns except for that key:
INSERT INTO History
(sunum, binnum, userid, added_on, upload_on)
SELECT sunum, binnum, userid, added_on, upload_on
FROM Scan
Though, looking at the structure, the ID values aren't auto-incremented. If you don't care what the ID is, you can declare the ID column as: ID INT NOT NULL AUTO_INCREMENT.
If you need to pull the ID over from the other table, you'll have to do an upsert or merge into that table.
I have a preexisting sqlserver table. Among other fields it has an identity column called as ID which is also a primary key and a RecordNumber column which is a required field. The int value in the RecordNumber column has to be unique. So before inserting a row, I get the max value of the ID column, add 1 to it and then inserting the row with the RecordNumber field = ID + 1. The problem is when two users try to save at the same time, they may get the same ID value and hence will save the same value in the RecordNumber field. Please let me know how to resolve this.
Thanks
The simplest and most efficient way to do this is to define that column as *auto increment *
refer
http://www.w3schools.com/sql/sql_autoincrement.asp
You can use the Transaction concept, using Commit and Rollback operations.
Very interesting link on MSDN : http://msdn.microsoft.com/en-gb/library/ms190295.aspx
Alternative #1 - Auto Increment a Field
CREATE TABLE SampleTable
(
P_Id int NOT NULL Identity(1,1),
FirstName varchar(255),
PRIMARY KEY (P_Id)
)
Alternative #2 - SequentialID as Default Constraint
CREATE TABLE SampleTable
(
P_Id uniqueidentifier NOT NULL,
FirstName varchar(255),
PRIMARY KEY (P_Id)
)
ALTER TABLE [dbo].[SampleTable]
ADD CONSTRAINT [DF_SampleTable_P_Id]
DEFAULT newsequentialid() FOR [P_Id]
Alternative #3 - NewID as Default Constraint
CREATE TABLE SampleTable
(
P_Id Varchar(100) NOT NULL,
FirstName varchar(255),
PRIMARY KEY (P_Id)
)
ALTER TABLE [dbo].[SampleTable]
ADD CONSTRAINT [DF_SampleTable_P_Id]
DEFAULT (newid()) FOR [P_Id]
Sample recommended Stored Proc should be used in case of Muti User Transaction
BEGIN TRY
SET NOCOUNT ON
SET XACT_ABORT ON
Begin TRAN
--Your Code
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
I am trying to create a database in SQL Server but it keeps throwing errors. Could you look at the script and see if I am messing it up somehow?
drop database if exists cptc;
create database cptc;
use cptc;
drop table if exists staff;
create table staff (
staffID int not null auto_increment,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID),
FULLTEXT (fname,lname)
)ENGINE = MYISAM;
INSERT INTO staff VALUES ('','michael','herrera','doctor','2539703420','B14','122');
INSERT INTO staff VALUES ('','holly','herrera','teacher','2534667896','B35','116');
INSERT INTO staff VALUES ('','jesse','kirsch','professor','2534567890','B12','112');
INSERT INTO staff VALUES ('','mark','wahlberg','professor','5552345678','B01','112');
INSERT INTO staff VALUES ('','philip','spears','technician','2065672345','B12','123');
INSERT INTO staff VALUES ('','andrew','jackson','teacher','2061234567','B32','101');
INSERT INTO staff VALUES ('','annie','smith','mechanic','2533345609','B23','102');
INSERT INTO staff VALUES ('','alfred','hills','teacher','2535821513','B14','103');
INSERT INTO staff VALUES ('','bobby','jones','nurse','5559876056','B10','104');
INSERT INTO staff VALUES ('','tiffany','jones','janitor','2539981265','B02','108');
UPDATE: I think I was having Errors because it is MySQL and NOT SQL. Thanks everyone For Your (Really Quick) Answers!
This part is MYSQL not SQL Server ENGINE = MYISAM;
here is what the table would look like in SQL Server (sans the fulltext index, you first need to enable fulltext search in order to create a full text index))
create table staff (
staffID int not null identity,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID)
)
The drop and create database will look like this
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases
WHERE name = N'cptc')
DROP DATABASE cptc
GO
CREATE DATABASE [cptc]
The drop table will look like this
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'staff') AND type in (N'U'))
DROP TABLE staff
GO
Finally, you need to ommit the column for the identity while inserting, example
INSERT INTO staff VALUES ('michael','herrera','doctor','2539703420','B14','122');
If you don't ommit the column, you will get the following error
Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'staff' can only be specified when a column list is used and IDENTITY_INSERT is ON.
auto_increment should be identity(1,1)
ENGINE = MYISAM; doesnt exist what do you expect with it?
FULLTEXT (fname, lname) is not correct. What do you want, implement full text serach?
The question was not clear. You were creating a table not database.
It would be helpful if you provide the actual error detail.
BTW, you shouldn't put any value for the autoincrement column which will be assigned by server automatically. I guess this was causing the issue.