DB2 to build tables, add fields, delete fields, modify fields, and more

Source: Internet
Author: User
Tags db2

Reproduced: http://blog.sina.com.cn/s/blog_67aaf4440100v01p.html, slightly modified.

--Create a database
Create Database ETP;


--Connect to database
Connect to ETP;


--Disconnect
Disconnect ETP;


--See what tables are in the current database
List tables;

--Delete Table
drop table studentinfo;

--Adding a constraint when creating a table 1
CREATE TABLE Studentinfo (
Stuno int NOT NULL,
Stuname varchar (8) NOT NULL,
Stuage int,
Stutel Char (8),
Constraint Pk_stuno primary KEY (Stuno),
Constraint un_stuname Unique (stuname),
Constraint ch_stuage Check (stuage>=0 and Stuage <150)
);

--Create a table with the addition of Constraint mode 2 (second, simple and convenient)
CREATE TABLE Studentinfo (
Stuno int NOT null primary key,
Stuname varchar (8) NOT null unique,
stuage int Check (stuage >=0 andstuage <150),
Stutel Char (8)
);


--View table structure
Describe table studentinfo;

Search and change of fields and additions
--Add Table field
ALTER TABLE studentinfo add Stubirth date;

ALTER TABLE studentinfo add ABC int;


--delete Field
ALTER TABLE studentinfo drop column ABC;


--Modify the field type
ALTER TABLE studentinfo ALTER COLUMN Stutel set data typechar (11); (color marked, differs from SQL Server)


--Add a non-null constraint
ALTER TABLE studentinfo ALTER COLUMN Stutel set not null; (color is marked, differs from SQL Server)

DB2 to build tables, add fields, delete fields, modify fields, and more

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.