faqts : Computers : Databases : MySQL : Language and Syntax : Queries : Create Table

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

Database: Relational: MySql: Key: Primary: Unique: Value: Integer:Default: How create default value?

Jul 1st, 2008 11:35
sheana krat, Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 20 March 2005 - 05:22 am ----------------------

Database: Relational: MySql: Key: Primary: Unique: Value: 
Integer:Default: How create default value?

---

Method: Insert a begin value in the primary key column.

Insert a begin value in the primary key auto_increment column the first
time.

Then after that let it be increased automatically via this
AUTO_INCREMENT.

---

e.g.

--- cut here: begin --------------------------------------------------

-----------------------------------------------
CREATE DATABASE
 databaseMyTest;
-----------------------------------------------
USE
 databaseMyTest;
-----------------------------------------------
DROP TABLE
 tableTest1;
-----------------------------------------------
CREATE TABLE
 tableTest1
(
 columnPrimaryKey1 INT AUTO_INCREMENT PRIMARY KEY,
 nameS VARCHAR( 50 )
)
;
-----------------------------------------------
-- this gives a default value to the primary key
INSERT INTO
 tableTest1
(
 columnPrimaryKey1,
 nameS
)
VALUES
(
 100000,
 "US"
)
;
-----------------------------------------------
INSERT INTO
 tableTest1
(
 nameS
)
VALUES
(
 "DE"
)
;
-----------------------------------------------
INSERT INTO
 tableTest1
(
 nameS
)
VALUES
(
 "GB"
)
;
-----------------------------------------------
SELECT
 nameS,
 columnPrimaryKey1
FROM
 tableTest1
;
-----------------------------------------------

When you run this, this shows output similar to the following:

--- cut here: begin --------------------------------------------------

mysql> source c:\temp\ddd.sql
ERROR 1007 (HY000): Can't create database 'databasemytest'; database 
exists
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.09 sec)

Query OK, 0 rows affected (0.12 sec)

Query OK, 1 row affected (0.05 sec)

Query OK, 1 row affected (0.06 sec)

Query OK, 1 row affected (0.10 sec)

+-------+-------------------+
| nameS | columnPrimaryKey1 |
+-------+-------------------+
| US    |            100000 |
| DE    |            100001 |
| GB    |            100002 |
+-------+-------------------+
3 rows in set (0.01 sec)

--- cut here: end ----------------------------------------------------

---
---

Internet: see also:

---

Database: Language: SQL: Overview: Can you give an overview of links 
about SQL?
http://www.faqts.com/knowledge_base/view.phtml/aid/32811/fid/54

----------------------------------------------------------------------
http://www.indianhandicraftsexporters.com
http://www.topgiftsideas.com
http://www.eindianweddingcards.com