Home Database Mysql Tutorial ALTER TABLE添加一列并且添加 检查 约束

ALTER TABLE添加一列并且添加 检查 约束

Jun 07, 2016 pm 05:45 PM
created nbsp sql

oracle的约束

* 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,
必须在表级定义约束
* 在定义约束时可以通过constraint关键字为约束命名,如果没有指定,oracle将自动为约束建立默认的名称

定义primary key约束(单个字段)
create table employees (empno number(5) primary key,...)

sql>
sql> create table emp (empno number(4) not null,
  2                    ename varchar2(10),
  3                    job varchar2(9),
  4                    mgr number(4),
  5                    hiredate date,
  6                    sal number(7, 2),
  7                    comm number(7, 2),
  8                    deptno number(2));

table created.

sql>
sql> insert into emp values (7369, 'smith', 'clerk',    7902, to_date('17-dec-1980', 'dd-mon-yyyy'), 800, null, 20);

1 row created.

sql> insert into emp values (7499, 'allen', 'salesman', 7698, to_date('20-feb-1981', 'dd-mon-yyyy'), 1600, 300, 30);

1 row created.

sql> insert into emp values (7521, 'ward',  'salesman', 7698, to_date('22-feb-1981', 'dd-mon-yyyy'), 1250, 500, 30);

1 row created.

sql> insert into emp values (7566, 'jones', 'manager',  7839, to_date('2-apr-1981',  'dd-mon-yyyy'), 2975, null, 20);

1 row created.

sql> insert into emp values (7654, 'martin', 'salesman', 7698,to_date('28-sep-1981', 'dd-mon-yyyy'), 1250, 1400, 30);

1 row created.

sql> insert into emp values (7698, 'blake', 'manager', 7839,to_date('1-may-1981', 'dd-mon-yyyy'), 2850, null, 30);

1 row created.

sql> insert into emp values (7782, 'clark', 'manager', 7839,to_date('9-jun-1981', 'dd-mon-yyyy'), 2450, null, 10);

1 row created.

sql> insert into emp values (7788, 'scott', 'analyst', 7566,to_date('09-dec-1982', 'dd-mon-yyyy'), 3000, null, 20);

1 row created.

sql> insert into emp values (7839, 'king', 'president', null,to_date('17-nov-1981', 'dd-mon-yyyy'), 5000, null, 10);

1 row created.

sql> insert into emp values (7844, 'turner', 'salesman', 7698,to_date('8-sep-1981', 'dd-mon-yyyy'), 1500, 0, 30);

1 row created.

sql> insert into emp values (7876, 'adams', 'clerk', 7788,to_date('12-jan-1983', 'dd-mon-yyyy'), 1100, null, 20);

1 row created.

sql> insert into emp values (7900, 'james', 'clerk', 7698,to_date('3-dec-1981', 'dd-mon-yyyy'), 950, null, 30);

1 row created.

sql> insert into emp values (7902, 'ford', 'analyst', 7566,to_date('3-dec-1981', 'dd-mon-yyyy'), 3000, null, 20);

1 row created.

sql> insert into emp values (7934, 'miller', 'clerk', 7782,to_date('23-jan-1982', 'dd-mon-yyyy'), 1300, null, 10);

1 row created.

sql>
sql> * from emp;

     empno ename      job              mgr hiredate         sal       comm     deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 smith      clerk           7902 17-dec-80        800                    20
      7499 allen      salesman        7698 20-feb-81       1600        300         30
      7521 ward       salesman        7698 22-feb-81       1250        500         30
      7566 jones      manager         7839 02-apr-81       2975                    20
      7654 martin     salesman        7698 28-sep-81       1250       1400         30
      7698 blake      manager         7839 01-may-81       2850                    30
      7782 clark      manager         7839 09-jun-81       2450                    10
      7788 scott      analyst         7566 09-dec-82       3000                    20
      7839 king       president            17-nov-81       5000                    10
      7844 turner     salesman        7698 08-sep-81       1500          0         30
      7876 adams      clerk           7788 12-jan-83       1100                    20

     empno ename      job              mgr hiredate         sal       comm     deptno
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7900 james      clerk           7698 03-dec-81        950                    30
      7902 ford       analyst         7566 03-dec-81       3000                    20
      7934 miller     clerk           7782 23-jan-82       1300                    10

14 rows selected.

sql>
sql> alter table emp
  2      add (
  3        gender   varchar(10));

table altered.

sql>
sql> alter table emp
  2      add constraint ck_gender
  3      check (gender in ('male', 'female'));

table altered.

sql>
sql> update emp set gender = 'male' where mod(empno,2) = 0;

10 rows updated.

sql>
sql> select * from emp;

     empno ename      job              mgr hiredate         sal       comm     deptno gender
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
      7369 smith      clerk           7902 17-dec-80        800                    20
      7499 allen      salesman        7698 20-feb-81       1600        300         30
      7521 ward       salesman        7698 22-feb-81       1250        500         30
      7566 jones      manager         7839 02-apr-81       2975                    20 male
      7654 martin     salesman        7698 28-sep-81       1250       1400         30 male
      7698 blake      manager         7839 01-may-81       2850                    30 male
      7782 clark      manager         7839 09-jun-81       2450                    10 male
      7788 scott      analyst         7566 09-dec-82       3000                    20 male
      7839 king       president            17-nov-81       5000                    10
      7844 turner     salesman        7698 08-sep-81       1500          0         30 male
      7876 adams      clerk           7788 12-jan-83       1100                    20 male

     empno ename      job              mgr hiredate         sal       comm     deptno gender
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
      7900 james      clerk           7698 03-dec-81        950                    30 male
      7902 ford       analyst         7566 03-dec-81       3000                    20 male
      7934 miller     clerk           7782 23-jan-82       1300                    10 male

14 rows selected.

sql>
sql> update emp set gender = 'female' where mod(empno,2) = 1;

4 rows updated.

sql>
sql> select ename, job, gender from emp order by gender, ename;

ename      job       gender
---------- --------- ----------
allen      salesman  female
king       president female
smith      clerk     female
ward       salesman  female
adams      clerk     male
blake      manager   male
clark      manager   male
ford       analyst   male
james      clerk     male
jones      manager   male
martin     salesman  male

ename      job       gender
---------- --------- ----------
miller     clerk     male
scott      analyst   male
turner     salesman  male

14 rows selected.

sql>
sql> insert into emp (empno, ename, gender) values (8001, 'pat', 'unknown');
insert into emp (empno, ename, gender) values (8001, 'pat', 'unknown')
*
error at line 1:
ora-02290: check constraint (sys.ck_gender) violated


sql>
sql> drop table emp;

table dropped.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

See all articles