-- 登录数据库
mysql -h127.0.0.1-P3306 -uroot -p;--查看所有数据库showdatabases;--创建数据库createdatabase test;--删除数据库dropdatabase test;--使用数据库use test;
2.表相关
--查看所有的表showtables;--创建表createtable test(id int, name varchar(10), age int,primarykey(id),unique(name),key index_age(age));--删除一个存在的表droptableif exist test;--修改表的名称altertable test rename test_1;renametable test to test_1;--查看表结构desc test;--查看表的创建语句showcreatetable test;
3.表结构相关
--添加字段altertable test add c varchar(2)default''comment'测试'after a;--删除字段altertable test drop c;--修改字段名称和属性altertable test change c d varchar(30);--只修改字段属性altertable test modify d varchar(50);
4.索引的操作
--添加主键索引altertable test addprimarykey(id);--删除主键索引altertable test dropprimarykey;--添加唯一键(带键名称)altertable test addunique uk_name (name);----添加唯一键(不带键名称)altertable test addunique(name);--创建普通索引altertable test addindex a_index (a);--创建联合索引altertable test addindex a_b_index (a,b);--删除索引dropindex a_index on test;