建database
Create database newdatabase;
使用表
Use newdatabase;
alter table 旧表名 rename 新表名; --修改表名
alter table tb_dept2 change column id tb_id int(10);Query OK, 7 rows affected (0.14 sec) 修改列名
Unsigned 无符号不允许负数最小值为0最大值255, ZEROFIL 数值之前自动用0补气例如int(3)ZEROFILL 输出数据为“00X”是无符号的, AUTO_INCREMENT自增长的字段, NULL插入值是没有数据默认为null,如果指定not null 插入值时必须在此字段前添加值,
1.创表
CREATE TABLE Studen_test(user_id INT NOT NULL AUTO_INCREMENT,name VARCHAR(30)NOT NULL,age VARCHAR(10) NOT NULL,Borthday DATE,PRIMARY KEY (user_id)); PRIMARY KEY (中间有空格)
2. 插入数据
INSERT INTO Student_test(name,borthday,age) VALUES ("小花","1991-03-27","16");
插入多条数据
insert into score (name , english , chinese) values("jing" , "120","130"),("jing1","121","131"),("jing2","122","132");
3.更新数据
UPDATE Studen_test SET age="17",Borthday="1991-03-27" WHERE user_id = 2;
4.查找数据
select * from Studen_test Where age = 12;
5.删除数据DELECT FROM Studen_test WHERE user_id =1;
MySQL添加字段和删除字段 alter add命令用来增加表的字段。 alter add命令格式:alter table 表名 add字段 类型 其他;
例如,在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0:
mysql> alter table MyClass add passtest int(4) default '0';
1) 加索引 mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]); 例子: mysql> alter table employee add index emp_name (name);
alter table tb_login(表名) add name()表字段 varchar (15) default '天添'(默认值);
2) 加主关键字的索引 mysql> alter table 表名 add primary key (字段名); 例子: mysql> alter table employee add primary key(id);
3) 加唯一限制条件的索引 mysql> alter table 表名 add unique 索引名 (字段名); 例子: mysql> alter table employee add unique emp_name2(cardnumber);
4) 删除某个索引 mysql> alter table 表名 drop index 索引名; 例子: mysql>alter table employee drop index emp_name;
5) 增加字段 mysql> ALTER TABLE table_name ADD field_name field_type;
6) 修改原字段名称及类型 mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
7) 删除字段MySQL ALTER TABLE table_name DROP field_name;
8) mysql 获取偶数行&&奇数行 mod(id,2)=0
9) 查询单个值或者多个值 select sex,age FROM Studen_test;
10)MySQL LIKE 子句
SELECT sex FROM Studen_test WHERE sex LIKE "%1";
11)SQL UNION 实例 UNION ALL
UNION ALL 从两个表中获取所有不同的值(只有不同的值)
UNION ALL 从两个表中获取所有的值)
SELECT age FROM Studen_test UNION SELECT age FROM teacher;
生序排序 ORDER BY * ASC
SELECT age FROM Studen_test UNION SELECT age FROM teacher ORDER BY age ASC ;
降序 ORDER BY * DESC
SELECT age FROM Studen_test UNION SELECT age FROM teacher ORDER BY age DESC ;
12)创建表用户操作
create user jss;
13)显示表用户
select user from mysql.user;
1.创表
CREATE TABLE Studen_test(user_id INT NOT NULL AUTO_INCREMENT,name VARCHAR(30)NOT NULL,age VARCHAR(10) NOT NULL,Borthday DATE,PRIMARY KEY (user_id));
2. 插入数据
INSERT INTO Student_test(name,borthday,age) VALUES ("小花","1991-03-27","16");
3.更新数据 UPDATE Studen_test SET age="17",Borthday="1991-03-27" WHERE user_id = 2;
4.查找数据 select * from Studen_test Where age = 12;
5.删除 DELECT FROM Studen_test WHERE user_id =1;
MySQL添加字段和删除字段 alter add命令用来增加表的字段。
alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0: mysql> alter table MyClass add passtest int(4) default '0';
1) 加索引 mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);
例子: mysql> alter table employee add index emp_name (name);
2) 加主关键字的索引 mysql> alter table 表名 add primary key (字段名);
例子: mysql> alter table employee add primary key(id);
3) 加唯一限制条件的索引 mysql> alter table 表名 add unique 索引名 (字段名); 例子: mysql> alter table employee add unique emp_name2(cardnumber);
4) 删除某个索引 mysql> alter table 表名 drop index 索引名; 例子: mysql>alter table employee drop index emp_name;
5) 增加字段 mysql> ALTER TABLE table_name ADD field_name field_type; 6) 修改原字段名称及类
Between 使用 搜索的是全部字段
mysql> select * from tb_login where age between 5 and 10;
+----+----------+--------+---------+-----------+------+
| id | user | pwd | section | name | age |
+----+----------+--------+---------+-----------+------+
| 5 | mrkj1 | passwd | ceshi| tiantian | 5 |
| 6 | jingjing | passwd | ceshi| tiantian | 6 |
| 7 | jingjing | passwd | ceshi| tiantian | 7 |
| 8 | jingjing | passwd | ceshi| tiantian | 8 |
| 9 | jingjing | passwd | ceshi| tiantian | 9 |
| 10 | jingjing | passwd | ceshi| tiantian | 10 |
| 17 | 0ajing | passwd | ceshi| tiantian | 9 |
| 18 | 1ajing | passwd | ceshi| tiantian | 8 |
| 19 | 2ajing | passwd | ceshi| tiantian | 7 |
| 20 | 2ajing | passwd | ceshi | 0tiantian | 6 |
| 21 | 2ajing | passwd | ceshi| atiantian | 5 |
+----+----------+--------+---------+-----------+------+
11 rows in set (0.00 sec)
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1):
mysql> SELECT * FROM table LIMIT 5,10;// 检索记录行 6-15
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1;
//检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行 //换句话说,LIMIT n 等价于 LIMIT 0
mysql> select age , user from tb_login where age between 8 and 10 limit 2;
+------+----------+
| age | user |
+------+----------+
| 8 | jingjing |
| 9 | jingjing |
+------+----------+
# 加扣号和不加括号的区别 and具有最高优先级
mysql> select user , age from tb_login where user = "jingjing" or user = "2ajing" and age between 7 and 10 ;
+----------+------+
| user | age |
+----------+------+
| jingjing | 3 |
| jingjing | 6 |
| jingjing | 7 |
| jingjing | 8 |
| jingjing | 9 |
| jingjing | 10 |
| jingjing | 11 |
| 2ajing | 7 |
+----------+------+
8 rows in set (0.01 sec)
mysql> select user,agefrom tb_login where( user = "jingjing" or user = "2ajing") and ( age between 7 and 10 );
+----------+------+
| user | age |
+----------+------+
| jingjing | 7 |
| jingjing | 8 |
| jingjing | 9 |
| jingjing | 10 |
| 2ajing | 7 |
+----------+------+
5 rows in set (0.01 sec)
In 操作 in( , ) 取选中的值 一个或者多个用逗号隔开
select user,agefrom tb_login where( user = "jingjing" or user = "2ajing") and ( age in( 7 ) );
+----------+------+
| user | age |
+----------+------+
| jingjing | 7 |
| 2ajing | 7 |
+----------+------+
2 rows in set (0.00 sec)
select user,agefrom tb_login where( user = "jingjinger = "2ajing") and ( age in( 7 , 10) );
+----------+------+
| user | age |
+----------+------+
| jingjing | 7 |
| jingjing | 10 |
| 2ajing | 7 |
+----------+------+
3 rows in set (0.01 sec)
not in ( , ) 取选中的值 一个或者多个用逗号隔开
select user,agefrom tb_login where( user = "jingjing" or user = "2ajing") and ( age not in( 7 ) );
+----------+------+
| user | age |
+----------+------+
| jingjing | 3 |
| jingjing | 6 |
| jingjing | 8 |
| jingjing | 9 |
| jingjing | 10 |
| jingjing | 11 |
| 2ajing | 6 |
| 2ajing | 5 |
| 2ajing | 4 |
| 2ajing | 3 |
| 2ajing | 2 |
+----------+------+
select user , age from tb_login where (user = "jingjing" or user = "2ajing" or user ="1ajing") and (age not in (7,10 ));
+----------+------+
| user | age |
+----------+------+
| jingjing | 3 |
| jingjing | 6 |
| jingjing | 8 |
| jingjing | 9 |
| jingjing | 11 |
| 1ajing | 8 |
| 2ajing | 6 |
| 2ajing | 5 |
| 2ajing | 4 |
| 2ajing | 3 |
| 2ajing | 2 |
+----------+------+
11 rows in set (0.00 sec)
通配符 很耗费时间 like %
select user from tb_login where user like "jing%";
+----------+
| user |
+----------+
| jingjing |
| jingjing |
| jingjing |
| jingjing |
| jingjing |
| jingjing |
| jingjing |
| jingjin |
| jingji |
| jingj |
| jing |
+----------+
11 rows in set (0.01 sec)
LIKE 字段
select user ,name from tb_login where user like "%ing%";
+----------+-----------+
| user | name |
+----------+-----------+
| jingjing | 天添 |
| jingjing | tiantian |
| jingjing | tiantian |
| jingjing | tiantian |
| jingjing | tiantian |
| jingjing | tiantian |
| jingjing | tiantian |
| jingjin| tiantian |
| jingji| tiantian |
| jingj| tiantian |
| jing| tiantian |
| ajing| tiantian |
| 0ajing | tiantian |
| 1ajing | tiantian |
| 2ajing | tiantian |
| 2ajing | 0tiantian |
| 2ajing | atiantian |
| 2ajing | ctian |
| 2ajing | 0ctian |
| 2ajing | 7ctian |
+----------+-----------+
20 rows in set (0.00 sec)
_ 下划线通配符只匹配一个字段
select user from tb_login where user like "_ing_";
+-------+
| user |
+-------+
| jingj |
+-------+
1 row in set (0.00 sec)
Concatenate 桥接
Select Conca( ‘X’,’X’ ) from X
返回平均值并且作为一个新值保存
select avg(age) as New_Age from tb_login;
+---------+
| New_Age |
+---------+
| 6.4615 |
+---------+
1 row in set (0.02 sec)
使用子查询
select id ,user from tb_login where age in ( select age from tb_login where location = "china" );
+----+----------+
| id | user |
+----+----------+
| 1 | mrkj |
| 2 | mrk |
| 4 | mrkj |
| 6 | jingjing |
| 8 | jingjing |
| 12 | jingjin |
| 13 | jingji |
| 14 | jingj |
| 15 | jing |
| 16 | ajing |
| 18 | 1ajing |
| 20 | 2ajing |
| 22 | 2ajing |
| 24 | 2ajing |
+----+----------+
Operand should contain 1 column(s)。
原因是in条件后面有多个字段,in后面只能有一个字段。
创建联结表
create table tbl_deptlj1 (id int(11) not null auto_increment,dept_name varchar(30) , loc_add varchar(30),primary key (id));
create table tbl_deptlj2 (id int(10) not null auto_increment , name varchar(10), dept_id int(11), primary key (id) , foreign key (dept_id) references tbl_deptlj1(id));
内连接
select * from tb_dept1 a inner join tb_dept2 b on a.id = b.dept_id;
+----+-----------+---------+-------+------+---------+
| id | dept_name | loc_add | tb_id | name | dept_id |
+----+-----------+---------+-------+------+---------+
| 1 | RD | 11 | 1 | z3 | 1 |
| 1 | RD | 11 | 2 | z4 | 1 |
| 1 | RD | 11 | 3 | z5 | 1 |
| 2 | HD | 12 | 4 | w5 | 2 |
| 2 | HD | 12 | 5 | w6 | 2 |
| 3 | MK | 13 | 6 | s7 | 3 |
| 4 | MIS | 14 | 7 | s8 | 4 |
+----+-----------+---------+-------+------+---------+
7 rows in set (0.00 sec)
左外链接
select * from tb_dept1 a left join tb_dept2 b on a.id= b.dept_id;
+----+-----------+---------+-------+------+---------+
| id | dept_name | loc_add | tb_id | name | dept_id |
+----+-----------+---------+-------+------+---------+
| 1 | RD | 11 | 1 | z3 | 1 |
| 1 | RD | 11 | 2 | z4 | 1 |
| 1 | RD | 11 | 3 | z5 | 1 |
| 2 | HD | 12 | 4 | w5 | 2 |
| 2 | HD | 12 | 5 | w6 | 2 |
| 3 | MK | 13 | 6 | s7 | 3 |
| 4 | MIS | 14 | 7 | s8 | 4 |
| 5 | FD | 15 | NULL | NULL | NULL |
+----+-----------+---------+-------+------+---------+
8 rows in set (0.02 sec)
右外链接
select * from tb_dept1 a right join tb_dept2 b on a.id = b.dept_id;
+------+-----------+---------+-------+------+---------+
| id | dept_name | loc_add | tb_id | name | dept_id |
+------+-----------+---------+-------+------+---------+
| 1 | RD | 11 | 1 | z3 | 1 |
| 1 | RD | 11 | 2 | z4 | 1 |
| 1 | RD | 11 | 3 | z5 | 1 |
| 2 | HD | 12 | 4 | w5 | 2 |
| 2 | HD | 12 | 5 | w6 | 2 |
| 3 | MK | 13 | 6 | s7 | 3 |
| 4 | MIS | 14 | 7 | s8 | 4 |
+------+-----------+---------+-------+------+---------+
7 rows in set (0.01 sec)
左链接
select * from tb_dept1 a left join tb_dept2 b on a.id = b.dept_id where b.dept_id is null;
+----+-----------+---------+-------+------+---------+
| id | dept_name | loc_add | tb_id | name | dept_id |
+----+-----------+---------+-------+------+---------+
| 5 | FD | 15 | NULL | NULL | NULL |
+----+-----------+---------+-------+------+---------+
1 row in set (0.00 sec)
右链接