1. 程式人生 > >Mysql ==》 文件(表)

Mysql ==》 文件(表)

mys 字段名 utf cnblogs show 復制 key 創建 記錄

表介紹:

表相當於文件,表中的一條記錄就相當於文件的一行內容,不同的時,表中的一條記錄有對應的標題,稱為表的字段。

例如: id,name ,age 就稱為字段,其余的,一行內容稱為一條記錄。

1.創建表

語法:
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件]
);

例子:
create table t1 (id int,name char(12));
create table t1 (id int,name char(12)) engine =innodb default charset utf8;
註意: 1.在用一張表中,字段名是不能相同的。 2.寬度和約束條件可選,即 可有可無 3.字段名和類型時必須的。

2.查看表

語法:
1.show tables;
2.show create table t1;

查看表結構:
1.describe  t1;  
2.desc  t1;

3.修改表

語法:
1. alter table t1 modify name char(12);   #修改表中字段的寬度
2. alter table course charset = utf8;        #修改表中的類型
3.alter table t1 modify name char(12) character set utf8; #修改表中的類型
4.alter table t1 rename t2;         #重命名表的名字
5.alter table t1 add age int;   #添加表中的字段
6.alter table t1 drop age;      #刪除表中的字段
7.alter table t1 modify id int  not null primary key auto_increment;  #修改為主鍵和自動增長
8.alter table t1 modify id int  not null;   #刪除自增約束
9.alter table t1 drop primary key;   #刪除主鍵

4.復制表

復制表結構+記錄(key 不會被復制: 主鍵、外鍵和索引)
語法:
1. create table new_t1 select * from t1;

只復制表結構:
1.select * from t1 where 1=2;  #條件為假,查不到任何記錄,所以不會復制表的記錄
2.create table new_t2 select * from t1 where 1=2;  #只復制表的結構

5.刪除表

語法:
drop  table  表名;

例如:
drop  table t1;

  

Mysql ==》 文件(表)