Could not locate entry in sysdatabases for database - mysql

I need this code to run the instant the professor hits the F5 button.
I created a database and I have a problem with the USE statement
SQL just can't switch to the database that USE statement is calling. it's saying this:
could not locate entry in sysdatabases for database
I tried with square brackets ([]) and it's not helping.
The project is about Car Service and its a school project.
I just need that USE statement to run or some other solution so the code can run without error and in one click on F5.
USE master;
IF DB_ID('ServisAutomobila') IS NOT NULL
DROP DATABASE ServisAutomobila;
GO
CREATE DATABASE ServisAutomobila;
USE ServisAutomobila
go
CREATE TABLE Automobil
(
Automobil_ID int identity (1,1) not null,
Model nvarchar (50) not null,
GodinaProizvodnje nvarchar(50) not null,
RegistarskiBroj nvarchar (50) unique not null,
BrojMotora nvarchar (50) unique not null,
BrojSasije nvarchar (50) unique not null,
Kvar nvarchar (250) not null,
TipAutomobila_ID int not null,
Zemlja_ID int not null,
Vlasnik_ID int not null
)
CREATE TABLE Zemlja
(
Zemlja_ID int identity (1,1) not null,
Zemlja nvarchar (50) not null
)

The GO keyword is a batch terminator. So in your example:
CREATE DATABASE ServisAutomobila;
USE ServisAutomobila
go
The batch is executed as one and results in that particular error because the database engine tries do use database ServisAutomobila which does not exists yet. This means that you must add that extra GO suggested in the previous comments before you can use the newly created database:
CREATE DATABASE ServisAutomobila;
GO;
USE ServisAutomobila;
GO;

Related

Cannot create table with flyway (MYSQL)

