Android - Sqlite Query with unique and foreign key constraint

I have two tables named x and y.

x table has two columns say _id and name.

_id would be primary key, name would be a unique constraint.

I'm not familiar with sqlite queries. Should the query be written like this?

 CREATE TABLE IF NOT EXISTS x (
    _id integer primary key autoincrement, 
    name text, 
    unique(name));

y table has three columns _id, xid, and address.

_id would be the primary key, xid would be a foreign key, address would be a unique key constraint.

CREATE TABLE IF NOT EXISTS y (
    _id integer primary key autoincrement, 
    address text, 
    xid references x(_id), 
    unique(address));

If I'm missing or not defining anything correctly, please, correct me.

Thanks in advance.

Answers


Your commands are correct.

You could make the constraints a little bit clearer by making them column constraints instead of table constraints:

CREATE TABLE IF NOT EXISTS x (
    _id integer primary key autoincrement, 
    name text unique);
CREATE TABLE IF NOT EXISTS y (
    _id integer primary key autoincrement, 
    address text unique,
    xid references x(_id));

Need Your Help

C++11 seed_seq initialization

c++ random c++11

The header file <random> allows for the seed sequence's internal sequence to be initialized. An object of class seed_seq can be constructed in multiple ways. I am curious about one of the ways,

How to get property from dynamic JObject programmatically

c# .net json json.net deserialization

I'm parsing a JSON string using the NewtonSoft JObject.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.