enter image description hereI MADE A TABLE THEN ADDED A COLUMN TO IT NOW ALL THE COLUMN VALUES ARE NULL IN ALL ROWS
insert into students
value
('05','0005','fname','2000','m','fyit','1234567894','king5','332','500','624','650','CS','st','walk','email');
email is where the null value is supposed to be.
1)
create database siws;
use siws;
create table students
(
student_id int(2) AUTO_INCREMENT primary key,
roll_no int(4),
fullname char(10),
dob int(4),
gender char(1),
class char(4),
phone_no bigint(10),
address varchar(20),
ssc int(3),
totssc int(3),
hsc int(3),
tothsc int(3),
department char(2),
caste char(4),
travelling char(6)
);
2)
Insert into students
value ('01','0001','aname','2000','m','syit','1234567890','king1','499','500','649','650','IT','open','bike');
insert into students
value ('02','0002','bname','2001','f','syit','1234567891','king2','497','500','639','650','CS','obc','cycle');
insert into students
value ('03','0003','cname','2002','m','fyit','1234567892','king3','437','500','629','650','IT','sc','car');
insert into students
value ('04','0004','dname','2002','f','syit','1234567893','king4','344','500','619','650','IT','open','plane');
insert into students
value ('05','0005','fname','2000','m','fyit','1234567894','king5','332','500','624','650','CS','st','walk');
3)
ALTER TABLE students
ADD Email varchar(255); //add column
sorry for the long codes im new here :)
for add a value to an existint row you need update
Update students
set email = 'youreamil#yexample.com'
where student_id =5
Related
i'm creating second table in HEIDI SQL, but it doesn't work.
it shows 1050 error: user1 aleady exists.
I aleady created user1 table and I try to create user2 table. However it doesn't
#2. TABLE
CREATE TABLE user1(
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
# TABLE 2
CREATE TABLE user2(
user_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(30) UNIQUE NOT NULL,
age INT(3) DEFAULT 30,
rdate TIMESTAMP
);
You can run this script, which will avoid this error "1050 error: user1 aleady exists.":
CREATE TABLE IF NOT EXISTS `user1 ` (
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
NB: your user1 table doesn't have a primary key and for the age column, it's advisable to store the date of birth because the age can be calculated.
I would advise you to write the script using drop table if exists:
DROP TABLE IF EXISTS user1;
CREATE TABLE user1 (
user_id INT,
name VARCHAR(20),
email VARCHAR(30),
age INT(3),
rdata DATE
);
# TABLE 2
DROP TABLE IF EXISTS;
CREATE TABLE user2(
user_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
email VARCHAR(30) UNIQUE NOT NULL,
age INT(3) DEFAULT 30,
rdate TIMESTAMP
);
CREATE TABLE IF NOT EXISTS will leave the previous version of the table -- which is troublesome if you make changes to the definitions. Using such constructs, I have spent a fair amount of time debugging code when the structure of a table didn't change.
I am trying to create three tables using MySQL Workbench, where two columns need to be auto incremented with a fixed starting value. I have checked some online resources and figured it out what statements to use.
create database test;
use test;
/*table Project */
create table Project(
Pnumber INT NOT NULL AUTO_INCREMENT,
Pname varchar(30) NOT NULL,
Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL,
primary key(Pnumber)
);
ALTER TABLE Project AUTO_INCREMENT = 7777770;
/*table Department*/
Create table Department(
Dcode varchar(5) NOT NULL,
Dname varchar(30),
Dmg_ssn varchar(30),
primary key(Dcode)
);
/* Table Employee*/
create table Employee(
Ssn INT NOT NULL AUTO_INCREMENT,
Ename varchar(30) NOT NULL,
Bdate DATE,
Address varchar(30),
Dcode varchar(5) NOT NULL,
Driver_License varchar(30),
primary key(Ssn),
foreign key(Dcode) references Department(Dcode)
);
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
/* Insert into Project*/
Insert into Project values ('7777770','Star', 'QLD');
Insert into Project values ('7777771','Innova', 'NSW');
Insert into Project values ('7777772','Andra', "QLD");
/* Insert into Department */
insert into Department values ('ABC12', 'Finances', 'RA12');
insert into Department values ('WXY10', 'Human Resources', 'RA12');
insert into Department values ('PBC32', 'S2', 'RB13');
/*Insert into Employee */
insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
However, after I created the tables, only the column Pnumber in table Project follows the required format, while the column Ssn in Employee does not.
This is a SELECT over the Employee table:
Ssn,Ename,Bdate,Address,Dcode,Driver_License
1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456
2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947
3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655
Any idea what I am doing wrong??
Your sql script has an employee table, not a student, I assume these two names refer to the same table. I will use the employee name.
So, you set the start value auto increment of the employee table to 1000000:
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
But then you explicitly insert 1, 2, and 3 into this column with your insert statements because '0000001' translates into 1. If you explicitly insert a value into auto increment and it is higher than the maximum value in the given field, then mysql will insert that value as is into the auto increment field.
If you are using auto increment, then you should let it work and do not specify an explicit value:
/*Insert into Employee */
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
Im trying to create a Column thats Value is defined through anUPDATE JOIN SET statement. The exact question that im trying to answer actually is
"Add to a relational table EMPLOYEE information about the total number of orders handled by each employee. Note, that if an employee handled no orders then for such employee the total number of orders must be set to zero. Enforce the appropriate consistency constraints on a relational table EMPLOYEE. "
ALTER TABLE EMPLOYEE
ADD TOTALNUMBER VARCHAR(40) NOT NULL;
UPDATE EMPLOYEE E JOIN ORDERS O ON(E.EMPLOYEE_ID = O.EMPLOYEE_ID)
SET E.TOTALNUMBER = E.EMPLOYEE_ID + O.ORDER_ID;
UPDATE EMPLOYEE
SET TOTALNUMBER = 0
WHERE TOTALNUMBER IS NULL;
tables being used
CREATE TABLE EMPLOYEE
(
EMPLOYEE_ID DECIMAL(9) NOT NULL,
LASTNAME VARCHAR(20) NOT NULL,
FIRSTNAME VARCHAR(10) NOT NULL,
TITLE VARCHAR(30),
TITLE_OF_COURTESY VARCHAR(25),
BIRTHDATE DATE,
HIREDATE DATE,
ADDRESS VARCHAR(60),
CITY VARCHAR(15),
REGION VARCHAR(15),
POSTAL_CODE VARCHAR(10),
COUNTRY VARCHAR(15),
HOME_PHONE VARCHAR(24),
EXTENSION VARCHAR(4),
PHOTO VARCHAR(255),
NOTES VARCHAR(2000),
REPORTS_TO DECIMAL(9),
CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPLOYEE_ID)
);
CREATE TABLE ORDERS
(
ORDER_ID DECIMAL(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID DECIMAL(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE,
SHIPPED_DATE DATE,
SHIP_VIA VARCHAR(40),
FREIGHT DECIMAL(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40),
SHIP_ADDRESS VARCHAR(60),
SHIP_CITY VARCHAR(15),
SHIP_REGION VARCHAR(15),
SHIP_POSTAL_CODE VARCHAR(10),
SHIP_COUNTRY VARCHAR(15),
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
Im unsure of what the exact result should be but i recieve a total of 9 rows with values ranging between 252 to 296. It doesnt seem to odd that an employee would deal with this many Orders but it seems to be too small of a list.
I don't recommend your current approach, and the best way to determine the number of orders per employees is to just aggregate and join, without storing this number in the actual employee table. That being said, if you want to proceed this way, then consider using this update query:
UPDATE EMPLOYEE e
LEFT JOIN
(
SELECT EMPLOYEE_ID, COUNT(*) AS num_orders
FROM ORDERS
GROUP BY EMPLOYEE_ID
) o
ON e.EMPLOYEE_ID = o.EMPLOYEE_ID
SET TOTALNUMBER = COALESCE(o.num_orders, 0);
Or, you could use:
SET TOTALNUMBER = o.num_orders;
and then use your second update to zero-out the employee totals which had no orders at all:
UPDATE EMPLOYEE
SET TOTALNUMBER = 0
WHERE TOTALNUMBER IS NULL;
But note that this would require that the TOTALNUMBER columns is not nullable. So you would need to remove the NOT NULL constraint.
Im not to sure if this is possible. I'm developing a fantasy golf tournament App asr a project. The user picks 6 golfers from six groups, each group contains ten golfers. The group that the golfer is in is determined by the group boolean in the golfers table. The scores table is used to record the competition entries.
I have two tables. A golfers table.
CREATE TABLE golfers (
golferid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
secondtname VARCHAR(30) NOT NULL,
country VARCHAR(50),
worldranking int(3),
tournamentposition int(2),
group1 boolean,
group2 boolean,
group3 boolean,
group4 boolean,
group5 boolean,
group6 boolean,
day1score int(2),
day2score int(2),
day3score int(2),
day4score int(2),
totalscore int(3),
golfscoretotal int(3)
);
And a scores table. As seen below.
CREATE TABLE scores (
scoreid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userid INT(11) NOT NULL,
golferid1 INT(6),
golfscoretotal1 INT(3),
golferid2 INT(6),
golfscoretotal2 INT(3),
golferid3 INT(6),
golfscoretotal3 INT(3),
golferid4 INT(6),
golfscoretotal4 INT(3),
golferid5 INT(6),
golfscoretotal5 INT(3),
golferid6 INT(6),
golfscoretotal6 INT(3),
totalscore INT(4),
FOREIGN KEY fk_userid REFERENCES users(id)
);
Is it possible to update the scores in the scores table (taken from the golfers table) for each golfer based on the id of the golfer in the golferid1 INT(6) column before the golfscoretotal1 column.
You could use something simple like this for golferid1:
CREATE TRIGGER update_scores1 After INSERT ON golfers FOR EACH ROW
UPDATE scores
SET golfscoretotal1 = new.golfscoretotal
WHERE golferid1 = NEW.golferid
Then for the others just create more triggers like this, resulting in a total of 6 triggers:
CREATE TRIGGER update_scores2 After INSERT ON golfers FOR EACH ROW
UPDATE scores
SET golfscoretotal2 = new.golfscoretotal
WHERE golferid2 = NEW.golferid
I've just started on SQL and so far I've made this and it works
CREATE TABLE employees(employee_ID int NOT NULL, name varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (employee_ID)
);
INSERT INTO employees VALUES(1, 'Adam Jones');
INSERT INTO employees VALUES(2, 'Amy Smith');
INSERT INTO employees VALUES(3, 'Anthony Wright');
CREATE TABLE department(department_ID varchar(20) NOT NULL,
department_name varchar(20) NOT NULL, head_of_dep varchar(20),
num_of_employees_in_dep int
);
INSERT INTO department VALUES('Bad At SQL Ltd', 'Need Help HQ', 'No One Yet', 3);
But I cant understand why this wont work
UPDATE department SET head_of_dep = name FROM employees WHERE employee_ID = 1
What am I doing wrong?
Using SQLfiddle and MySQL 5.5.32
You need to rewrite it to
UPDATE department SET head_of_dep = (SELECT name FROM employees WHERE employee_ID = 1)
because you actually have to SELECT the value