MySQL的數據類型,MySQL增刪改--添加主外鍵、添加屬性、刪除主外鍵、改表名、獲取系統當前時間等

ls /etc/rc.d/init.d/mysql56
service mysql56 start

ps aux |grep "mysql"|grep "socket=" --color
mysql -S/var/run/mysqld/mysql56.sock
[root@localhost ~]# service mysql56 start
Starting MySQL.. SUCCESS!
[root@localhost ~]# mysql -S/var/run/mysqld/mysql56.sock

 
vim /etc/my.cnf
做爲mysql這個客戶端程序的配置文件
[mysql]
socket=/var/run/mysqld/mysql56.sock
prompt=\u@\h>

create table table s(stuID int zerofill);
alter database sx charset=utf8                                建立數據庫時將字符集設爲utf8,這樣在sx數據庫下建的表的字符集默認都爲utf8,主要是避免出現亂碼。
alter table s add stuName varchar(32) default 'tom' not null;  主鍵也是一種特殊的索引,自動增加列必須是索引
 alter table s add primary key(stuID) ;    給s表添加主鍵
 alter table s modify stuID int auto_increment;  將s表中的stuID改成自增加變量
 alter table s modify stuID int zerofill auto_increment;
alter table s modify stuID int(4) zerofill auto_increment;
mysql> insert into s(stuName) select '';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> set names utf8;         將客戶端輸入的名字轉爲utf8,保證客戶端與表端字符集一致
Query OK, 0 rows affected (0.00 sec)

