C:\Users\AN515-44>MySQL -h 127.0.0.1 -uroot -p密码
mysql> create database <库名>; #创建数据库school
结果:
mysql> create database school;
Query OK, 1 row affected (0.03 sec)
mysql> use <库名>; #选择数据库school
结果:
mysql> use school
Database changed
mysql> show databases;
结果:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.05 sec)
drop database <库名>
Demo:
mysql> drop database shool ;
Query OK, 0 rows affected (0.03 sec)
show tables;
Demo:
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.03 sec)
要先选择数据库
CREATE TABLE <表名> ([表定义选项])[表选项][分区选项];
没有选择数据库则CREATE TABLE <库名>.<表名> ([表定义选项])[表选项][分区选项];
例:create table school.class0(id integer(5) zerofill,name varchar(128),teacher varchar(64));
<列名1> <类型1> [,…] <列名n> <类型n>
Demo:
mysql> create table class(class_id int, class_name varchar(128),
class_teacher varchar(64)); #创建表class
结果:
mysql> create table class(class_id int, class_name varchar(128), class_teacher varchar(64)); #创建表class
Query OK, 0 rows affected (0.16 sec)
注:CREATE TABLE 命令语法比较多,其主要是由表创建定义(create-definition)、表选项(table-options)和分区选项(partition-options)所组成的。
CREATE TABLE 语句的主要语法及使用说明如下:
Demo:
mysql> create table school.class2(class_id int, class_name varchar(128), class_teacher varchar(64)); #创建表class2
mysql> create table ‘school’.’class3’(class_id int, class_name varchar(128), class_teacher varchar(64)); #Error! 会创建失败
Demo:
mysql> use school; #选择数据库school
mysql> create table class4(class_id int PRIMARY KEY, class_name varchar(128) NOT NULL, class_teacher varchar(64) UNIQUE); #创建表class3
注意:
desc <表名>;
mysql> show create table <表名>;
Demo:
mysql> desc class;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| class_id | int | YES | | NULL | |
| class_name | varchar(128) | YES | | NULL | |
| class_teacher | varchar(64) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> show create table userinfo;
mysql> insert into <表名> values(<数据1>,...,<数据n>);
mysql> insert into <表名> (列名2,...,列名n) values(<数据2>,...,<数据n>); //从第二列开始
Demo:
mysql> insert into class values(101,'六年级一班','马老师');
Query OK, 1 row affected (0.03 sec)
mysql> insert into class values(102,'六年级二班','王老师');
Query OK, 1 row affected (0.01 sec)
mysql> insert into class1 (name,teacher) values ('五年级四班','张老师');
Query OK, 1 row affected (0.09 sec)
delete from class where id = 100;
SELECT column_name,column_name //列名,使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
FROM table_name //表名
[WHERE Clause] //筛选数据条件
[LIMIT N] //LIMIT 属性来设定返回的记录数
[ OFFSET M] //OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0
Demo:
mysql> select class_id from class;
+----------+
| class_id |
+----------+
| 101 |
| 102 |
+----------+
mysql> select * from class;
+----------+------------+---------------+
| class_id | class_name | class_teacher |
+----------+------------+---------------+
| 101 | 六年级一班 | 马老师 |
| 102 | 六年级二班 | 王老师 |
+----------+------------+---------------+
2 rows in set (0.00 sec)
mysql> select class_id,class_teacher from class;
+----------+---------------+
| class_id | class_teacher |
+----------+---------------+
| 101 | 马老师 |
| 102 | 王老师 |
+----------+---------------+
2 rows in set (0.00 sec)
mysql> select class_teacher from class where class_id = 101;
+---------------+
| class_teacher |
+---------------+
| 马老师 |
+---------------+
1 row in set (0.00 sec)
mysql> select class_id from class where class_teacher = '王老师';
+----------+
| class_id |
+----------+
| 102 |
+----------+
1 row in set (0.01 sec)