SQL command that creates a database - mysql

Write an SQL command that creates a database that has the following structure:
Table 2: Name: StaffDetails
Screenshot of Structure: Table Structure
CREATE DATABASE StaffDetails_database;
USE StaffDetails _database;
CREATE TABLE StaffDetails (
StaffRef INT NOT NULL,
LName NCHAR(25) NOT NULL,
FName NCHAR(25) NOT NULL,
Phone NCHAR(25) NULL,
StartDate DATE NOT NULL,
PRIMARY KEY (StaffRef),
);
Would this be correct way to create this?

Related

error when forward engineering MySQL model

I have created a model in MySQL workbench, when I want to forward engineer it to create the "create" and "insert" script I get the following error:
ERROR:
Executing SQL script in server
ERROR: Error 1366: Incorrect integer value: 'G1' for column 'gebruiker_id' at row 1
SQL Code:
INSERT INTO `databaseher`.`gebruiker` (`gebruiker_id`, `voornaam`, `achternaam`, `E-mail`) VALUES ('G1', 'Ronny', 'Giezen', 'r.giezen#gmail.com')
I don't understand whats wrong with it, because the datatype of the column where the value "G1" inserts into is "VARCHAR(4)". It should be possible to insert both a letter and a number.... At least that's what I thought...
This is the create table:
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
If someone could help, that'll be awesome.
Thank you in advance!
Just a guess here because we don't have the full picture.
Can you run this:
Drop table 'databaseher'.'gebruiker'
And after recreate the table
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
rerun the insert.
I am guessing that the table was initially created with the column gebruiker as integer

Error in importing sql file [#1064]

I am trying to create a database for my login/signup forms. I creted this database using SQL. However, when importing it in phpmyadmin it says "Import has been successfully finished, 5 queries executed."
then the error:
Error
SQL query:
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null default "",
password varchar(50) not null default "",
age integer(50) not null,
primary key ('email', 'password')
)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''email', 'password')
)' at line 7
this is my sql code:
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key ('email', 'password')
);
Remove Single quotes in email, password.
While defining primary key you don't need to add quotes.
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key (email, password)
);

SQL Error #1007

I have this #1007 error with my SQL code. When I try to import into my database it gives me this #1007 error. The data base is called company. I'm new to SQL and it would be good if someone could help me out. Thanks
CREATE DATABASE company;
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
It looks like the database "company" may already exist. If you look at the link provided by #Marc B, the error "#1007" corresponds to:
"Error: 1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS)
Message: Can't create database '%s'; database exists
An attempt to create a database failed because the database already exists.
Drop the database first if you really want to replace an existing database, or add an IF NOT EXISTS clause to the CREATE DATABASE statement if to retain an existing database without having the statement produce an error."
Check your schema to make sure that you don't already have a database called "company" created.
Perhaps you are only trying to create the table "login", whereas you would simply need the code:
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
making sure you create this table in the already existing "company" database.
You are trying to create a database that has already been created.
Staring at your two commands, you could do CREATE TABLE IF NOT EXISTS
you also need to set default database before creating the table
CREATE DATABASE IF NOT EXISTS company;
USE company
CREATE TABLE login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);
or you could put the DB name before the table name
CREATE DATABASE IF NOT EXISTS company;
CREATE TABLE company.login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);

MySQL CREATE TABLE command

I am new to MySQL commands. please, I tried creating a table using the MAMP MySQL editor and I got an error #1046 No database selected..Below is the simple code:
[code]
CREATE TABLE EMPLOYEE_TABLE AS:
(SSN NUMBER(9) NOT NULL,
LAST_NAME VARCHAR2(20) NOT NULL,
FIRST_NAME VARCHAR2(20) NOT NULL,
MIDDLE_NAME VARCHAR2(20) NOT NULL,
ST ADDRESS VARCHARS2(20) NOT NULL,
CITY CHAR(20) NOT NULL,
STATE CHAR(2) NOT NULL,
ZIP NUMBER(4) NOT NULL,
DATE_HIRED DATE)
STORAGE(INITIAL 3K,
NEXT 1K)
[/code]
You have to select the database where you want to insert the table, there are to ways to do it :
Selecting the default database : USE databasename;
Specifying database name before the table in the CREATE TABLE statement : CREATE TABLE DATABASENAME.EMPLOYEE_TABLE ...
Where databasename is the name of your database.

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.