MySql 为关系型数据库RDBMS(Relational Database Management System)这种关系可以理解为表格的表格,一个关系型数据库由一张或多张表组成。
1~连接MySql
命令:mysql -h主机地址 -u用户名 -p
然后输入密码
假如刚好装好mysql,在os x系统下 root的密码会在通知里出现
1、连接本机
mysql -u root -p #root前的空格可以省略
输入密码
2、连接远程主机,假设远程主机IP为110.110.110.110
mysql -h110.110.110.110 -u 用户名 -p 密码
3、退出MySql
exit()
2~修改密码
命令:mysql -u 用户名 -p 旧密码 password 新密码
3.1~创建新的数据库
命令:create database 数据库名[其他选项];
数据库选项:
CHARACTER SET charser_name
COLLATE collation_name
假设创建一个testdb的数据库(mysql对大小写不敏感,这点药注意)
mysql>create database testdb character set utf8;
3.2~显示数据库
命令:show databases;注意追后加s
mysql>show databases;
3.3~删除数据库
命令:drop databsase 数据库名
3.4~连接数据库
命令: use 数据库名
4.1~创建数据表
命令:create table 表名[列表声明]
以创建students表为例,表里存放 id,name,sex,age,address
mysql> create table students(
>id char(10) not null primary key,
>name char(10) not null,
>sex char(6) not null,
>age int not null,
>address char(36) not null);
#not null 表示该列的值不能为空,必须填写
#primary key 表示该列是表的主键,该列的值必须唯一
#char(6)表示值的最大长度为6
4.2~删除数据表
命令 :drop table 表名
4.3~插入数据
命令: insert into 表名 values();
mysql> insert into students values('1','summer','male','24','hangzhou')
4.4~查询表中的数据
select 列名 from 表名 [查询条件]
1.查询表中全部数据
mysql> select * from students;
输出的结果如下:
+----+--------+------+-----+----------+
| id | name | sex | age | address |
+----+--------+------+-----+----------+
| 1 | summer | male | 24 | hangzhou |
+----+--------+------+-----+----------+
1 row in set (0.00 sec)
2.查询表中的特定数据
列如查询id和name这两列
mysql> select id,name from students;
列如查询name为summer的这一列
mysql> select * from students where name = 'summer';
4.5~ 删除表中的数据
命令:delete from 表名 where 表达式
mysql> delte from students where id='1';
4.6~修改表中数据
命令:update 表名 set 字段=新值 where 条件
列如把 id 为1 的 name 改为spring
mysql> update students set name = 'spring' where id = '1'
把所有的age 都加1
mysql> update students set age += 1
4.7~修改表中的列
1,改
命令:alter table 表名 change 列名 新数据类型[其他]
例如把address改为adr
mysql> alter table students change address addre char(36) not null
2,加
命令:alter table add 列名 新数据类型[其他]
例如添加新的列,列名为 test
mysql> alter table add test int(4) default'0'
#defalut'0' 表示默认值为0
3,删
命令:alter table 表名 drop 列名
例如删除 test 这一列
mysql> alter table students drop test;
4.8修改表名
mysql> alter table students rename New_students;
或者
mysql> rename students to New_students;