/

/

SQL Constraints Generator - Enforce Rules | AI2sql

Content

SQL Constraints Generator - Enforce Rules | AI2sql

SQL Constraints Generator - Enforce Rules | AI2sql

SQL Constraints Generator - Enforce Rules | AI2sql

Constraints enforce rules on data in tables. Ensure data integrity and prevent invalid data entry.

PRIMARY KEY

CREATE TABLE users (

id INT PRIMARY KEY,

email VARCHAR(255)

);

FOREIGN KEY

CREATE TABLE orders (

id INT PRIMARY KEY,

customer_id INT,

FOREIGN KEY (customer_id) REFERENCES customers(id)

);

UNIQUE

CREATE TABLE users (

id INT PRIMARY KEY,

email VARCHAR(255) UNIQUE

);

NOT NULL

CREATE TABLE products (

id INT PRIMARY KEY,

name VARCHAR(255) NOT NULL,

price DECIMAL(10,2) NOT NULL

);

CHECK

CREATE TABLE products (

id INT PRIMARY KEY,

price DECIMAL(10,2) CHECK (price > 0),

stock INT CHECK (stock >= 0)

);

DEFAULT

CREATE TABLE orders (

id INT PRIMARY KEY,

status VARCHAR(50) DEFAULT 'pending',

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Add Constraint to Existing Table

ALTER TABLE orders ADD CONSTRAINT fk_customer

FOREIGN KEY (customer_id) REFERENCES customers(id);

Generate Constrained Tables

AI2sql creates tables with proper constraints.

Share this

More Articles

More Articles

More Articles