-
創建表
caeate table 表名 (字段1 數據類型,字段2 數據類型,字段3 數據類型...)
-
使用Null值
默認允許字段爲null值
使用not null,可以設置字段不允許爲空值create table 表名 (字段 數據類型 not null)
-
主鍵在介紹
create table 表名 (字段1 數據類型 not null primary key)
可以有符合組件的形式
主鍵值必須唯一
主鍵必須爲not null(也可以先創建表,之後在修改表時約束主鍵爲not null) -
使用auto_increment:是某列的值自動遞增
create table 表名( 字段1 數據類型 not null auto_increment primary key)
一個表中 通常只允許一個列自動遞增
需要設爲主鍵 -
default:指定默認值
create table 表名(字段1 數據類型 not null primary key,字段2 數據類型 default "默認的值")
在新增行時,如果沒有給字段2賦值,則字段2會是默認的值
默認值只會在新增時起到作用,修改時不會有默認值
不允許函數作爲默認值 -
engine引擎類型
create table 表名 (字段1 數據類型 not null auto_increment primary key,字段2 數據類型 default “默認值”) engine=類型
mysql中有多種引擎
主要引擎:
innodb:是一個可靠地事務處理引擎,但不支持全文本搜索
myisam:是一個性能極高的引擎,他支持全文本搜索,但是不支持事務處理
memory:在功能等同於myisam,但是由於數據庫存儲在內存中,速度極快,適合緩存
引擎類型可以混用,(即同一個數庫中某些表用innodb 有些表使用myisam)
但是不能跨引擎,
可以預先設置好數據庫的默認引擎:在數據庫的安裝文件中my.ini文件中default-storage-engine=innodb -
alter table:更新表:對錶結果進行修改(注意不是對數據進行修改)
-
alter table 表名 操作 字段
-
add
-
drop
alter table orders add cust_country
在orders表中添加一個字段cust_country
alter table orders add cust_country
在orders表中刪除cust_country字段
alter table orders add constraint FK_orders_customers foreign key(cust_id) references customers(cust_id)
表添加一個約束,約束名字:FK_orders_customers 約束類型:foreign key 關聯 customers表的cust_id字段
- 刪除表
drop table 表名
- 重命名錶
rename table 原表名 to 新表名