Search code examples
sqlsqlitenvarchar

NVARCHAR with variable length?


I want to create a table in SQL which has a string attr for Users of a website. How should I declare this attr, since names don't have a max length? Can I just leave the length empty and do it like this?

CREATE TABLE User(
id NUMBER PRIMARY KEY NOT NULL
name NVARCHAR
)

Also, do I need to say NOT NULL for PRIMARY KEYS, or are primary keys already never null?

Thanks in advance :)


Solution

  • You should declare length as MAX and primary key are automatically NOT NULL

    CREATE TABLE User(
    id NUMBER PRIMARY KEY 
    name NVARCHAR(MAX)
    )
    

    Since you have later specified dbms as SQL Lite , the documentation says

    What is the maximum size of a VARCHAR in SQLite?

    SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to let you put 500 characters in it. And it will keep all 500 characters intact - it never truncates.