-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorials-sql.html
681 lines (515 loc) · 25.8 KB
/
tutorials-sql.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PrograMix</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<style>
body {
padding-top: 70px;
/* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
font-size: 15px;
font-style: verdana;
}
li{
color: #33AAFF;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><img src="images/logo-trans-134px.png" style="margin-top: -10px"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<!-- languages nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Languages<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="programming-language-java.html">Java</a></li>
<li><a class="dropdown-item" href="programming-language-python.html">Python</a></li>
<li><a class="dropdown-item" href="programming-language-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="programming-language-php.html">PHP</a></li>
<li><a class="dropdown-item" href="programming-language-c++.html">C++</a></li>
<li><a class="dropdown-item" href="programming-language-c_sharp.html">C#</a></li>
<li><a class="dropdown-item" href="programming-language-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="programming-language-SQL.html">SQL</a></li>
<li><a class="dropdown-item" href="programming-language-Perl.html">Perl</a></li>
<li><a class="dropdown-item" href="programming-language-JavaScript.html">JavaScript</a></li>
</ul>
</div>
</li><!--end of languages dropdown-->
<!--tutorials in nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Tutorials<span class="caret"></span></a>
<!-- Tutorials dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="tutorials-java.html">Java</a></li>
<li><a class="dropdown-item" href="tutorials-python.html">Python</a></li>
<li><a class="dropdown-item" href="tutorials-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="tutorials-php.html">PHP</a></li>
<li><a class="dropdown-item" href="tutorials-c++.html">C++</a></li>
<li><a class="dropdown-item" href="tutorials-c_shar.html">C#</a></li>
<li><a class="dropdown-item" href="tutorials-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="tutorials-sql.html">SQL</a></li>
<li><a class="dropdown-item" href="tutorials-perl.html">Perl</a></li>
<li><a class="dropdown-item" href="tutorials-js.html">JavaScript</a></li>
</ul>
</div>
</li><!-- end of tutorials -->
<!--programming software in navbar with dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Softwares<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="prosoft-nb.html">NetBeans</a></li>
<li><a class="dropdown-item" href="prosoft-jc.html">JCreator</a></li>
<li><a class="dropdown-item" href="prosoft-st.html">Sublime Text</a></li>
<li><a class="dropdown-item" href="prosoft-eclipse.html">Eclipse</a></li>
<li><a class="dropdown-item" href="prosoft-xcode.html">Xcode</a></li>
</ul>
</div>
</li><!-- end of programming softwares -->
<!-- search bar icon w/ dropdown searchbox-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false"><img src="images/search-icon.png"></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<form method="get" action="/search" id="search">
<input name="q" type="text" size="40" placeholder="Search..." />
</form>
</div>
</li><!-- end of search bar-->
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content w/ container-->
<div class="container">
<div class="row">
<div class="col-lg-12 text-left">
<div class="jumbotron jumbotron-fluid">
<div class="container" style="text-align: center">
<h1 class="display-3">SQL</h1>
<p class="lead"> </p>
</div>
</div>
<p class="lead">
<h2>SQL SELECT Statement</h2>
<p style="text-indent: 40px">The most commonly used SQL command is SELECT statement. SQL SELECT statement is used to query or retrieve data from a table in the database. A query may retrieve information from specified columns or from all of the columns in the table. To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name. The whole query is called SQL SELECT Statement.</p>
<h2>Syntax of SQL SELECT Statement:</h2>
<div class="alert alert-info" role="alert">
SELECT column_list FROM table-name <br>
[WHERE Clause]<br>
[GROUP BY clause]<br>
[HAVING clause]<br>
[ORDER BY clause];<br>
</div>
<ul>
<li style="color: black">table-name is the name of the table from which the information is retrieved.</li>
<li style="color: black">column_list includes one or more columns from which data is retrieved.</li>
<li style="color: black">The code within the brackets is optional.</li>
</ul>
<br>
<b>database table student_details</b>
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>first_name</th>
<th>last_name</th>
<th>age</th>
<th>subject</th>
<th>games</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">100</th>
<td>Rahul</td>
<td>Sharma</td>
<td>10</td>
<td>Science</td>
<td>Cricket</td>
</tr>
<tr>
<th scope="row">101</th>
<td>Anjali</td>
<td>Bhagwat</td>
<td>12</td>
<td>Maths</td>
<td>Football</td>
</tr>
<tr>
<th scope="row">102</th>
<td>Stephen</td>
<td>Fleming</td>
<td>09</td>
<td>Science</td>
<td>Cricket</td>
</tr>
<tr>
<th scope="row">103</th>
<td>Shekar</td>
<td>Gowda</td>
<td>18</td>
<td>Maths</td>
<td>Badminton</td>
</tr>
<tr>
<th scope="row">104</th>
<td>Priya</td>
<td>Chandra</td>
<td>15</td>
<td>Economics</td>
<td>Chess</td>
</tr>
</tbody>
</table>
<b>NOTE:</b> These database tables are used here for better explanation of SQL commands. In reality, the tables can have different columns and different data.<br>
<br>
<p style="text-indent: 40px">For example, consider the table student_details. To select the first name of all the students the query would be like:</p><br>
<div class="alert alert-info" role="alert">
SELECT first_name FROM student_details;
</div>
<b>SELECT Statements: Syntax</b><br>
<b>NOTE:</b> SQL commands are not case sensitive. The above SELECT statement can also be written as<br>
<br>
"select first_name from students_details;"<br>
<p style="text-indent: 40px">You can also retrieve data from more than one column. For example, to select first name and last name of all the students.</p><br>
<div class="alert alert-info" role="alert">
SELECT first_name, last_name FROM student_details;
</div>
<p style="text-indent: 40px">You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement. We will discuss these commands in coming chapters.</p><br>
<b>NOTE:</b> In a SQL SELECT statement only SELECT and FROM statements are mandatory. Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.<br>
<h2>How to use expressions in SQL SELECT Statement?</h2>
<p style="text-indent: 40px">Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and ORDER BY Clauses of the SQL SELECT Statement.</p><br>
<p style="text-indent: 40px">Here we will explain how to use expressions in the SQL SELECT Statement. About using expressions in WHERE and ORDER BY clause, they will be explained in their respective sections.</p><br>
<p style="text-indent: 40px">The operators are evaluated in a specific order of precedence, when more than one arithmetic operator is used in an expression. The order of evaluation is: parentheses, division, multiplication, addition, and subtraction. The evaluation is performed from the left to the right of the expression.</p>
<h2>SELECT Statement Example?</h2>
<p style="text-indent: 40px">If we want to display the first and last name of an employee combined together, the SQL Select Statement would be like</p><br>
<div class="alert alert-info" role="alert">
SELECT first_name + ' ' + last_name FROM employee;
</div>
<b>Output:</b><br>
<div class="alert alert-info" role="alert">
first_name + ' ' + last_name<br>
---------------------------------<br>
Rahul Sharma<br>
Anjali Bhagwat<br>
Stephen Fleming<br>
Shekar Gowda<br>
Priya Chandra<br>
</div>
You can also provide aliases as below.<br>
<br>
<div class="alert alert-info" role="alert">
SELECT first_name + ' ' + last_name AS emp_name FROM employee;
</div>
<b>Output:</b><br>
<div class="alert alert-info" role="alert">
emp_name<br>
-------------<br>
Rahul Sharma<br>
Anjali Bhagwat<br>
Stephen Fleming<br>
Shekar Gowda<br>
Priya Chandra
</div>
<h2>SQL INSERT Statement</h2>
<p style="text-indent: 40px">The INSERT Statement is used to add new rows of data to a table./
<p style="text-indent: 40px">We can insert data to a table in two ways,/<br>
<br>
<b>1) Inserting the data directly to a table.</b>
<h2>Syntax for SQL INSERT is:</h2>
<div class="alert alert-info" role="alert">
INSERT INTO TABLE_NAME <br>
[ (col1, col2, col3,...colN)] <br>
VALUES (value1, value2, value3,...valueN); <br>
</div>
<ul>
<li style="color: black">col1, col2,...colN -- the names of the columns in the table into which you want to insert data.</li>
</ul>
<p style="text-indent: 40px">While inserting a row, if you are adding value for all the columns of the table you need not specify the column(s) name in the sql query. But you need to make sure the order of the values is in the same order as the columns in the table. The sql insert query will be as follows/<br>
<div class="alert alert-info" role="alert">
INSERT INTO TABLE_NAME <br>
VALUES (value1, value2, value3,...valueN); <br>
</div>
<p style="text-indent: 40px"><b>For Example:</b> If you want to insert a row to the employee table, the query would be like,<br>
<div class="alert alert-info" role="alert">
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);
</div>
<b>NOTE:</b> When adding a row, only the characters or date values should be enclosed with single quotes.<br>
<br>
<p style="text-indent: 40px">If you are inserting data to all the columns, the column names can be omitted. The above insert statement can also be written as,/<br>
<div class="alert alert-info" role="alert">
INSERT INTO employee
VALUES (105, 'Srinath', 'Aeronautics', 27, 33000);
</div>
<p style="text-indent: 40px"><b>Inserting data to a table through a select statement./</b>
<h2>Syntax for SQL INSERT is:</h2>
<div class="alert alert-info" role="alert">
INSERT INTO table_name
[(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM table_name [WHERE condition];
</div>
<p style="text-indent: 40px"><b>For Example:</b> To insert a row into the employee table from a temporary table, the sql insert query would be like,/<br>
<div class="alert alert-info" role="alert">
INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id, emp_name, dept, age, salary, location
FROM temp_employee;
</div>
<p style="text-indent: 40px">If you are inserting data to all the columns, the above insert statement can also be written as,/<br>
<div class="alert alert-info" role="alert">
INSERT INTO employee
SELECT * FROM temp_employee;
</div>
<p style="text-indent: 40px"><b>NOTE:</b> We have assumed the temp_employee table has columns emp_id, emp_name, dept, age, salary, location in the above given order and the same datatype.</p><br>
<b>IMPORTANT NOTE:</b>
<br>
<p style="text-indent: 40px">1) When adding a new row, you should ensure the datatype of the value and the column matches</p>
<p style="text-indent: 40px">2) You follow the integrity constraints, if any, defined for the table.</p>
<h2>SQL UPDATE Statement</h2>
<p style="text-indent: 40px">The UPDATE Statement is used to modify the existing rows in a table.</p>
<h2>The Syntax for SQL UPDATE Command is:</h2>
<div class="alert alert-info" role="alert">
UPDATE table_name <br>
SET column_name1 = value1, <br>
column_name2 = value2, ... <br>
[WHERE condition] <br>
</div>
<ul>
<li style="color: black">table_name - the table name which has to be updated.</li>
<li style="color: black">column_name1, column_name2.. - the columns that gets changed.</li>
<li style="color: black">value1, value2... - are the new values.</li>
</ul>
<br>
<p style="text-indent: 40px"><b>NOTE:</b> In the Update statement, WHERE clause identifies the rows that get affected. If you do not include the WHERE clause, column values for all the rows get affected.<br>
<br>
<p style="text-indent: 40px"><b>For Example:</b> To update the location of an employee, the sql update query would be like,<br>
<div class="alert alert-info" role="alert">
UPDATE employee
SET location ='Mysore'
WHERE id = 101;
</div>
<p style="text-indent: 40px">To change the salaries of all the employees, the query would be,<br>
<div class="alert alert-info" role="alert">
UPDATE employee <br>
SET salary = salary + (salary * 0.2); <br>
</div>
<h2>SQL Delete Statement</h2>
<p style="text-indent: 40px">The DELETE Statement is used to delete rows from a table.
<h2>Syntax of a SQL DELETE Statement</h2>
<div class="alert alert-info" role="alert">
DELETE FROM table_name [WHERE condition];
</div>
<ul>
<li>table_name -- the table name which has to be updated.
</ul>
<br>
<p style="text-indent: 40px"><b>NOTE:</b> The WHERE clause in the sql delete command is optional and it identifies the rows in the column that gets deleted. If you do not include the WHERE clause all the rows in the table is deleted, so be careful while writing a DELETE query without WHERE clause.
<br>
<h2>SQL DELETE Example</h2>
<p style="text-indent: 40px">To delete an employee with id 100 from the employee table, the sql delete query would be like,<br>
<div class="alert alert-info" role="alert">
DELETE FROM employee WHERE id = 100;
</div>
<p style="text-indent: 40px">To delete all the rows from the employee table, the query would be like,<br>
<div class="alert alert-info" role="alert">
DELETE FROM employee;
</div>
<h2>SQL TRUNCATE Statement</h2>
<p style="text-indent: 40px">The SQL TRUNCATE command is used to delete all the rows from the table and free the space containing the table.<br>
<h2>Syntax to TRUNCATE a table:</h2>
<div class="alert alert-info" role="alert">
TRUNCATE TABLE table_name;
</div>
<h2>SQL TRUNCATE Statement Example</h2>
<p style="text-indent: 40px">To delete all the rows from employee table, the query would be like,<br>
<div class="alert alert-info" role="alert">
TRUNCATE TABLE employee;
</div>
<p style="text-indent: 40px"><b>Difference between DELETE and TRUNCATE Statements:</b><br>
<br>
<b>DELETE Statement:</b> This command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. But it does not free the space containing the table.<br>
<br>
<b>TRUNCATE statement:</b> This command is used to delete all the rows from the table and free the space containing the table.<br>
<h2>SQL DROP Statement:</h2>
<p style="text-indent: 40px">The SQL DROP command is used to remove an object from the database. If you drop a table, all the rows in the table is deleted and the table structure is removed from the database. Once a table is dropped we cannot get it back, so be careful while using DROP command. When a table is dropped all the references to the table will not be valid.</p>
<p style="text-indent: 40px"><b>Syntax to drop a sql table structure:</b></p>
<div class="alert alert-info" role="alert">
DROP TABLE table_name;
</div>
<h2>SQL DROP Statement Example</h2>
<p style="text-indent: 40px">To drop the table employee, the query would be like:</p>
<div class="alert alert-info" role="alert">
DROP TABLE employee;
</div>
<p style="text-indent: 40px"><b>Difference between DROP and TRUNCATE Statement:</b></p>
<p style="text-indent: 40px">If a table is dropped, all the relationships with other tables will no longer be valid, the integrity constraints will be dropped, grant or access privileges on the table will also be dropped, if you want use the table again it has to be recreated with the integrity constraints, access privileges and the relationships with other tables should be established again. But, if a table is truncated, the table structure remains the same, therefore any of the above problems will not exist.</p>
<h2>SQL CREATE TABLE Statement</h2>
<p style="text-indent: 40px">The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like primary key, unique key, foreign key can be defined for the columns while creating the table. The integrity constraints can be defined at column level or table level. The implementation and the syntax of the CREATE Statements differs for different RDBMS.</p>
<h2>The Syntax for the CREATE TABLE Statement is:</h2>
<div class="alert alert-info" role="alert">
CREATE TABLE table_name <br>
(column_name1 datatype, <br>
column_name2 datatype, <br>
... column_nameN datatype <br>
);
</div>
<ul>
<li style="color: black">table_name - is the name of the table.</li>
<li style="color: black">column_name1, column_name2.... - is the name of the columns</li>
<li style="color: black">datatype - is the datatype for the column like char, date, number etc.</li>
</ul>
<br>
<p style="text-indent: 40px">For Example: If you want to create the employee table, the statement would be like,</p>
<div class="alert alert-info" role="alert">
CREATE TABLE employee <br>
( id number(5), <br>
name char(20), <br>
dept char(10), <br>
age number(2), <br>
salary number(10), <br>
location char(10)
);
</div>
<p style="text-indent: 40px">In Oracle database, the datatype for an integer column is represented as "number". In Sybase it is represented as "int".</p>
<p style="text-indent: 40px">Oracle provides another way of creating a table.</p>
<div class="alert alert-info" role="alert">
CREATE TABLE temp_employee
SELECT * FROM employee
</div>
<p style="text-indent: 40px">In the above statement, temp_employee table is created with the same number of columns and datatype as employee table.</p>
<h1>SQL ALTER TABLE Statement</h1>
<p style="text-indent: 40px">The SQL ALTER TABLE command is used to modify the definition (structure) of a table by modifying the definition of its columns. The ALTER command is used to perform the following functions.</p>
<ol>
<li style="color: black">Add, drop, modify table columns </li>
<li style="color: black">Add and drop constraints </li>
<li style="color: black">Enable and Disable constraints </li>
</ol>
<h2>Syntax to add a column</h2>
<div class="alert alert-info" role="alert">
ALTER TABLE table_name ADD column_name datatype;
</div>
<p style="text-indent: 40px"><b>For Example:</b> To add a column "experience" to the employee table, the query would be like.</p>
<div class="alert alert-info" role="alert">
ALTER TABLE employee ADD experience number(3);
</div>
<h2>Syntax to drop a column</h2>
<div class="alert alert-info" role="alert">
ALTER TABLE table_name DROP column_name;
</div>
<p style="text-indent: 40px"><b>For Example:</b> To drop the column "location" from the employee table, the query would be like.</p>
<div class="alert alert-info" role="alert">
ALTER TABLE employee DROP location;
</div>
<h2>Syntax to modify a column</h2>
<div class="alert alert-info" role="alert">
ALTER TABLE table_name MODIFY column_name datatype;
</div>
<p style="text-indent: 40px"><b>For Example:</b> To modify the column salary in the employee table, the query would be like.</p>
<div class="alert alert-info" role="alert">
ALTER TABLE employee MODIFY salary number(15,2);
</div>
<h1>SQL RENAME Command</h1>
<p style="text-indent: 40px">The SQL RENAME command is used to change the name of the table or a database object.</p>
<p style="text-indent: 40px">If you change the object's name any reference to the old name will be affected. You have to manually change the old name to the new name in every reference.</p>
<h2>Syntax to rename a table</h2>
<div class="alert alert-info" role="alert">
RENAME old_table_name To new_table_name;
</div>
<p style="text-indent: 40px"><b>For Example:</b> To change the name of the table employee to my_employee, the query would be like.</p>
<div class="alert alert-info" role="alert">
RENAME employee TO my_emloyee;
</div>
<h1>SQL RENAME Statement</h1>
<p style="text-indent: 40px">With RENAME statement you can rename a table.</p>
<p style="text-indent: 40px">Some of the relational database management system (RDBMS) does not support this command, because this is not standardizing statement.</p>
<p style="text-indent: 40px">For example renaming a table through MS SQL Server you must use storage procedure SP_RENAME.</p>
<h2>Syntax for SQL RENAME is:</h2>
<div class="alert alert-info" role="alert">
RENAME TABLE {tbl_name} TO {new_tbl_name};
</div>
<p style="text-indent: 40px"><b>Where {tbl_name} table that exists in the current database, and {new_tbl_name} is new table name.</p></b>
<h4>As for Oracle may also be use the following option:</h4>
<div class="alert alert-info" role="alert">
ALTER TABLE {tbl_name} RENAME TO {new_tbl_name};
</div>
As Example<br>
<br>
<div class="alert alert-info" role="alert">
CREATE TABLE employees<br>
( id NUMBER(6),<br>
name VARCHAR(20)<br>
);<br>
INSERT INTO employees( id, name ) values( 1, 'name 1');<br>
INSERT INTO employees( id, name ) values( 2, 'name 2');<br>
INSERT INTO employees( id, name ) values( 3, 'name 3');
</div>
<div class="alert alert-info" role="alert">
SELECT * FROM employees;
</div>
<b>SELECT Output:</b><br>
<br>
<div class="alert alert-info" role="alert">
id name<br>
------------------- -------------------<br>
1 name 1<br>
2 name 2<br>
3 name 3<br>
RENAME TABLE employees TO employees_new;<br>
<br>
SELECT * FROM employees_new;
</div>
<b>SELECT Output:</b><br>
<br>
<div class="alert alert-info" role="alert">
id name<br>
------------------- -------------------<br>
1 name 1<br>
2 name 2<br>
3 name 3<br>
</div>
<b>SQL RENAME NOTES</b><br>
<br>
<p style="text-indent: 40px">1)The minimum version that supports table renaming is Oracle 8i. All the dependencies of the table will automatically updated. No need of updating them after wards</p>
</p>
<br>
<br>
<br>
<br>
</div>
</div>
<!-- /.row -->
</div>
<!-- end of content & container -->
<!-- jQuery Version 1.11.1 -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>