MySQL常用操作(数据操作)---DAY_4

插入记录操作

-- inser infor
CREATE TABLE IF NOT EXISTS user1(
id TINYINT UNSIGNED auto_increment key,
username VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(20) not NULL DEFAULT '123456',
email VARCHAR(50) not NULL,
age TINYINT  NOT NULL DEFAULT 18
);

-- keep in order
INSERT user1 VALUES (1,'king','king','[email protected]',18);
INSERT user1 VALUE (2,'kings','king','[email protected]',18);
INSERT user1(username,password) value('a','a');
ALTER TABLE user1 ALTER email set DEFAULT '[email protected]';

-- add more than one infor
INSERT user1 VALUES(DEFAULT,'d','dd',DEFAULT,35),
(6,'d1','d1d',DEFAULT,DEFAULT);


INSERT user1 SET id=98,username='test',password='111,',email=DEFAULT,age=DEFAULT;

INSERT user1 SET username='test1';

-- add infor from other tables
CREATE TABLE IF NOT EXISTS user2(
id TINYINT UNSIGNED auto_increment key,
username VARCHAR(20) NOT NULL UNIQUE

);

-- inser result into user2 
INSERT user2 SELECT id,username from user1;
-- must keep index type and setting in order

删除与更新记录

-- update and delete 
UPDATE user1 SET age=15;
UPDATE user1 SET age=11,email='[email protected]';
UPDATE user1 SET age=15,email='[email protected]'WHERE id=;
UPDATE user1 SET age=age-5,email='[email protected]'WHERE id>=8;
UPDATE user1 SET age=DEFAULT WHERE username='d1';
DELETE from user1 ;
DELETE from user1 WHERE id=6;

-- delete infor compeletely
TRUNCATE TABLE user1;
-- can not use where condition

你可能感兴趣的:(MySQL常用操作(数据操作)---DAY_4)