创建第一张表
创建一个库
create database 库名;
切换
use 库名;
创建一个表的语法
#语法:
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
#注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的
增加表中内容
insert into 表名(键1,键2,键3) values #插入(值1,值2,值3);
ps 注意插入值 字符串 形式
实列
创建一张表
mysql> create database staff;#创建库
Query OK, 1 row affected (0.00 sec)
mysql> use staff; #切换库
Database changed
mysql> create table staff_info (#创建表
id int,
name varchar(50),
age int(3),
sex enum('male','female'),
phone bigint(11),
job varchar(11));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;#查看所有表
+-----------------+
| Tables_in_staff |
+-----------------+
| staff_info |
+-----------------+
1 row in set (0.00 sec)
mysql> desc staff_info;#查看表类型
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| phone | bigint(11) | YES | | NULL | |
| job | varchar(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> select id,name,sex from staff_info;#查询
Empty set (0.00 sec)
mysql> select * from staff_info;#查询
Empty set (0.00 sec)
mysql> insert into staff_info (id,name,age,sex,phone,job) values (1,'Alex',83,'female',13651054608,'IT');#插入
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values #插入(2,'Egon',26,'male',13304320533,'Teacher');
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff_info values #插入(3,'nezha',25,'male',13332353222,'IT'),(4,'boss_jin',40,'male',13332353333,'IT');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from staff_info;#查询
+------+----------+------+--------+-------------+---------+
| id | name | age | sex | phone | job |
+------+----------+------+--------+-------------+---------+
| 1 | Alex | 83 | female | 13651054608 | IT |
| 2 | Egon | 26 | male | 13304320533 | Teacher |
| 3 | nezha | 25 | male | 13332353222 | IT |
| 4 | boss_jin | 40 | male | 13332353333 | IT |
+------+----------+------+--------+-------------+---------+
4 rows in set (0.00 sec)
非学,无以致疑;非问,无以广识