I try do create a simple mysql database with Flyway migration, but it doesn't going well.
I have created my .sql it named as V1_0__init.sql and I've written some sql commands there:
CREATE TABLE User (
id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
created DATE NOT NULL,
username varchar(15) NOT NULL,
password varchar(15) NOT NULL,
email varchar(40) NOT NULL,
status boolean NOT NULL,
unique(username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think it should be fine, but I have two errors, one at the first line (CREATE TABLE) and another at the fourth line (created DATE NOT NULL).
The error message says:
Invalid or Incomplete statement
Expected one of the following: ALGORITHM DEFINER SQL VIEW
(At the 'created DATE NOT NULL' line have a similar message, the only difference is this: Algoritm definer OR sql view)
I've tried to fix these but it didn't go well.
Anyone could help me how to fix this?
Maybe did I miss something? Should I do something in application.properties?

Issue Converting varchar to nvarchar mysql

Sorry if this is an easy question, I am coming to MySQL from SQL Server.
When I execute my create statement it contains nvarchar but commits to the database as varchar. Even in my alter statement afterwards the column does not change at all. Does the collation or DB engine make a difference?
During execution I am not encountering any issues in results, other than the fact the column changes datatype. I attached a screencast of my activity http://screencast.com/t/wc94oei2
I have not been able to find anyone with similar issues through my Google searches
Did you mean, this..
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name nvarchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)
----turns to---
CREATE TABLE stars (
idstars int(11) NOT NULL AUTO_INCREMENT,
Name varchar(200) DEFAULT NULL,
PRIMARY KEY (idstars),
UNIQUE KEY Name_UNIQUE (Name)
)

MYSQL allowing me to insert nulls in PK and FK

I have been testing a database i am doing right now and i am noticing that it is letting me insert null values into fields that are part of a primary key, despite stating in the script that the value of the field should be NOT NULL. I am using MAC's MySQL Workbench, and I have been googling around and can't figure out why this is happening. (Maybe I am too brain-fried right now... I am even starting to doubt myself)
Part of the script of the database creation (these are the tables I have tested..):
DROP DATABASE IF EXISTS solytierra ;
CREATE DATABASE IF NOT EXISTS solytierra DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE solytierra ;
DROP TABLE IF EXISTS solytierra.Cliente ;
CREATE TABLE IF NOT EXISTS solytierra.Cliente (
CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(100) NULL,
EmailGeneral VARCHAR(45) NULL,
Web VARCHAR(45) NULL,
Notas VARCHAR(150) NULL,
insertado Timestamp,
CONSTRAINT pk_Cliente PRIMARY KEY (CIF)
) ENGINE=InnoDB;
DROP TABLE IF EXISTS solytierra.PersonaContacto ;
CREATE TABLE IF NOT EXISTS solytierra.PersonaContacto (
Cliente_CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(50) NOT NULL,
Apellidos VARCHAR(100) NOT NULL,
Notas VARCHAR(150) NULL,
CONSTRAINT pk_PersonaContacto PRIMARY KEY (Cliente_CIF , Nombre , Apellidos),
CONSTRAINT fk_PersonaContacto_Cliente FOREIGN KEY (Cliente_CIF)
REFERENCES solytierra.Cliente (CIF)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
...
It will let me create Clients without CIF, "PersonaContacto" without Cliente_CIF or without "Nombre"....
I have also tested other databases that i already had that used to work and it is happening the same in an all them.
Got it!!
I don't know what sql mode i was running on by default, but with this:
SET sql_mode = TRADITIONAL;
It is now running perfectly! I didn't know that there were different sql modes! Thanks a lot to everyone for your time and efforts! It really helped me to see that the problem was in my workbench, not the code and look for the answer accordingly! I hope this thread will be useful for future beginners like me!
If the value being stored in the column CIF is actually a NULL, then the expression LENGTH(CIF) should also return NULL. (If it's a zero length string, then LENGTH(CIF) will return 0.
To verify:
SELECT c.CIF, LENGTH(c.CIF) FROM solytierra.Cliente c ;
SELECT c.CIF FROM solytierra.Cliente c WHERE c.CIF IS NULL;
If you are running an INSERT statement, I can't explain the behavior you are observing, either MySQL allowing a NULL value to be stored or MySQL providing an implicit default value.)
If it's a zero length string being stored, that's the behavior we would expect if the columns were not explicitly declared to be NOT NULL but were later declared to part of the primary key. It's also the behavior we'd expect if the column were defined NOT NULL DEFAULT ''.
When the NOT NULL is omitted from the column declaration and the column is later declared to be part of the PRIMARY KEY, MySQL will use an an implicit default value based on the datatype of the column (zero length string for VARCHAR, zero for an integer, etc.)
But I'm not able to reproduce the problem you report, with the table definitions you've posted.
I recommend you check the table definition by getting the output from:
SHOW CREATE TABLE solytierra.Cliente;

mysql and sql developer

I found how to make sql developer connect to a mysql server and then I made a database. The problem I now have is that I cannot add my tables to it. Is it a syntax problem or am I doing something wrong? I have used (use database;) but now I see and error on some lines. Like in line 352, but it looks fine....
CREATE TABLE acctmanager
(amid VARCHAR2(4) PRIMARY KEY,
amfirst VARCHAR2(12) NOT NULL,
amlast VARCHAR2(12) NOT NULL,
amedate DATE DEFAULT SYSDATE,
region CHAR(2) NOT NULL);
there is no varchar2 in MySQL
replace varchar2 with varchar

Generating Unique Usernames in SQL Server

I am curious how people are handling the following situation. Let's say we have a Users Table that looks something like this:
CREATE TABLE [dbo].[Users](
[UserId] [uniqueidentifier] NOT NULL,
[ProfileId] [uniqueidentifier] NULL,
[UserTypeId] [uniqueidentifier] NOT NULL,
[UserName] [varchar](150) NOT NULL,
[Password] [varchar](150) NOT NULL,
[Salt] [nchar](10) NOT NULL,
[ActivationCode] [char](8) NULL,
[InvalidLoginAttempts] [int] NOT NULL,
[IsLockedOut] [int] NOT NULL,
[LastLoginDate] [datetime2](7) NOT NULL,
[Active] [int] NOT NULL,
[DateCreated] [datetime2](7) NOT NULL,
[LastUpdated] [datetime2](7) NOT NULL,
So here is my actual question. Previously when using int for the PK I could insert a user and autocreate a username based on the identity insert if the user did not supply a username. Example of why this would happen. "OpenId Registration for instance" So how would one generate a unique "count" so to say using guids. I certainly don't want to display "welcome userXXX-XXX-XXX and so on.
My thoughts are maybe build a seperate table for this with and int IDENTITY and store the guid in there??
Why not just add another column with identity to the table and use that.
Or pick a random number, check if userName already exists, if so generate a new.
Following Magnus's proposal, you could build a default user name by concatenating an additional int/autonumber field in the table + the server's name (or the database name), or part of it as!
Suppose that you have SERV1, SERV2, SERV3 running. You could then either build the default user name as userSERVX_Y, or userSERVX0000Y, etc. Your username building scheme will depend on your server naming strategy. If your server names are not 'sexy'enough, you could add somwehere a table with aliases for your servers.