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.
Related
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.
i would like that whenever i insert a row the newly generated id be used as a username and also get inserted in the user_name field. Any idea on how how i can achieve this will be much appretiated.
I have tried to run the below code on mysql workbench,and everything seem to work fine except for the insert statement.
create database if not exists mydatabase;
create table if not exists mydatabase.mytable
(
id int auto_increment not null,
user_name int not null,
pass_word varchar(100),
primary key(id)
);
insert into mydatabase.mytable(user_name,pass_word) values (select SCOPE_IDENTITY(),'mypass');
Not sure if your version of sql supports multiple auto_increment columns. However I'm thinking you could either create a trigger or insert the following every time you do an insert:
update my_table
set user_name = id
So I'm new to the use of multiple tables. Prior to today, 1 table suited my needs (and I could probably get away with using 1 here as well).
I'm creating a plugin for a game I play but I'm using a MySQL database to store all the information. I have 3 tables, Players, Warners and Warns. Warns has 2 foreign keys in it (one referencing to Players and the other to Warners).
At the moment I need to do 3 queries. Add the information to Players & Warners, and then to Warns. Is there a way I can cut down the amount of queries and what would happen if I were to just omit the first 2 queries?
Query Examples:
INSERT INTO slimewarnsplayers VALUES ('123e4567-e89b-12d3-a456-426655440000', 'Spedwards');
INSERT INTO slimewarnswarners VALUES ('f47ac10b-58cc-4372-a567-0e02b2c3d479', '_Sped');
INSERT INTO slimewarnswarns VALUES ('', '123e4567-e89b-12d3-a456-426655440000', 'f47ac10b-58cc-4372-a567-0e02b2c3d479', 'spamming', 'medium');
Tables:
CREATE TABLE IF NOT EXISTS SlimeWarnsPlayers (
uuid VARCHAR(36) NOT NULL,
name VARCHAR(26) NOT NULL,
PRIMARY KEY (uuid)
);
CREATE TABLE IF NOT EXISTS SlimeWarnsWarners (
uuid VARCHAR(36) NOT NULL,
name VARCHAR(26) NOT NULL,
PRIMARY KEY (uuid)
);
CREATE TABLE IF NOT EXISTS SlimeWarnsWarns (
id INT NOT NULL AUTO_INCREMENT,
pUUID VARCHAR(36) NOT NULL,
wUUID VARCHAR(36) NOT NULL,
warning VARCHAR(60) NOT NULL,
level VARCHAR(60) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (pUUID) REFERENCES SlimeWarnsPlayers(uuid),
FOREIGN KEY (wUUID) REFERENCES SlimeWarnsWarners(uuid)
);
Is there a way I can cut down the amount of queries?
NO, I don't see that. From your posted INSERT statements (as depicted below) it's clear that those are 3 different tables and you are inserting different data to them. so, you will have to perform the INSERT operation separately for them.
INSERT INTO slimewarnsplayers
INSERT INTO slimewarnswarners
INSERT INTO slimewarnswarns
Another option would be (May not be considered good), creating a procedure which will accept the data and table name and create a prepared statement/dynamic query to achieve what you are saying. something like (A sample pseudo code)
create procedure sp_insert(tablename varchar(10), data1 varchar(10),
data2 varchar(10))
as
begin
--dynamic query here
INSERT INTO tablename VALUES (data1, data2);
end
To explain further, you can then call this procedure from your application end passing the required data. Do note that, if you have a Foreign Key relationship with other table then you will have to catch the last inserted key from your master table and then pass the same to procedure.
No, you can't insert into multiple tables in one MySQL command. You can however use transactions.
BEGIN;
INSERT INTO slimewarnsplayers VALUES(.....);
last_id = LAST_INSERT_ID()
INSERT INTO SlimeWarnsWarners VALUES(last_id, ....);
INSERT INTO SlimeWarnsWarns VALUES(last_id, ....);
COMMIT;
I would also take a look at http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
and this post MySQL Insert into multiple tables? (Database normalization?)
I want a trigger that creates a table when data is added to the table. The table name must be the name of entry that was inserted.
Example: when patient registers in the hospital a rows is inserted to the paitientDetail table. The trigger must create table that has the patient's name.
Before I answer this question, I have to preface by saying this is a terrible idea. Please do not create a new table for each patient. This is going to create many issues for you down the road.
For example if you have 100 patients, you would then have 100 tables. Then what are you going to do if you need to return data for every single patient...you will have a mess trying to do that.
In SQL Server should you want to perform this type of process, you can do this the following way:
Sample Table:
CREATE TABLE [dbo].[Test](
[id] [int] NOT NULL,
[name] [varchar](50) NOT NULL
) ON [PRIMARY]
Your Trigger would use Dynamic SQL to retrieve the name from the Inserted data.:
CREATE TRIGGER dbo.testTrg
ON dbo.test
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
declare #newtable varchar(50)
set #newtable = (select quotename(name) from inserted)
declare #sql varchar(max)
set #sql = 'create table ' + #newtable + '
(
[id] [int] NOT NULL,
[newColumn1] [varchar](50) NOT NULL
) ON [PRIMARY]'
exec(#sql)
END
This trigger will create the table:
CREATE TABLE [dbo].[test1](
[id] [int] NOT NULL,
[newColumn1] [varchar](50) NOT NULL
) ON [PRIMARY]
A much better design for patients would be the following. Have one Patient table then you would have additional tables as needed to include any additional details for the Patient:
Patient Table:
id int, -- PK
name varchar(50)
Doctor Table
id int, -- PK
name varchar(50)
Patient_Doctor table
p_id int, -- PK -- FK to Patient Table
d_id int -- PK -- FK to Doctor Table
I have a problem with temporary tables in mysql. I created this temporary table:
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_general_detalle_entrada_salida (
numero_registro INT(10),
numero_admision INT(10),
id_referencia INT(10),
facturable CHAR(1),
fecha_acceso DATE,
cod_via INT(10),
anexo2 VARCHAR(40),
anexo3 VARCHAR(40),
cod_bodega CHAR(3),
id_centro_costo INT(10),
cod_medico INT(10),
cantidad FLOAT(15,2),
precio_venta_bruto FLOAT(15,2),
descuento FLOAT(15,2),
precio_venta_neto FLOAT(15,2),
copago FLOAT(15,2),
consumido CHAR(1)
)ENGINE = INNODB;
When I do a simple select count(*) from tmp_general_detalle_entrada_salida on the temporary table, the query returns random values (5, 4, 5, 5, 0, 'table doesn't exist', etc). I'm working with mysql 5.0.51b and PHP 5.2.6.
The changing numbers makes it seem as the data is mutating between your selects. The table does not exist indicates your connection was closed in between selects. Temporary tables go away when your connection closes.
You could try running
set global general_log='ON';
set global general_log_file='/tmp/mysql.log'
This will log every single query from connections to selects /tmp/mysql.log. This will give you more insight to what the servers seeing.