Common oracle Objects

Source: Internet
Author: User

Common Object tables in oracle are the most basic objects in oracle databases. Common tables, partition tables, index tables, and cluster tables can be used to store user data. In an SQL database, user data can be stored in DDL: 1/* 2 create table 3 create table [schema.] table_name 4 (5 column_name datatype [default expr] 6 [,…] 7); 8 */9 10 createtable dept0111 (12 dno number (2), 13 name varchar2 (10), 14 loc varchar2 (20) default 'hohhot '15 ); 16 17 18 insertinto dept01 (dno, name) 19 values (10, 'technical location'); 20 21 select * from dept01; 22 23/* 24 add columns: 25 alter table table_name add26 (27 column datatype [default expr] 28 [, column datatype…] 29); 30 */31 32 altertable dept01 add33 (34 eee number (4) 35); 36 37/* 38 modify the column definition: 39 alter table table_name modify40 (41 column datatype [default expr] 42 [, column datatype…] 43); 44 */45 altertable dept01 modify46 (47 eee varchar2 (15) default 'Break' 48); 49 50 51/* 52 Delete columns: 53 alter table table_name drop (column); 54 */55 56 altertable dept01 dropcolumn eee; 57 58/* 59 alter column name 60 alter table table_name rename column column_name to new_column_name; 61 */62 63 altertable dept01 rename column eee to aaa; 64 65/* 66 Modify Table Name: 67 rename object_name to new_object_name; 68 */69 70 rename dept 01 to deptt; 71 72 73 select * from deptt; 74 75 76 77/* 78 Add comment 79 comment on table table_name is 'text '; 80 comment on column table_name.column is 'text'; 81 */82 83 comment ontable deptt is 'Alias '; 84 85 comment oncolumn deptt. name is 'name'; 86 87 ----------------------------------------------------------- 88/* Table truncation and table deletion 89 */90 ---- table truncation type: (delete all data. delete can also be used, this is not allowed.) 91 truncatetable table_name; 92 9 3. Run the following command: 94 truncatetable deptt; 95 96 ---- Delete table 97 droptable table_name [cascade constraints [purge; 98 -- cascade constraints: used to specify cascade Delete external key constraints from the table, purge is used to specify to completely delete a table. That is, when deleting a master table, you must specify the cascade constraints clause 99 droptable deptt; 100 101 102 ---- restore deleted table 103 -- when the drop table statement is executed to delete a table, oracle stores the deleted table in the database recycle bin, starting from 10 Gb, use flashback table to quickly restore the deleted table 104 -- Syntax: 105 flashback table table_name to before drop; 106 107 flashback table deptt to befo Re drop; 108 109 110 select * from deptt; 111 ------------------------------------------ 112 113 -- display table information 114 user_tables 115 -- the data dictionary view is used to display information of all tables of the current user 116 -- select deptt from user_tables; 118 119 -- user_objects this data dictionary view is used to display all database objects of the current user 120 121 -- user_tab_comments this data dictionary is used to display comments of all tables of the current user 122 -- user_col_comments used to display all comment 124 125 126 127 -------------------------------------------------------- constraints ------------- ------------------------------- 128 129/* 130 constraints include not null, unique, primary key, foreign key, and check131 1) not null: used to ensure that the column cannot be null132 133 2) unique (unique constraint ): uniquely identifies data in a column. By default, oracle automatically creates a unique index based on the unique constraint, and the index name is exactly the same as the constraint name 134 135 3) primary key (primary key constraint ): it is used to uniquely identify the data of the table row. After the primary key constraint is defined, the column values of the primary key constraint Column cannot be repeated, and cannot be null136 137 4) foreign key (external key constraint ): defines the relationship between master and slave tables. The external key constraint must be defined on the slave table, but the master table must have a primary key constraint or a unique constraint. 138 when an external key constraint is defined, the data in the external key column must exist in the primary table key column or be null. 139 140 5) check: used to force conditions that must be met for table row data. 141 */142 143 144 -------------------------------------------- 145 146/* 147 define constraints 148 149 create table [schema.] table_name150 (151 column_name datatype [default expr] [column_constraint], 152 ...... 153 [table_constraint] [,…] 154); 155 column-level constraints: 156 column [constraint constraint_name] constraint_type157 table-level constraints: 158 column ,......, 159 [constraint constraint_name] constraint_type160 (column,...) 161 */162 163 --!) Define the not null constraint 164 createtable emp01165 (166 eno intnotnull, 167 name varchar2 (10) constraint nn_name notnull, 168 salary number (6, 2) 169); 170 -- correct as follows: 171 insertinto emp01172 values (1, 'A', 1000); 173 174 -- error 175 insertinto emp01176 values (1, null, 1000); 177 178 179 values 180 181 -- 2) define the unique constraint 182 createtable emp02183 (184 eno int, 185 name varchar2 (10), 186 salar Y number (187), 188 constraint u_name unique (name) 189); 190 191 insertinto emp02 1000 values (1, 'Scott ', 192); insertinto emp02193 values (2, 'Scott ', 1000); 194 -- cannot be inserted again, used to uniquely identify 195 -------------------------------------------------- 196 197 -- 3) define primary key constraint 198 createtable dept04199 (200 dno intprimarykey, 201 dname varchar2 (10), 202 loc varchar2 (20) 203); 204 205 insertinto dept04 206 values (1, 'sales ', 'Dallas'); 207 insertinto dept04208 values (1, 'admin', 'Dallas '); 209 --- insertion error, primary key cannot be repeated 210 211 droptable dept04; 212 limit 213 -- 4) define the foreign key constraint 214 215 createtable emp04216 (217 eno int, 218 name varchar2 (10), 219 salary number (6, 2 ), 220 dno intconstraint ck_dno references dept04 (dno) 221); 222 223 insertinto emp04224 values (111, 'Scott ', 225); ins Ertinto emp04226 values (111, 'Scott ', 227, 2); 228 --- insert error, violation of the complete constraints 229 230 ------------------------------------------------------- 231 -- 5) define check constraints 232 233 createtable emp05234 (235 eno int, 236 name varchar2 (10), 237 salary number (238), 239 check (salary between1000and5000 ); 240 241 insertinto emp05242 values (1111, 'Scott ', 800); 243 -- violation of constraints 244 245 ------------------------------------------------ -------- 246 247 -- 6) define composite constraints 248 249 createtable item250 (251 order_id number (3), 252 item_id number (3), 253 product varchar2 (20), 254 primarykey (order_id, item_id) 255); 256 257 ----------------------- maintenance constraint ------------------------------ 258 259 -- 1) Add constraint 260/* 261 If unique, primary key, foreign key, and check constraint are added, the add clause of the alter table statement 262 must be used. If the not null constraint is added, the modify clause of the alter table statement must be used. 263 */264 265 altertabletableadd [constraint conatraint_name266 constraint_type (column ,...); 267 268 altertabletable modify column269 [constraint constraint_namenotnull; 270 271 ---- 1 add not null constraint 272 altertable emp02 modify name notnull; 273 274 ---- 2 Add the unique constraint 275 altertable emp04 addconstraint u_emp04 unique (name); 276 277 ---- 3 Add the primary key constraint 278 altertable dept01 addprimarykey (dno ); 279 280 ---- 4 add foreign key constraint 281 altertable emp01 add dno number (2) references dept01 (dno); 282 limit ---- 283 284 select * from emp01; 285 286 select * from dept01; 287 288 ---- 5 add check constraints 289 altertable emp01 addcheck (salary between800and5000); 290 291 292 -- 2) Modify constraint name 293 -- format: 294 altertabletable rename constraint old_constraint_name 295 to new_constraint_name; 296 297 -- 3) Delete constraints 298 299 -- format: 300 altertabledrop301 constraint constraint_name | primarykey [cascade; 302 303 -- 4) prohibit constraints 304 305 -- format: 306 altertabletable disable constraint constraint_name [cascade; 307 308 -- 5 activation constraint 309 310 -- format: 311 altertabletable enable constraint constraint_name; 312 313 limit 314 315 -- display constraint information 316 317/* 318 1, user_constraints: exists in dd. All constraint information of the current user 319 320 2, user_cons_columns: used to display the table column 321 322 corresponding to the current user constraint */

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.