Example 1:mysql
Create table if not exists `secondweek_tb`( `id` int Unsigned auto_increment, `title` Varchar(100) Not null, `author` Varchar(40) Not null, `submission_date` Date, primary key ( `id` ) )engine=InnoDB default charset=utf8;
解释:sql
Unsigned
声明为无符号;数据库
若是你不想字段为NULL能够设置字段的属性为Not NULL
, 若是在操做数据库时若是输入该字段的数据为NULL ,就会报错;ui
Auto_increment
定义列为自增的属性,通常用于主键,数值会自动加1;编码
Primary key
关键字用于定义列为主键。 您能够使用多列来定义主键,列间以逗号分隔;code
Engine
设置存储引擎;blog
Charset
设置编码;排序
Varchar(100) 和 Varchar(40)的区别:
一个汉字占多少长度与编码有关,以下说MySQL 5.0 以上的版本设置;索引
Varchar(100)表示字节的取值范围为0-65535 字节,可是可变长varchar(100);rem
4.0版本如下,varchar(100),指的是100字节,若是存放UTF8汉字时,只能存33个(每一个中文3字节);
5.0版本以上,varchar(100),指的是100字符,不管存放的是数字、字母仍是UTF8中文(每一个中文3字节),均可以存放100个 ;
Example 2:
CREATE TABLE test_tb( id SMALLINT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL UNIQUE, password CHAR(32) NOT NULL, email VARCHAR(50) NOT NULL DEFAULT 'a@a.com', age TINYINT UNSIGNED DEFAULT 18, addr VARCHAR(200) NOT NULL DEFAULT '北京', salary FLOAT(6,2), regTime INT UNSIGNED, face CHAR(100) NOT NULL DEFAULT 'default.jpg', sex ENUM('男','女','保密') DEFAULT '保密' )engine=InnoDB default charset=utf8;
约束 = 别乱搞;
约束就是在定义字段类型的时候,我就先定义好规则,不让你乱插入(别乱搞!);
主键约束又能够分为:
Example:
# 单字段主键 id Int Primary Key, # 多字段主键 id int, uid int Primary Key(id,uid),
字段值不能为空,若是为空报错!
example:
name char(20) Not null;
保证数据表中的字段惟一性,即字段值不能重复;
phone int Unique,
当插入空值时,默认填充默认值;
sex char(5) Default 0,
待续;
建立表时字段类型后面加上 auto_increment,该列数据数值就会自动+1;
`id` int auto_increment, #定义列为自增的属性,通常用于主键,数值会自动加1;
索引就是用来加快数据表的查询和排序用的;
了解每种(6种)索引方式的应用场景、优缺点;
有两种方式;
Alter Table test1_tb Rename To test2_tb;
Rename Table test2_tb To test1_tb;
alter table test change column address address1 varchar(30);
alter table test modify address char(10);
待续;
待续;
alter table test1_tb add column name varchar(10);
alter table test1_tb drop column age;
待续;
待续;
Drop table name_table;