mysql的命令二
1.插入数据
格式一:insert into table_name valuse (字段1,字段2);
insert test1 values ('wangsan',22,'male');
格式二:insert into table_name valuse (字段1,字段2);
insert into test1 values ('lisi',22,'male');
格式三:只插入某一个属性insert into table_name (字段名) values (字段属性);
例子:insert into test1 (gender) values ('male');
2.更新属性
格式:update table_name set 需要改的字段名=改后的字段名 where 条件;
update test1 set name='zhao' where age=22; #修改名为age=22的信息为zhao
3.想清空表数据但是要保存表数据
mysql> truncate table test1; #清空test1表数据,保留表结构 mysql> select * from test1; Empty set (0.00 sec) #数据库内空 mysql> desc test1; #查看表结构 +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | age | int(99) | YES | | NULL | | | gender | char(10) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
4.数据库一些配置文件
主配置文件:/etc/my.cnf
数据库目录:/var/lib/mysql
服务启动日志目录:/var/log/mysqld.log
端口号:3306
程序名:mysqld
传输协议:tcp
socket地址:支持远程通信ip:3306 仅支持本地通信:/var/lib/mysql/mysql.sock
5.mysql约束条件
例子:create table test2 (name char(30)not null,age tinyint(10));
mysql> insert into test2 (age) values (8); ERROR 1364 (HY000): Field 'name' doesn't have a default value #name的值不能为空
primary key主键,值不能重复且不能为null
mysql> alter table test2 add id tinyint(30)primary key; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc test2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | char(30) | NO | | NULL | | | age | int(10) | YES | | 5 | | | id | tinyint(30) | NO | PRI | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
6.mysql索引
index普通索引
unqiue唯一索引
fulltest模糊索引,仅支持char ,varchar,test
primary key主键
foreign key外键
创建表同时创建索引
create table db1 (name char(30),id tinyint(1) index(id));
给已存在的表添加主键
alter table db1 add primary key(age);
浙公网安备 33010602011771号