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.
Related
I am just wondering to know can I have a string of TIMESTAMPs in a MySQL table schema like following:
CREATE TABLE IF NOT EXISTS auth (
id BINARY(16) PRIMARY KEY,
email VARCHAR(64) UNIQUE NOT NULL,
password VARCHAR(64) NOT NULL,
admin INT NOT NULL DEFAULT 0,
created_at [TIMESTAMP]
);
Unfortunately the above code gives me a syntax error!
My idea is to have a list of TIMESTAMs to store every future updates inside of it, something like ['2023-01-01' , '2023-02-02' , ....]
If this is not possible in my suggested way, how can I store any changes of one column of a table like created_at column? Should I do it in the web-server?
I am new to MySQL, so I'll explain by example.
I have 2 tables:
Admins(
id int auto_increment not null,
primary key(id)
);
Users(
admin_id int not null,
id varchar(255) not null,
password varchar(255) not null
);
I basically create an entry for a admin, then I want the admin to be able to add users that are tied to his ID, so then I can also read all users tied to that certain admin (the id and password parameters, to be exact)
I cannot figure out the syntax for how to do this, could anyone provide some help? Maybe there is a way to just write all that data straight in the admin table somehow so I don't have to use 2 tables?
I use PHP to do everything, by the way
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;
I would like to save user data in my database.
There is common data about the user account (nickname, password, etc.) but also data like firstname, name, age, location, ...
How can I manage my data base? Should I create different tables? One containing common user data and another containing all the other data?
This is a design choice, and it basically depends on how much information you usually need, and how many extra fields you have.
Option 1: Keep them in the same table, if its not too much or you usually need all the data.
Option 2: Create a User Profile table, that contains the user data that its related to the person and not the account.
create one single table.
CREATE TABLE `admin`
(
`User_Name` varchar(60) NOT NULL,
`Password` varchar(60) NOT NULL,
'firstname' varchar(60) not null,
'Age' int(11) Not null,
'Location' varchar(50) NOT NULL,
PRIMARY KEY (`User_Name`)
)
ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create 2 tables:
1. userProfile
create table userProfile(
UserID int primary key auto_increment(1,1),
firstname varchar(50),
age int(11),
location varchar(50));
2.userAccounts:
create table userAccounts(
ID int primary key auto_increment(1,1),
UserID int(11),
UserName varchar(50),
Password varchar(50));
there is a relation between Table1(UserID) and Table2(UserID).
I wrote schema:
CREATE TABLE user
(
user_id INT UNSIGNED AUTO_INCREMENT NOT NULL,
fname VARCHAR(20) NOT NULL,
lname VARCHAR(20) NOT NULL,
email VARCHAR(30) NOT NULL,
password VARCHAR(60) NOT NULL,
gender ENUM('M', 'F'),
CONSTRAINT pk_user_id PRIMARY KEY (user_id)
)
But when inserting only two values, lets say the users first name and last name, the email and password are left blank, meaning the not null is not working as expected. I do get 2 warnings when I show them, but the behavior is not as expected. Do I have to specifically make constraints to fail an insert to make sure this never happens?
Unfortunately, mysql does not support CHECK constraints.
They are "valid syntax" in mysql only for compatibility reasons, so you can code them, and the create table SQL will execute OK, but they are ignored thereafter.