1、char varchar区别在哪里?
char是固定长度类型,varchar是可变长度,当insert的值比较小的时候建议用varchar,否则有点浪费空间。
2、在定义列的属性的时候指定“zerofill”属性时,当数值的实际宽度小于指定列的宽度的时候会自动用0补充。
3、text和blob的区别主要就是blob保存二进制数据。
4、建立外键连接:
create table class(
code varchar(20) primary key,
name varchar(20) not null
);
create table aa(
id int auto_increment primary key,
uid varchar(20),
name varchar(20),
class varchar(20),
foreign key(class) references class(code)
);
5、主键最后指定类型
create table class(
code varchar(20) auto_increment,
name varchar(20) not null,
primary key (code)
) default charset = utf8;
6、添加字段
alert table 表名 add colunn 字段 type after 字段(在哪个字段后面添加)
7、删除字段
alert table 表名 drop column 字段
8、删除主键,在删除主键之前应该先删除自增长,否组不允许删除
alert table 表名 change id id int(10); --删除自增长
alert table 表名 drop primary key;
9、添加索引
alert table 表名 add index ind_ycs_set_1(字段) --为字段添加索引
例如:在product表中为productname列创建一个索引,索引使用列名称的前10个字符
create index index_pname on product (productname(10))
10、创建唯一索引,唯一索引的作用不是用来提高查询速度的(普通索引才是为了提高查询速度),它是用来避免数据出现重复。
create table aa(
id int auto_increment primary key,
uid varchar(20),
name varchar(20),
class varchar(20),
unique key suoyin(uid) --为字段“uid”创建了名为suoyin的唯一索引
);
11、删除索引
alert table aa drop index suoyin;
12、有时候我们会需要清除一个表中的所有资料。要达到者个目的,一种方式是我们在 SQL DROP 那一页 看到 的 DROP TABLE 指令。不过这样整个表格就消失,而无法再被用了。另一种方式就是运用 truncate table 的指令。在这个指令之下,表格中的资料会完全消失,可是表格本身会继续存在。
mysql小知识记录
最新推荐文章于 2024-07-31 11:15:19 发布