Linux中Mysql是区分大小写的,win不区分大小写
show databases
查看数据库服务器中数据库输入命令行后需要输入;
作为结束
use <databasesname>
选择一个数据库进行操作mysql> use test Database changed
exit
退出数据服务器create database <name>
数据库服务器中创建数据库mysql> create database study -> ; Query OK, 1 row affected (0.05 sec) mysql> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | study | | test | +--------------------+ 4 rows in set (0.00 sec) mysql>
show tables
查看数据库中的数据表mysql> show tables -> ; Empty set (0.00 sec) mysql>
drop database<datebasename>
删除数据库mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | study | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> drop database test; Query OK, 0 rows affected (0.03 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | study | +--------------------+ 3 rows in set (0.00 sec)
create table <tablename>(datename datatype(size),...)
创建数据表表名字中参数为原始名和元素大小
mysql> create table pet(name varchar(20), owner varchar(20), species varchar(20), sex char(1), birth date, death date); Query OK, 0 rows affected (0.31 sec)
mysql> show tables -> ; +-----------------+ | Tables_in_study | +-----------------+ | pet | +-----------------+ 1 row in set (0.00 sec)
在字段类型之后加上
NOT NULL
即可
create table pet(name varchar(20) NOT NULL)
describe <tablename>
查看数据表的结构mysql> describe pet -> ; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.03 sec)
select <Field> from <tablename>
查看数据表中的记录Field
表示字段名字。通过这个参数可以进行筛选出需要的子段。
*
是全部字段名
mysql> select * from pet -> ; Empty set (0.00 sec)
mysql> select * from pet where name = "Tom" and sex = "f"; +------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +------+-------+---------+------+------------+-------+ | Tom | Diane | ham | f | 1999-03-30 | NULL | +------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
mysql> select * from user limit 2 -> ; +----------+--------+ | username | pass | +----------+--------+ | dawd | 213123 | | dokpd | 213123 | +----------+--------+ 2 rows in set (0.02 sec) mysql> select * from user limit 1; +----------+--------+ | username | pass | +----------+--------+ | dawd | 213123 | +----------+--------+ 1 row in set (0.01 sec)
insert into <tablename>
往数据表中添加数据记录mysql> insert into pet -> values ("Tom", "Diane", "ham", "f", "1999-03-30",NULL); Query OK, 1 row affected (0.11 sec)
输入命令行后需要补齐输入的信息 value (. . .)
mysql> select * from pet -> ; +------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +------+-------+---------+------+------------+-------+ | Tom | Diane | ham | f | 1999-03-30 | NULL | +------+-------+---------+------+------------+-------+ 1 row in set (0.00 sec)
drop table <tablename>
删除数据表mysql> show tables; +-----------------+ | Tables_in_study | +-----------------+ | pet | | tmp | +-----------------+ 2 rows in set (0.00 sec) mysql> drop table tmp; Query OK, 0 rows affected (0.65 sec) mysql> show tables; +-----------------+ | Tables_in_study | +-----------------+ | pet | +-----------------+ 1 row in set (0.00 sec)
update tablename set Field = newValue where conditon...
更新满足条件的值mysql> select * from pet -> ; +------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +------+-------+---------+------+------------+-------+ | Tom | Diane | ham | f | 1999-03-30 | NULL | | Jack | Diane | ham | f | 1999-03-30 | NULL | +------+-------+---------+------+------------+-------+ 2 rows in set (0.00 sec) mysql> update pet set name = "Li" where name = "Tom"; Query OK, 1 row affected (0.12 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from pet -> ; +------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +------+-------+---------+------+------------+-------+ | Li | Diane | ham | f | 1999-03-30 | NULL | | Jack | Diane | ham | f | 1999-03-30 | NULL | +------+-------+---------+------+------------+-------+ 2 rows in set (0.00 sec)
delete from <tablename>
清除数据表中的所有记录mysql> select * from user; +----------+------+ | username | pass | +----------+------+ | 21344 | 1234 | | Tom | 1234 | +----------+------+ 2 rows in set (0.00 sec) mysql> delete from user; Query OK, 2 rows affected (0.02 sec) mysql> select * from user; Empty set (0.00 sec)
mysql> select * from user; +----------+--------+ | username | pass | +----------+--------+ | awdad | 1234 | | dawd | 213123 | | dawda | 1234 | | dokpd | 213123 | | jack | 1234 | | tmp | 1234 | +----------+--------+ 6 rows in set (0.00 sec) mysql> delete from user where pass = 1234; Query OK, 4 rows affected (0.15 sec) mysql> select * from user; +----------+--------+ | username | pass | +----------+--------+ | dawd | 213123 | | dokpd | 213123 | +----------+--------+ 2 rows in set (0.00 sec)