MySQL

MySQL在连接之前,需要启动服务器守护程序,可以通过下面两种方式之一:

  1. 启动相关 windows 服务,比如 MySQL 服务
  2. 通过命令行窗口,执行 mysqld --defaults-file=my.ini 命令

服务启动后,就可以通过客户端访问了:

mysql -u root -p
mysql -uroot -p(密码)
mysql -h 45.42.32.1 -P 9999 -uroot -p

数据库与账户

登录成功后

查看数据库命令:show databases;

切换数据库:use mysql;

创建数据库:

create database bookstore;
create database bookstore character set UTF8; -- 创建时可以指定字符集
use bookstore;  -- 切换到数据库下
-- 创建表
CREATE
TABLE `cart` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `number` int(11) DEFAULT NULL, `userid` int(11) DEFAULT NULL, `prices` decimal(9,2) DEFAULT NULL, `ststus` int(11) DEFAULT '0', `p_id` int(11) DEFAULT NULL, `o_id` int(11) DEFAULT NULL, PRIMARY KEY (`cid`), KEY `userid` (`userid`), KEY `p_id` (`p_id`), KEY `o_id` (`o_id`), CONSTRAINT `cart_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`uid`), CONSTRAINT `cart_ibfk_2` FOREIGN KEY (`p_id`) REFERENCES `product` (`pno`), CONSTRAINT `cart_ibfk_3` FOREIGN KEY (`o_id`) REFERENCES `orderfrom` (`oid`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

 增加主键

alter table 表名 add primary key 字段名;

索引的创建原则:

1、频繁读取的字段,适合创建

2、更新频繁的字段,不适合

3、数据差异大的字段才适合,比如性别就不适合

创建索引

create index 索引名 on 表名(字段名)

唯一索引 

create unique index 索引名 on 表名(字段名)

查看索引

show index from 表名

删除索引

alter table 表名 drop index 索引名;

posted on 2018-09-20 10:06  东子z  阅读(112)  评论(0编辑  收藏  举报

导航