mysql 学习笔记
mysql$ mysql -u root mysql //安装mysql后默认启动命令
mysql$ mysql -u rick -p mysql;//以用户名rick登入mysql,提示输入密码
mysql$ mysql -u rick -p; //以用户名rick登入mysql,提示输入密码
mysql> show databases;//用于显示所有数据库。
mysql> use wangry //使用数据库wangry
mysql> use rick//使用数据库rick,没有;号
mysql$ mysql -u rick -p rick;//在登入是指定使用数据库rick(结尾部),就不用再使用use命令了
mysql> grant all on *.* torick@localhost identified by '123456'; //为rick创建一个本地登入,密码123456
mysql> grant all on *.* torick@'192.168.1.60/255.255.255.0' identified by '123456';创建一个来自C类子网192.168.0的登入
MySQL中SELECT命令类似于其他编程语言里的print或者write,你可以用它来显示一个字符串、数字、数学表达式的结果等等。
mysql> select user, host, password frommysql.user; //显示mysql的用户名,主机,密码
mysql> select database(); //表示当前选择(连接)的数据库
mysql> \q//退出mysql
mysql> Quit //退出mysql
mysql>?; help;//显示命令列表
创建表
创建一个名为childern的表,将每个孩子存储一个唯一的数字、名字和年龄,把孩子编号作为主键(primary key)绿色为数据类型。如下;
mysql> create table children (childnointeger auto_increment notnullprimary key,fnamevarchar(30),ageinteger);
mysql> create table children6(childnointeger auto_increment not null, fname varchar(30), age integer, primary key(childno));
mysql> create table children11(
-> childno integer auto_increment not null,
-> fname varchar(30),
-> age integer,
-> primary key(childno) //把孩子编号作为主键(primary key)
-> ); //想换行按enter,命令的结束标志是;
向表中添加数据
mysql> insert into children(fname,age)values("Jenny", 21); //向表children中添加数据,childno为auto_increment列,mysql会自动分配一个唯一的数字.
mysql> insert into children(fname, age)values("andrew", 17);
使用select从表中提取数据,看是否添加成功了,如下
查看数据库rick里的表
mysql> use rick
Database changed
mysql> show tables;
+----------------+
| Tables_in_rick |
+----------------+
| children |
| children11 |
| children2 |
| children3 |
| children4 |
| children5 |
| children6 |
| children7 |
| children8 |
| children9 |
+----------------+
查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| rick |
| test |
| wangry |
+--------------------+
删除数据库
mysql> drop database test;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| rick |
| wangry |
+--------------------+
查看表的定义
mysql> desc children;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| childno |int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(30) | YES | |NULL | |
| age | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
查看创建表的SQL语句
mysql> show create table children \G;
*************************** 1. row***************************
Table: children
Create Table: CREATE TABLE `children` (
`childno` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(30) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`childno`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULTCHARSET=utf8
删除表children11
mysql> drop table children11;
Query OK, 0 rows affected (0.09 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_rick |
+----------------+
| children |
| children2 |
| children3 |
| children4 |
| children5 |
| children6 |
| children7 |
| children8 |
| children9 |
+----------------+