← Back to PostgreSQL Course | Chapter 1: Introduction | Lesson 6 of 7

CREATE TABLE से Table बनाना

CREATE TABLE एक table का name, इसके columns, इनके data types, और कोई rules define करता है।

In this page:

  1. CREATE TABLE
Syntax
sql
CREATE TABLE table_name (
  id SERIAL PRIMARY KEY,
  column1 data_type NOT NULL,
  column2 data_type DEFAULT value
);

CREATE TABLE

हर column को एक name और type के साथ list करें, और PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, और CHECK जैसे constraints add करें। PostgreSQL में एक auto-numbered key आमतौर पर GENERATED ALWAYS AS IDENTITY (या older SERIAL) की तरह लिखी जाती है।

नीचे example portable syntax इस्तेमाल करता है जो SQLite पर भी चलता है।

Note: हर table की एक primary key होनी चाहिए।

उदाहरण: CREATE TABLE

sql
CREATE TABLE employees (
  id INTEGER PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  email VARCHAR(100) UNIQUE,
  salary INTEGER DEFAULT 3000 CHECK (salary >= 0)
);
INSERT INTO employees (id, name, email) VALUES (1, 'Ada', '[email protected]');
INSERT INTO employees (id, name, email, salary) VALUES (2, 'Bob', '[email protected]', 4200);
SELECT * FROM employees;

-- Output:
-- id | name | email | salary
-- 1 | Ada | [email protected] | 3000
-- 2 | Bob | [email protected] | 4200
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक primary key भूल जाना
  2. सब कुछ के लिए TEXT चुनना
  3. required columns पर NOT NULL skip करना
चैप्टर सारांश
  • CREATE TABLE columns और types list करता है
  • Constraints: PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, CHECK मिलते हैं
  • Auto ids के लिए IDENTITY इस्तेमाल करें
  • हर table को एक primary key चाहिए
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.