2

I have a table called UserInfo like this:

Userid     Job     Age
John       A       35
Sarah      B       27

and I want to create another table like this called UserScores (score is null):

Userid     Score
John       
Sarah      

Where the userid column is the same exact as the other table. How would I do this with SQL? This is what I tried:

CREATE TABLE UserScores AS
    SELECT userid, score
    FROM user

Here's my error message: enter image description here

I need the userid to be the primary key in my UserScores table as well. How would I do this? Thanks in advance.

i am bad at coding
  • 555
  • 1
  • 3
  • 14

1 Answers1

3

You add NULL as value to scores as below and add the promary kley after creatuing the table userscores

CREATE TABLE user (userid INT)
INSERT INTO user VALUES (1)
CREATE TABLE UserScores AS
    SELECT userid, NULL AS score
    FROM user
ALTER TABLE UserScores
ADD PRIMARY KEY (userid); 

db<>fiddle here

EDIt

you should also define the type of score in the create

CREATE TABLE UserScores (userid  int, scrore int) AS
    SELECT userid, NULL AS score
    FROM user
nbk
  • 45,398
  • 8
  • 30
  • 47
  • 1
    note that you will likely want to change score's data type after this. I'd just make the table the way you want, keys and all, and then `insert into UserScores (userid) select userid from user;` – ysth Mar 26 '21 at 21:23