mysql sql語句學習(一)
為了學習資料庫,先熟悉一下sql語言,也就以mysql為例子開始學習了!
(參考書籍《深入淺出mysql 資料庫開發、最佳化、管理維護》)
下邊是執行的sql語句,主要是建立表和修改表:
首先進入windows命令提示符下:
Microsoft Windows XP [版本 5.1.2600]
(C) 版權所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>mysql -uroot -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.41-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.02 sec)
mysql> use test;
Database changed
mysql> create table emp(ename varchar(20) not null primary key,hirdate date,sal
decimal(10,2),deptno int(2));
Query OK, 0 rows affected (0.08 sec)
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(20) | NO | PRI | NULL | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
mysql> show create table emp;
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
| emp | CREATE TABLE `emp` (
`ename` varchar(20) NOT NULL,
`hirdate` date DEFAULT NULL,
`sal` decimal(10,2) DEFAULT NULL,
`deptno` int(2) DEFAULT NULL,
PRIMARY KEY (`ename`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table emp modify ename varchar(30);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(30) | NO | PRI | | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table emp add column age int(3);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(30) | NO | PRI | | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp change age agenum int(4);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(30) | NO | PRI | | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
| agenum | int(4) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp drop agenum;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(30) | NO | PRI | | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table emp add column birth date after ename;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename | varchar(30) | NO | PRI | | |
| birth | date | YES | | NULL | |
| hirdate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp modify sal first;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'first
' at line 1
mysql> alter table emp modify sal decimal(10,2) first;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| sal | decimal(10,2) | YES | | NULL | |
| ename | varchar(30) | NO | PRI | | |
| birth | date | YES | | NULL | |
| hirdate | date | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp change ename name varchar(20) first;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| name | varchar(20) | NO | PRI | | |
| sal | decimal(10,2) | YES | | NULL | |
| birth | date | YES | | NULL | |
| hirdate | date | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp change birth birthday date after hirdate;
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name | varchar(20) | NO | PRI | | |
| sal | decimal(10,2) | YES | | NULL | |
| hirdate | date | YES | | NULL | |
| birthday | date | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table emp rename emptable;
Query OK, 0 rows affected (0.22 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| emptable |
+----------------+
1 row in set (0.00 sec)
mysql> drop table emptable;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> exit;
Bye
C:\Documents and Settings\Administrator>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22275400/viewspace-626291/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySql 學習筆記一:SQL語句優化MySql筆記優化
- SQL 語句學習SQL
- sql語句學習SQL
- sql 語句練習 In MySQLMySql
- sql 語句練習(3) In MySQLMySql
- sql語句學習總結SQL
- 簡單的SQL語句學習SQL
- Java學習筆記--sql語句Java筆記SQL
- MySql 常用Sql語句MySql
- 學習MySQL的select語句MySql
- 學習筆記----一句SQL語句寫個乘法表筆記SQL
- SQL語句基礎學習之TRIMSQL
- MySQL特有的SQL語句 第一彈MySql
- MySQL入門---(一)SQL的DDL語句MySql
- 【MySQL】經典資料庫SQL語句編寫練習題——SQL語句掃盲MySql資料庫
- MYSQL SQL語句優化MySql優化
- mysql捕捉所有SQL語句MySql
- 【SQL Server學習筆記】Delete 語句、Output 子句、Merge語句SQLServer筆記delete
- MyBatis學習 之 三、動態SQL語句MyBatisSQL
- MySQL事務控制語句(學習筆記)MySql筆記
- sql 語句練習(2)SQL
- MySQL基本sql語句總結MySql
- mysql 常用sql語句 簡介MySql
- MySQL之SQL語句優化MySql優化
- MySQL中常用的SQL語句MySql
- MySql與Sql Server Update語句MySqlServer
- MYSQL 常用sql語句小結MySql
- 從一個MysqL的例子來學習查詢語句(轉)MySql
- MySQL的一些常用的SQL語句整理MySql
- MySQL的SQL語句最佳化一例MySql
- MySQL學習筆記之SQL語句建立、修改和刪除資料庫MySql筆記資料庫
- mysql基礎複習(SQL語句的四個分類),MySql
- MySQL指南之SQL語句基礎MySql
- MySql常用操作SQL語句彙總MySql
- mysql執行sql語句過程MySql
- mysql的sql語句執行流程MySql
- mysql空間大小的SQL語句MySql
- mysql 查詢建表語句sqlMySql