mysql> select last_insert_id();         查詢最後一次插入數據的id號
+------------------+
| last_insert_id() |
+------------------+
|                5 |
+------------------+
1 row in set (0.00 sec)
show create table s;                   顯示建立表的過程
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                   |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| s     | CREATE TABLE `s` (
  `stuID` int(4) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `stuName` varchar(32) NOT NULL DEFAULT 'tom',
  PRIMARY KEY (`stuID`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> desc s;                       顯示錶結構
+---------+--------------------------+------+-----+---------+----------------+
| Field   | Type                     | Null | Key | Default | Extra          |
+---------+--------------------------+------+-----+---------+----------------+
| stuID   | int(4) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| stuName | varchar(32)              | NO   |     | tom     |                |
+---------+--------------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
錢,decimal 通常用oracl數據庫
不太精確的用double       float4B double8B
utf8下一個字符絕大多數佔3個字節
mysql> select length('中國人');
+---------------------+
| length('中國人') |
+---------------------+
|                   9 |
+---------------------+
1 row in set (0.00 sec)
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
|              4 |
+----------------+
1 row in set (0.01 sec)

text數據類型不區分大小寫

set數據類型,通常用enum,不用set
set不過是取值範圍的限制
mysql> alter table s add ab set('L','M','N','O');
Query OK, 5 rows affected (0.09 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> desc s;
+---------+--------------------------+------+-----+---------+----------------+
| Field   | Type                     | Null | Key | Default | Extra          |
+---------+--------------------------+------+-----+---------+----------------+
| stuID   | int(4) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| stuName | varchar(32)              | NO   |     | tom     |                |
| ab      | set('L','M','N','O')     | YES  |     | NULL    |                |
+---------+--------------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into s(ab) values('L');
Query OK, 1 row affected (0.00 sec)

mysql> select * from s ;
+-------+---------+------+
| stuID | stuName | ab   |
+-------+---------+------+
|  0001 | tom     | NULL |
|  0002 | tom     | NULL |
|  0003 | tom     | NULL |
|  0004 |         | NULL |
|  0005 | ??      | NULL |
|  0006 | tom     | L    |
+-------+---------+------+
6 rows in set (0.00 sec)

mysql> insert into s(ab) values('L,M,N,O');
Query OK, 1 row affected (0.00 sec)

mysql> select * from s ;
+-------+---------+---------+
| stuID | stuName | ab      |
+-------+---------+---------+
|  0001 | tom     | NULL    |
|  0002 | tom     | NULL    |
|  0003 | tom     | NULL    |
|  0004 |         | NULL    |
|  0005 | ??      | NULL    |
|  0006 | tom     | L       |
|  0007 | tom     | L,M,N,O |
+-------+---------+---------+
7 rows in set (0.00 sec)

日期時間
datetime
date
time
year
timestamp
mysql> -- hiredate 2012-1-2 3:4:5
mysql> alter table stu change a hiredate datetime;        將stu表中的a更名爲hiredate,數據類型爲datetime,即年月日時分秒形式
Query OK, 3 rows affected (0.28 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> insert into stu(hiredate,sno) select '2013-1-2',4;
Query OK, 1 row affected, 1 warning (0.05 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into stu(hiredate,sno) select '2013-1-2 03:04:05',5;
Query OK, 1 row affected, 1 warning (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from stu;
+-----+----------+------+--------+-------+---------------------+
| sno | sname    | sage | deptno | score | hiredate            |
+-----+----------+------+--------+-------+---------------------+
|   0 |          | NULL |   NULL |  1.23 | NULL                |
|   1 | dsfk5945 | 1515 |   NULL |  NULL | NULL                |
|   2 | dsfk5945 | 1515 |      1 |  NULL | NULL                |
|   4 |          | NULL |   NULL |  NULL | 2013-01-02 00:00:00 |
|   5 |          | NULL |   NULL |  NULL | 2013-01-02 03:04:05 |
+-----+----------+------+--------+-------+---------------------+
5 rows in set (0.00 sec)

mysql> create table stu2(a datetime,b timestamp);       建立表stu2,a的數據類型爲datetime,b的數據類型爲timestamp,timestamp會自動獲取系統的當前時間
Query OK, 0 rows affected (0.06 sec)

mysql> desc stu2;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| a     | datetime  | YES  |     | NULL              |                             |
| b     | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)

mysql> select current_timestamp;            獲取當前時間
+---------------------+
| current_timestamp   |
+---------------------+
| 2013-07-18 10:56:52 |
+---------------------+
1 row in set (0.03 sec)
mysql> insert into stu2(a) select '2013-1-2 2:3:4';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from stu2;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2013-01-02 02:03:04 | 2013-07-18 10:57:46 |
+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> select now();       取出當前服務器的時間
+---------------------+
| now()               |
+---------------------+
| 2013-07-18 10:58:17 |
+---------------------+
1 row in set (0.00 sec)
mysql> insert into stu2 select now(),now();
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from stu2;
+---------------------+---------------------+
| a                   | b                   |
+---------------------+---------------------+
| 2013-01-02 02:03:04 | 2013-07-18 10:57:46 |
| 2013-07-18 10:59:22 | 2013-07-18 10:59:22 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> show variables like '%zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)

設計數據庫的時候,三種完整性:
一、實體完整性:經過主鍵來保證
二、引用的完整性:經過外鍵來保證
mysql> alter table stu add foreign key(deptno) references dept(deptno);             將dept表中的deptno做爲stu表中的外鍵
三、用戶自定義的完整性,域的完整性
create table stu(
      stuID int,
       age int check(age >=0 and age<=120)   --在sqlServer中能夠實現,但在Mysql中還實現不了
);
check約束在客戶端實現,不在db中實現
mysql> set foreign_key_checks=on;
Query OK, 0 rows affected (0.00 sec)

mysql> set foreign_key_checks=off;
Query OK, 0 rows affected (0.00 sec)

sql
structured query language
client->server
ddl  definition create dabetase table alter drop table
dml insert delete update select
dcl control grant deny revoke
sql標準:
create table ...;
ansi,iso/iec
ansi C iso c
sql -86
sql:2003
ansi c iso c
gcc
在mysql中,and的優先級比or高
mysql> create database  if not exists sx charset=utf8; 建立數據庫的時候就指定字符集,這樣在數據庫下建立表時,字符集也都是建立數據庫時指定的字符集,可是建議在建立表時指定字符集
在Mysql中不能改數據庫名,但在微軟的sqlServer中能夠改

[root@localhost ~]# mysql ds -e "set names utf8;select * from stu;"      
+-----+---------------+------+--------+-------+----------+
| sno | sname         | sage | deptno | score | hiredate |
+-----+---------------+------+--------+-------+----------+
|   9 | 张三 | NULL |   NULL |  NULL | NULL     |

mysql> desc dept;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno   | int(11)     | NO   | PRI | NULL    |       |
| deptname | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create table t1 like dept;        建立與dept結構一致的表t1
Query OK, 0 rows affected (0.15 sec)

mysql> desc t1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno   | int(11)     | NO   | PRI | NULL    |       |
| deptname | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
|      1 | dsh      |
|      2 | sdfg     |
|      3 | swgrwg   |
+--------+----------+
3 rows in set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

mysql> create table t2 select * from dept;  建立一個與dept結構一致的表t2並將dept中的數據導入到t2中
Query OK, 3 rows affected (0.10 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> desc t2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| deptno   | int(11)     | NO   |     | NULL    |       |
| deptname | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from t2;
+--------+----------+
| deptno | deptname |
+--------+----------+
|      1 | dsh      |
|      2 | sdfg     |
|      3 | swgrwg   |
+--------+----------+
3 rows in set (0.00 sec)

mysql> create table t3 select * from dept where deptno>2; 建立表時刷選數據導入t3中
Query OK, 1 row affected (0.08 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+--------+----------+
| deptno | deptname |
+--------+----------+
|      3 | swgrwg   |
+--------+----------+
1 row in set (0.00 sec)

 alter table t3 rename table3;                        將表名t3改成table3
 alter table table3 add deptLeader varchar(32) not null;          添加屬性deptLeader
alter table table3 drop deptLeader;                                      刪除屬性deptLeader
alter table table3 modify deptName varchar(64) not null default'cc';       
 alter table table3 change deptno deptID int;     修改屬性名稱
alter table table3 add primary key(deptID);   添加主鍵
alter table table3 drop primary key;        刪除主鍵
 alter table stu2 engine=myisam;      設置引擎
alter table stu2 charset=utf8;            設置字符集
mysql> insert into dept values(8,'a1'),(9,'a2'),(10,'a3');  (微軟的sqlserver不支持這樣一次性插入多條數據,逗號隔開,微軟的sqlserver2008能夠支持多條數據的插入eg:insert into dept values(8,'a1') (9,'a2') (10,'a3');只是中間沒逗號而已)
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from stu;
+-----+---------+------+--------+-------+----------+
| sno | sname   | sage | deptno | score | hiredate |
+-----+---------+------+--------+-------+----------+
|   9 | zhaoliu |   20 |      2 |  NULL | NULL     |
+-----+---------+------+--------+-------+----------+
1 row in set (0.00 sec)

mysql> truncate table stu;             將stu表中的數據都刪除
Query OK, 0 rows affected (0.03 sec)

mysql> select * from stu;
Empty set (0.00 sec)

mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
|      1 | dsh      |
|      2 | sdfg     |
|      3 | swgrwg   |
|      5 | sales    |
|      6 | NULL     |
|      8 | a1       |
|      9 | a2       |
|     10 | a3       |
+--------+----------+
8 rows in set (0.00 sec)
mysql> select deptno into @a from dept where deptname='dsh';
Query OK, 1 row affected (0.00 sec)

mysql> update stu set age=age*1.1 where deptno=@a;
mysql> select @a;
+------+
| @a   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql

相關文章
相關標籤/搜索